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."""
11 from chromite.lib import cros_build_lib
12 from chromite.lib import osutils
14 CHROME_COMMITTER_URL = 'svn://svn.chromium.org/chrome'
15 SVN_MIRROR_URL = 'svn://svn-mirror.golo.chromium.org'
16 STATUS_URL = 'https://chromium-status.appspot.com/current?format=json'
19 def FindGclientFile(path):
20 """Returns the nearest higher-level gclient file from the specified path.
23 path: The path to use. Defaults to cwd.
25 return osutils.FindInPathParents(
26 '.gclient', path, test_func=os.path.isfile)
29 def FindGclientCheckoutRoot(path):
30 """Get the root of your gclient managed checkout."""
31 gclient_path = FindGclientFile(path)
33 return os.path.dirname(gclient_path)
38 """Check whether to use the golo.chromium.org mirrors.
40 This function returns whether or not we should use the mirrors from
41 golo.chromium.org, which we presume are only accessible from within
42 that subdomain, and a few other known exceptions.
44 host = cros_build_lib.GetHostName(fully_qualified=True)
47 '.chrome.corp.google.com',
49 return any([host.endswith(s) for s in GOLO_SUFFIXES])
53 """Get the base URLs for checking out Chromium and Chrome."""
55 external_url = '%s/chrome' % SVN_MIRROR_URL
56 internal_url = '%s/chrome-internal' % SVN_MIRROR_URL
58 external_url = 'http://src.chromium.org/svn'
59 internal_url = 'svn://svn.chromium.org/chrome-internal'
61 # No mirror for this one at the moment.
62 pdf_url = 'svn://svn.chromium.org/pdf'
64 return external_url, internal_url, pdf_url
67 def GetTipOfTrunkSvnRevision(svn_url):
68 """Returns the current svn revision for the chrome tree."""
69 cmd = ['svn', 'info', svn_url]
70 svn_info = cros_build_lib.RunCommand(cmd, redirect_stdout=True).output
72 revision_re = re.compile(r'^Revision:\s+(\d+)')
73 for line in svn_info.splitlines():
74 match = revision_re.match(line)
76 svn_revision = match.group(1)
77 cros_build_lib.Info('Found SVN Revision %s' % svn_revision)
80 raise Exception('Could not find revision information from %s' % svn_url)
83 def _GetGclientURLs(internal, use_pdf, rev):
84 """Get the URLs to use in gclient file.
86 See WriteConfigFile below.
89 external_url, internal_url, pdf_url = GetBaseURLs()
91 if rev is None or isinstance(rev, (int, long)):
92 rev_str = '@%s' % rev if rev else ''
93 results.append(('src', '%s/trunk/src%s' % (external_url, rev_str)))
95 results.append(('src-internal', '%s/trunk/src-internal' % internal_url))
97 results.append(('src-pdf', '%s/trunk/chrome' % pdf_url))
99 # TODO(petermayo): Fall back to the archive directory if needed.
100 primary_url = '%s/trunk/tools/buildspec/releases/%s' % (internal_url, rev)
101 results.append(('CHROME_DEPS', primary_url))
103 results.append(('CHROMIUM_DEPS', '%s/releases/%s' % (external_url, rev)))
108 def _GetGclientSolutions(internal, use_pdf, rev):
109 """Get the solutions array to write to the gclient file.
111 See WriteConfigFile below.
113 urls = _GetGclientURLs(internal, use_pdf, rev)
114 custom_deps, custom_vars = {}, {}
115 # This suppresses pdf from a buildspec
117 custom_deps.update({'src/pdf': None, 'src-pdf': None})
120 'svn_url': SVN_MIRROR_URL,
121 'webkit_trunk': '%s/webkit-readonly/trunk' % SVN_MIRROR_URL,
122 'googlecode_url': SVN_MIRROR_URL + '/%s',
123 'gsutil': SVN_MIRROR_URL + '/gsutil',
124 'sourceforge_url': SVN_MIRROR_URL + '/%(repo)s'
127 solutions = [{'name': name,
129 'custom_deps': custom_deps,
130 'custom_vars': custom_vars} for (name, url) in urls]
134 def _GetGclientSpec(internal, use_pdf, rev):
135 """Return a formatted gclient spec.
137 See WriteConfigFile below.
139 solutions = _GetGclientSolutions(internal=internal, use_pdf=use_pdf, rev=rev)
140 return 'solutions = %s\n' % pprint.pformat(solutions)
143 def WriteConfigFile(gclient, cwd, internal, use_pdf, rev):
144 """Initialize the specified directory as a gclient checkout.
146 For gclient documentation, see:
147 http://src.chromium.org/svn/trunk/tools/depot_tools/README.gclient
150 gclient: Path to gclient.
151 cwd: Directory to sync.
152 internal: Whether you want an internal checkout.
153 use_pdf: Whether you want the PDF source code.
154 rev: Revision or tag to use. If None, use the latest from trunk. If this is
155 a number, use the specified revision. If this is a string, use the
158 spec = _GetGclientSpec(internal=internal, use_pdf=use_pdf, rev=rev)
159 cmd = [gclient, 'config', '--spec', spec]
160 cros_build_lib.RunCommand(cmd, cwd=cwd)
163 def Revert(gclient, cwd):
164 """Revert all local changes.
167 gclient: Path to gclient.
168 cwd: Directory to revert.
170 cros_build_lib.RunCommand([gclient, 'revert', '--nohooks'], cwd=cwd)
173 def Sync(gclient, cwd, reset=False):
174 """Sync the specified directory using gclient.
177 gclient: Path to gclient.
178 cwd: Directory to sync.
179 reset: Reset to pristine version of the source code.
181 cmd = [gclient, 'sync', '--verbose', '--nohooks', '--transitive',
182 '--manually_grab_svn_rev']
184 cmd += ['--reset', '--force', '--delete_unversioned_trees']
185 cros_build_lib.RunCommand(cmd, cwd=cwd)