1 # Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Common functions used for syncing Chrome."""
7 from __future__ import print_function
12 from chromite.cbuildbot import constants
13 from chromite.lib import cros_build_lib
14 from chromite.lib import git
15 from chromite.lib import osutils
17 CHROME_COMMITTER_URL = 'https://chromium.googlesource.com/chromium/src'
18 STATUS_URL = 'https://chromium-status.appspot.com/current?format=json'
21 def FindGclientFile(path):
22 """Returns the nearest higher-level gclient file from the specified path.
25 path: The path to use. Defaults to cwd.
27 return osutils.FindInPathParents(
28 '.gclient', path, test_func=os.path.isfile)
31 def FindGclientCheckoutRoot(path):
32 """Get the root of your gclient managed checkout."""
33 gclient_path = FindGclientFile(path)
35 return os.path.dirname(gclient_path)
39 def _GetGclientURLs(internal, rev):
40 """Get the URLs and deps_file values to use in gclient file.
42 See WriteConfigFile below.
46 if rev is None or git.IsSHA1(rev):
47 # Regular chromium checkout; src may float to origin/master or be pinned.
48 url = constants.CHROMIUM_GOB_URL
51 # TODO(szager): .DEPS.git will eventually be deprecated in favor of DEPS.
52 # When that happens, this could should continue to work, because gclient
53 # will fall back to DEPS if .DEPS.git doesn't exist. Eventually, this
54 # code should be cleaned up to stop referring to non-existent .DEPS.git.
55 results.append(('src', url, '.DEPS.git'))
58 ('src-internal', constants.CHROME_INTERNAL_GOB_URL, '.DEPS.git'))
60 # Internal buildspec: check out the buildspec repo and set deps_file to
61 # the path to the desired release spec.
62 url = constants.INTERNAL_GOB_URL + '/chrome/tools/buildspec.git'
63 results.append(('CHROME_DEPS', url, 'releases/%s/.DEPS.git' % rev))
65 # External buildspec: use the main chromium src repository, pinned to the
66 # release tag, with deps_file set to .DEPS.git (which is created by
68 url = constants.CHROMIUM_GOB_URL + '@refs/tags/' + rev
69 results.append(('src', url, '.DEPS.git'))
74 def _GetGclientSolutions(internal, rev):
75 """Get the solutions array to write to the gclient file.
77 See WriteConfigFile below.
79 urls = _GetGclientURLs(internal, rev)
81 for (name, url, deps_file) in urls:
89 solution['deps_file'] = deps_file
90 solutions.append(solution)
94 def _GetGclientSpec(internal, rev):
95 """Return a formatted gclient spec.
97 See WriteConfigFile below.
99 solutions = _GetGclientSolutions(internal=internal, rev=rev)
100 result = 'solutions = %s\n' % pprint.pformat(solutions)
102 # Horrible hack, I will go to hell for this. The bots need to have a git
103 # cache set up; but how can we tell whether this code is running on a bot
104 # or a developer's machine?
105 if cros_build_lib.HostIsCIBuilder():
106 result += "cache_dir = '/b/git-cache'\n"
110 def WriteConfigFile(gclient, cwd, internal, rev):
111 """Initialize the specified directory as a gclient checkout.
113 For gclient documentation, see:
114 http://src.chromium.org/svn/trunk/tools/depot_tools/README.gclient
117 gclient: Path to gclient.
118 cwd: Directory to sync.
119 internal: Whether you want an internal checkout.
120 rev: Revision or tag to use.
121 - If None, use the latest from trunk.
122 - If this is a sha1, use the specified revision.
123 - Otherwise, treat this as a chrome version string.
125 spec = _GetGclientSpec(internal=internal, rev=rev)
126 cmd = [gclient, 'config', '--spec', spec]
127 cros_build_lib.RunCommand(cmd, cwd=cwd)
130 def Revert(gclient, cwd):
131 """Revert all local changes.
134 gclient: Path to gclient.
135 cwd: Directory to revert.
137 cros_build_lib.RunCommand([gclient, 'revert', '--nohooks'], cwd=cwd)
140 def Sync(gclient, cwd, reset=False):
141 """Sync the specified directory using gclient.
144 gclient: Path to gclient.
145 cwd: Directory to sync.
146 reset: Reset to pristine version of the source code.
148 cmd = [gclient, 'sync', '--verbose', '--nohooks', '--transitive',
149 '--with_branch_heads', '--with_tags']
152 cmd += ['--reset', '--force', '--delete_unversioned_trees']
153 cros_build_lib.RunCommand(cmd, cwd=cwd)