Upstream version 8.36.161.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / lib / gclient.py
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.
4
5 """Common functions used for syncing Chrome."""
6
7 import os
8 import pprint
9 import re
10
11 from chromite.lib import cros_build_lib
12 from chromite.lib import osutils
13
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'
17
18
19 def FindGclientFile(path):
20   """Returns the nearest higher-level gclient file from the specified path.
21
22   Args:
23     path: The path to use. Defaults to cwd.
24   """
25   return osutils.FindInPathParents(
26       '.gclient', path, test_func=os.path.isfile)
27
28
29 def FindGclientCheckoutRoot(path):
30   """Get the root of your gclient managed checkout."""
31   gclient_path = FindGclientFile(path)
32   if gclient_path:
33     return os.path.dirname(gclient_path)
34   return None
35
36
37 def _UseGoloMirror():
38   """Check whether to use the golo.chromium.org mirrors.
39
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.
43   """
44   host = cros_build_lib.GetHostName(fully_qualified=True)
45   GOLO_SUFFIXES = [
46       '.golo.chromium.org',
47       '.chrome.corp.google.com',
48   ]
49   return any([host.endswith(s) for s in GOLO_SUFFIXES])
50
51
52 def GetBaseURLs():
53   """Get the base URLs for checking out Chromium and Chrome."""
54   if _UseGoloMirror():
55     external_url = '%s/chrome' % SVN_MIRROR_URL
56     internal_url = '%s/chrome-internal' % SVN_MIRROR_URL
57   else:
58     external_url = 'http://src.chromium.org/svn'
59     internal_url = 'svn://svn.chromium.org/chrome-internal'
60
61   # No mirror for this one at the moment.
62   pdf_url = 'svn://svn.chromium.org/pdf'
63
64   return external_url, internal_url, pdf_url
65
66
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
71
72   revision_re = re.compile(r'^Revision:\s+(\d+)')
73   for line in svn_info.splitlines():
74     match = revision_re.match(line)
75     if match:
76       svn_revision = match.group(1)
77       cros_build_lib.Info('Found SVN Revision %s' % svn_revision)
78       return svn_revision
79
80   raise Exception('Could not find revision information from %s' % svn_url)
81
82
83 def _GetGclientURLs(internal, use_pdf, rev):
84   """Get the URLs to use in gclient file.
85
86   See WriteConfigFile below.
87   """
88   results = []
89   external_url, internal_url, pdf_url = GetBaseURLs()
90
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)))
94     if internal:
95       results.append(('src-internal', '%s/trunk/src-internal' % internal_url))
96     if use_pdf:
97       results.append(('src-pdf', '%s/trunk/chrome' % pdf_url))
98   elif internal:
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))
102   else:
103     results.append(('CHROMIUM_DEPS', '%s/releases/%s' % (external_url, rev)))
104
105   return results
106
107
108 def _GetGclientSolutions(internal, use_pdf, rev):
109   """Get the solutions array to write to the gclient file.
110
111   See WriteConfigFile below.
112   """
113   urls = _GetGclientURLs(internal, use_pdf, rev)
114   custom_deps, custom_vars = {}, {}
115   # This suppresses pdf from a buildspec
116   if not use_pdf:
117     custom_deps.update({'src/pdf': None, 'src-pdf': None})
118   if _UseGoloMirror():
119     custom_vars.update({
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'
125     })
126
127   solutions = [{'name': name,
128                 'url': url,
129                 'custom_deps': custom_deps,
130                 'custom_vars': custom_vars} for (name, url) in urls]
131   return solutions
132
133
134 def _GetGclientSpec(internal, use_pdf, rev):
135   """Return a formatted gclient spec.
136
137   See WriteConfigFile below.
138   """
139   solutions = _GetGclientSolutions(internal=internal, use_pdf=use_pdf, rev=rev)
140   return 'solutions = %s\n' % pprint.pformat(solutions)
141
142
143 def WriteConfigFile(gclient, cwd, internal, use_pdf, rev):
144   """Initialize the specified directory as a gclient checkout.
145
146   For gclient documentation, see:
147     http://src.chromium.org/svn/trunk/tools/depot_tools/README.gclient
148
149   Args:
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
156       specified tag.
157   """
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)
161
162
163 def Revert(gclient, cwd):
164   """Revert all local changes.
165
166   Args:
167     gclient: Path to gclient.
168     cwd: Directory to revert.
169   """
170   cros_build_lib.RunCommand([gclient, 'revert', '--nohooks'], cwd=cwd)
171
172
173 def Sync(gclient, cwd, reset=False):
174   """Sync the specified directory using gclient.
175
176   Args:
177     gclient: Path to gclient.
178     cwd: Directory to sync.
179     reset: Reset to pristine version of the source code.
180   """
181   cmd = [gclient, 'sync', '--verbose', '--nohooks', '--transitive',
182          '--manually_grab_svn_rev']
183   if reset:
184     cmd += ['--reset', '--force', '--delete_unversioned_trees']
185   cros_build_lib.RunCommand(cmd, cwd=cwd)