Upstream version 10.39.225.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 from __future__ import print_function
8
9 import os
10 import pprint
11
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
16
17 CHROME_COMMITTER_URL = 'https://chromium.googlesource.com/chromium/src'
18 STATUS_URL = 'https://chromium-status.appspot.com/current?format=json'
19
20
21 def FindGclientFile(path):
22   """Returns the nearest higher-level gclient file from the specified path.
23
24   Args:
25     path: The path to use. Defaults to cwd.
26   """
27   return osutils.FindInPathParents(
28       '.gclient', path, test_func=os.path.isfile)
29
30
31 def FindGclientCheckoutRoot(path):
32   """Get the root of your gclient managed checkout."""
33   gclient_path = FindGclientFile(path)
34   if gclient_path:
35     return os.path.dirname(gclient_path)
36   return None
37
38
39 def _GetGclientURLs(internal, rev):
40   """Get the URLs and deps_file values to use in gclient file.
41
42   See WriteConfigFile below.
43   """
44   results = []
45
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
49     if rev:
50       url += ('@' + rev)
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'))
56     if internal:
57       results.append(
58           ('src-internal', constants.CHROME_INTERNAL_GOB_URL, '.DEPS.git'))
59   elif internal:
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))
64   else:
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
67     # publish_deps.py).
68     url = constants.CHROMIUM_GOB_URL + '@refs/tags/' + rev
69     results.append(('src', url, '.DEPS.git'))
70
71   return results
72
73
74 def _GetGclientSolutions(internal, rev):
75   """Get the solutions array to write to the gclient file.
76
77   See WriteConfigFile below.
78   """
79   urls = _GetGclientURLs(internal, rev)
80   solutions = []
81   for (name, url, deps_file) in urls:
82     solution = {
83         'name': name,
84         'url': url,
85         'custom_deps': {},
86         'custom_vars': {},
87     }
88     if deps_file:
89       solution['deps_file'] = deps_file
90     solutions.append(solution)
91   return solutions
92
93
94 def _GetGclientSpec(internal, rev):
95   """Return a formatted gclient spec.
96
97   See WriteConfigFile below.
98   """
99   solutions = _GetGclientSolutions(internal=internal, rev=rev)
100   result = 'solutions = %s\n' % pprint.pformat(solutions)
101
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"
107
108   return result
109
110 def WriteConfigFile(gclient, cwd, internal, rev):
111   """Initialize the specified directory as a gclient checkout.
112
113   For gclient documentation, see:
114     http://src.chromium.org/svn/trunk/tools/depot_tools/README.gclient
115
116   Args:
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.
124   """
125   spec = _GetGclientSpec(internal=internal, rev=rev)
126   cmd = [gclient, 'config', '--spec', spec]
127   cros_build_lib.RunCommand(cmd, cwd=cwd)
128
129
130 def Revert(gclient, cwd):
131   """Revert all local changes.
132
133   Args:
134     gclient: Path to gclient.
135     cwd: Directory to revert.
136   """
137   cros_build_lib.RunCommand([gclient, 'revert', '--nohooks'], cwd=cwd)
138
139
140 def Sync(gclient, cwd, reset=False):
141   """Sync the specified directory using gclient.
142
143   Args:
144     gclient: Path to gclient.
145     cwd: Directory to sync.
146     reset: Reset to pristine version of the source code.
147   """
148   cmd = [gclient, 'sync', '--verbose', '--nohooks', '--transitive',
149          '--with_branch_heads', '--with_tags']
150
151   if reset:
152     cmd += ['--reset', '--force', '--delete_unversioned_trees']
153   cros_build_lib.RunCommand(cmd, cwd=cwd)