[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1 | #!/usr/bin/python |
[email protected] | ba55177 | 2010-02-03 18:21:42 | [diff] [blame] | 2 | # Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 5 | |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 6 | """Meta checkout manager supporting both Subversion and GIT. |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 7 | |
| 8 | Files |
| 9 | .gclient : Current client configuration, written by 'config' command. |
| 10 | Format is a Python script defining 'solutions', a list whose |
| 11 | entries each are maps binding the strings "name" and "url" |
| 12 | to strings specifying the name and location of the client |
| 13 | module, as well as "custom_deps" to a map similar to the DEPS |
| 14 | file below. |
| 15 | .gclient_entries : A cache constructed by 'update' command. Format is a |
| 16 | Python script defining 'entries', a list of the names |
| 17 | of all modules in the client |
| 18 | <module>/DEPS : Python script defining var 'deps' as a map from each requisite |
| 19 | submodule name to a URL where it can be found (via one SCM) |
| 20 | |
| 21 | Hooks |
| 22 | .gclient and DEPS files may optionally contain a list named "hooks" to |
| 23 | allow custom actions to be performed based on files that have changed in the |
[email protected] | 67820ef | 2009-07-27 17:23:00 | [diff] [blame] | 24 | working copy as a result of a "sync"/"update" or "revert" operation. This |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 25 | can be prevented by using --nohooks (hooks run by default). Hooks can also |
[email protected] | 5df6a46 | 2009-08-28 18:52:26 | [diff] [blame] | 26 | be forced to run with the "runhooks" operation. If "sync" is run with |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 27 | --force, all known hooks will run regardless of the state of the working |
| 28 | copy. |
| 29 | |
| 30 | Each item in a "hooks" list is a dict, containing these two keys: |
| 31 | "pattern" The associated value is a string containing a regular |
| 32 | expression. When a file whose pathname matches the expression |
| 33 | is checked out, updated, or reverted, the hook's "action" will |
| 34 | run. |
| 35 | "action" A list describing a command to run along with its arguments, if |
| 36 | any. An action command will run at most one time per gclient |
| 37 | invocation, regardless of how many files matched the pattern. |
| 38 | The action is executed in the same directory as the .gclient |
| 39 | file. If the first item in the list is the string "python", |
| 40 | the current Python interpreter (sys.executable) will be used |
[email protected] | 71b4068 | 2009-07-31 23:40:09 | [diff] [blame] | 41 | to run the command. If the list contains string "$matching_files" |
| 42 | it will be removed from the list and the list will be extended |
| 43 | by the list of matching files. |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 44 | |
| 45 | Example: |
| 46 | hooks = [ |
| 47 | { "pattern": "\\.(gif|jpe?g|pr0n|png)$", |
| 48 | "action": ["python", "image_indexer.py", "--all"]}, |
| 49 | ] |
| 50 | """ |
| 51 | |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 52 | __version__ = "0.4.1" |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 53 | |
| 54 | import errno |
[email protected] | 754960e | 2009-09-21 12:31:05 | [diff] [blame] | 55 | import logging |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 56 | import optparse |
| 57 | import os |
[email protected] | 2e38de7 | 2009-09-28 17:04:47 | [diff] [blame] | 58 | import pprint |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 59 | import re |
[email protected] | 4b90e3a | 2010-07-01 20:28:26 | [diff] [blame] | 60 | import subprocess |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 61 | import sys |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 62 | import urlparse |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 63 | import urllib |
| 64 | |
[email protected] | ada4c65 | 2009-12-03 15:32:01 | [diff] [blame] | 65 | import breakpad |
| 66 | |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 67 | import gclient_scm |
| 68 | import gclient_utils |
[email protected] | 1f7a3d1 | 2010-02-04 15:11:50 | [diff] [blame] | 69 | from third_party.repo.progress import Progress |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 70 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 71 | |
[email protected] | 1f7d118 | 2010-05-17 18:17:38 | [diff] [blame] | 72 | def attr(attr, data): |
| 73 | """Sets an attribute on a function.""" |
| 74 | def hook(fn): |
| 75 | setattr(fn, attr, data) |
| 76 | return fn |
| 77 | return hook |
[email protected] | e3da35f | 2010-03-09 21:40:45 | [diff] [blame] | 78 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 79 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 80 | ## GClient implementation. |
| 81 | |
| 82 | |
[email protected] | 116704f | 2010-06-11 17:34:38 | [diff] [blame] | 83 | class GClientKeywords(object): |
| 84 | class FromImpl(object): |
| 85 | """Used to implement the From() syntax.""" |
| 86 | |
| 87 | def __init__(self, module_name, sub_target_name=None): |
| 88 | """module_name is the dep module we want to include from. It can also be |
| 89 | the name of a subdirectory to include from. |
| 90 | |
| 91 | sub_target_name is an optional parameter if the module name in the other |
| 92 | DEPS file is different. E.g., you might want to map src/net to net.""" |
| 93 | self.module_name = module_name |
| 94 | self.sub_target_name = sub_target_name |
| 95 | |
| 96 | def __str__(self): |
| 97 | return 'From(%s, %s)' % (repr(self.module_name), |
| 98 | repr(self.sub_target_name)) |
| 99 | |
| 100 | def GetUrl(self, target_name, sub_deps_base_url, root_dir, sub_deps): |
| 101 | """Resolve the URL for this From entry.""" |
| 102 | sub_deps_target_name = target_name |
| 103 | if self.sub_target_name: |
| 104 | sub_deps_target_name = self.sub_target_name |
| 105 | url = sub_deps[sub_deps_target_name] |
| 106 | if url.startswith('/'): |
| 107 | # If it's a relative URL, we need to resolve the URL relative to the |
| 108 | # sub deps base URL. |
| 109 | if not isinstance(sub_deps_base_url, basestring): |
| 110 | sub_deps_base_url = sub_deps_base_url.GetPath() |
| 111 | scm = gclient_scm.CreateSCM(sub_deps_base_url, root_dir, |
| 112 | None) |
| 113 | url = scm.FullUrlForRelativeUrl(url) |
| 114 | return url |
| 115 | |
| 116 | class FileImpl(object): |
| 117 | """Used to implement the File('') syntax which lets you sync a single file |
| 118 | from an SVN repo.""" |
| 119 | |
| 120 | def __init__(self, file_location): |
| 121 | self.file_location = file_location |
| 122 | |
| 123 | def __str__(self): |
| 124 | return 'File("%s")' % self.file_location |
| 125 | |
| 126 | def GetPath(self): |
| 127 | return os.path.split(self.file_location)[0] |
| 128 | |
| 129 | def GetFilename(self): |
| 130 | rev_tokens = self.file_location.split('@') |
| 131 | return os.path.split(rev_tokens[0])[1] |
| 132 | |
| 133 | def GetRevision(self): |
| 134 | rev_tokens = self.file_location.split('@') |
| 135 | if len(rev_tokens) > 1: |
| 136 | return rev_tokens[1] |
| 137 | return None |
| 138 | |
| 139 | class VarImpl(object): |
| 140 | def __init__(self, custom_vars, local_scope): |
| 141 | self._custom_vars = custom_vars |
| 142 | self._local_scope = local_scope |
| 143 | |
| 144 | def Lookup(self, var_name): |
| 145 | """Implements the Var syntax.""" |
| 146 | if var_name in self._custom_vars: |
| 147 | return self._custom_vars[var_name] |
| 148 | elif var_name in self._local_scope.get("vars", {}): |
| 149 | return self._local_scope["vars"][var_name] |
| 150 | raise gclient_utils.Error("Var is not defined: %s" % var_name) |
| 151 | |
| 152 | |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 153 | class Dependency(GClientKeywords): |
| 154 | """Object that represents a dependency checkout.""" |
[email protected] | 9eda411 | 2010-06-11 18:56:10 | [diff] [blame] | 155 | DEPS_FILE = 'DEPS' |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 156 | |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 157 | def __init__(self, parent, name, url, safesync_url=None, custom_deps=None, |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 158 | custom_vars=None, deps_file=None): |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 159 | GClientKeywords.__init__(self) |
| 160 | self.parent = parent |
| 161 | self.name = name |
| 162 | self.url = url |
| 163 | # These 2 are only set in .gclient and not in DEPS files. |
| 164 | self.safesync_url = safesync_url |
| 165 | self.custom_vars = custom_vars or {} |
| 166 | self.custom_deps = custom_deps or {} |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 167 | self.deps_hooks = [] |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 168 | self.dependencies = [] |
| 169 | self.deps_file = deps_file or self.DEPS_FILE |
[email protected] | 271375b | 2010-06-23 19:17:38 | [diff] [blame] | 170 | self.deps_parsed = False |
| 171 | self.direct_reference = False |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 172 | |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 173 | # Sanity checks |
| 174 | if not self.name and self.parent: |
| 175 | raise gclient_utils.Error('Dependency without name') |
| 176 | if not isinstance(self.url, |
| 177 | (basestring, self.FromImpl, self.FileImpl, None.__class__)): |
| 178 | raise gclient_utils.Error('dependency url must be either a string, None, ' |
| 179 | 'File() or From() instead of %s' % |
| 180 | self.url.__class__.__name__) |
| 181 | if '/' in self.deps_file or '\\' in self.deps_file: |
| 182 | raise gclient_utils.Error('deps_file name must not be a path, just a ' |
| 183 | 'filename. %s' % self.deps_file) |
| 184 | |
[email protected] | 271375b | 2010-06-23 19:17:38 | [diff] [blame] | 185 | def ParseDepsFile(self, direct_reference): |
| 186 | """Parses the DEPS file for this dependency.""" |
| 187 | if direct_reference: |
| 188 | # Maybe it was referenced earlier by a From() keyword but it's now |
| 189 | # directly referenced. |
| 190 | self.direct_reference = direct_reference |
| 191 | self.deps_parsed = True |
| 192 | filepath = os.path.join(self.root_dir(), self.name, self.deps_file) |
| 193 | if not os.path.isfile(filepath): |
[email protected] | 0d42592 | 2010-06-21 19:22:24 | [diff] [blame] | 194 | return {} |
[email protected] | 271375b | 2010-06-23 19:17:38 | [diff] [blame] | 195 | deps_content = gclient_utils.FileRead(filepath) |
[email protected] | 0d42592 | 2010-06-21 19:22:24 | [diff] [blame] | 196 | |
[email protected] | 271375b | 2010-06-23 19:17:38 | [diff] [blame] | 197 | # Eval the content. |
| 198 | # One thing is unintuitive, vars= {} must happen before Var() use. |
| 199 | local_scope = {} |
| 200 | var = self.VarImpl(self.custom_vars, local_scope) |
| 201 | global_scope = { |
| 202 | 'File': self.FileImpl, |
| 203 | 'From': self.FromImpl, |
| 204 | 'Var': var.Lookup, |
| 205 | 'deps_os': {}, |
| 206 | } |
[email protected] | 5990f9d | 2010-07-07 18:02:58 | [diff] [blame] | 207 | try: |
| 208 | exec(deps_content, global_scope, local_scope) |
| 209 | except SyntaxError, e: |
| 210 | gclient_utils.SyntaxErrorToError(filepath, e) |
[email protected] | 271375b | 2010-06-23 19:17:38 | [diff] [blame] | 211 | deps = local_scope.get('deps', {}) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 212 | # load os specific dependencies if defined. these dependencies may |
| 213 | # override or extend the values defined by the 'deps' member. |
[email protected] | 271375b | 2010-06-23 19:17:38 | [diff] [blame] | 214 | if 'deps_os' in local_scope: |
| 215 | for deps_os_key in self.enforced_os(): |
| 216 | os_deps = local_scope['deps_os'].get(deps_os_key, {}) |
| 217 | if len(self.enforced_os()) > 1: |
| 218 | # Ignore any conflict when including deps for more than one |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 219 | # platform, so we collect the broadest set of dependencies available. |
| 220 | # We may end up with the wrong revision of something for our |
| 221 | # platform, but this is the best we can do. |
| 222 | deps.update([x for x in os_deps.items() if not x[0] in deps]) |
| 223 | else: |
| 224 | deps.update(os_deps) |
| 225 | |
[email protected] | 271375b | 2010-06-23 19:17:38 | [diff] [blame] | 226 | self.deps_hooks.extend(local_scope.get('hooks', [])) |
| 227 | |
| 228 | # If a line is in custom_deps, but not in the solution, we want to append |
| 229 | # this line to the solution. |
| 230 | for d in self.custom_deps: |
| 231 | if d not in deps: |
| 232 | deps[d] = self.custom_deps[d] |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 233 | |
| 234 | # If use_relative_paths is set in the DEPS file, regenerate |
| 235 | # the dictionary using paths relative to the directory containing |
| 236 | # the DEPS file. |
[email protected] | 271375b | 2010-06-23 19:17:38 | [diff] [blame] | 237 | use_relative_paths = local_scope.get('use_relative_paths', False) |
| 238 | if use_relative_paths: |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 239 | rel_deps = {} |
| 240 | for d, url in deps.items(): |
| 241 | # normpath is required to allow DEPS to use .. in their |
| 242 | # dependency local path. |
[email protected] | 271375b | 2010-06-23 19:17:38 | [diff] [blame] | 243 | rel_deps[os.path.normpath(os.path.join(self.name, d))] = url |
| 244 | deps = rel_deps |
| 245 | # TODO(maruel): Add these dependencies into self.dependencies. |
| 246 | return deps |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 247 | |
[email protected] | 271375b | 2010-06-23 19:17:38 | [diff] [blame] | 248 | def _ParseAllDeps(self, solution_urls): |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 249 | """Parse the complete list of dependencies for the client. |
| 250 | |
| 251 | Args: |
| 252 | solution_urls: A dict mapping module names (as relative paths) to URLs |
| 253 | corresponding to the solutions specified by the client. This parameter |
| 254 | is passed as an optimization. |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 255 | |
| 256 | Returns: |
| 257 | A dict mapping module names (as relative paths) to URLs corresponding |
| 258 | to the entire set of dependencies to checkout for the given client. |
| 259 | |
| 260 | Raises: |
| 261 | Error: If a dependency conflicts with another dependency or of a solution. |
| 262 | """ |
| 263 | deps = {} |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 264 | for solution in self.dependencies: |
[email protected] | 271375b | 2010-06-23 19:17:38 | [diff] [blame] | 265 | solution_deps = solution.ParseDepsFile(True) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 266 | |
| 267 | # If a line is in custom_deps, but not in the solution, we want to append |
| 268 | # this line to the solution. |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 269 | for d in solution.custom_deps: |
| 270 | if d not in solution_deps: |
| 271 | solution_deps[d] = solution.custom_deps[d] |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 272 | |
| 273 | for d in solution_deps: |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 274 | if d in solution.custom_deps: |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 275 | # Dependency is overriden. |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 276 | url = solution.custom_deps[d] |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 277 | if url is None: |
| 278 | continue |
| 279 | else: |
| 280 | url = solution_deps[d] |
| 281 | # if we have a From reference dependent on another solution, then |
| 282 | # just skip the From reference. When we pull deps for the solution, |
| 283 | # we will take care of this dependency. |
| 284 | # |
| 285 | # If multiple solutions all have the same From reference, then we |
| 286 | # should only add one to our list of dependencies. |
[email protected] | 4b5b177 | 2010-04-08 01:52:56 | [diff] [blame] | 287 | if isinstance(url, self.FromImpl): |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 288 | if url.module_name in solution_urls: |
| 289 | # Already parsed. |
| 290 | continue |
| 291 | if d in deps and type(deps[d]) != str: |
| 292 | if url.module_name == deps[d].module_name: |
| 293 | continue |
[email protected] | 4b5b177 | 2010-04-08 01:52:56 | [diff] [blame] | 294 | elif isinstance(url, str): |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 295 | parsed_url = urlparse.urlparse(url) |
| 296 | scheme = parsed_url[0] |
| 297 | if not scheme: |
| 298 | # A relative url. Fetch the real base. |
| 299 | path = parsed_url[2] |
| 300 | if path[0] != "/": |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 301 | raise gclient_utils.Error( |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 302 | "relative DEPS entry \"%s\" must begin with a slash" % d) |
[email protected] | e6f7835 | 2010-01-13 17:05:33 | [diff] [blame] | 303 | # Create a scm just to query the full url. |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 304 | scm = gclient_scm.CreateSCM(solution.url, self.root_dir(), |
| 305 | None) |
[email protected] | e6f7835 | 2010-01-13 17:05:33 | [diff] [blame] | 306 | url = scm.FullUrlForRelativeUrl(url) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 307 | if d in deps and deps[d] != url: |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 308 | raise gclient_utils.Error( |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 309 | "Solutions have conflicting versions of dependency \"%s\"" % d) |
| 310 | if d in solution_urls and solution_urls[d] != url: |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 311 | raise gclient_utils.Error( |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 312 | "Dependency \"%s\" conflicts with specified solution" % d) |
| 313 | # Grab the dependency. |
| 314 | deps[d] = url |
| 315 | return deps |
| 316 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 317 | def _RunHooks(self, command, file_list, is_using_git): |
| 318 | """Evaluates all hooks, running actions as needed. |
| 319 | """ |
| 320 | # Hooks only run for these command types. |
| 321 | if not command in ('update', 'revert', 'runhooks'): |
| 322 | return |
| 323 | |
[email protected] | 67820ef | 2009-07-27 17:23:00 | [diff] [blame] | 324 | # Hooks only run when --nohooks is not specified |
| 325 | if self._options.nohooks: |
| 326 | return |
| 327 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 328 | # Get any hooks from the .gclient file. |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 329 | hooks = self.deps_hooks[:] |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 330 | # Add any hooks found in DEPS files. |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 331 | for d in self.dependencies: |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 332 | hooks.extend(d.deps_hooks) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 333 | |
| 334 | # If "--force" was specified, run all hooks regardless of what files have |
| 335 | # changed. If the user is using git, then we don't know what files have |
| 336 | # changed so we always run all hooks. |
| 337 | if self._options.force or is_using_git: |
| 338 | for hook_dict in hooks: |
[email protected] | 71b4068 | 2009-07-31 23:40:09 | [diff] [blame] | 339 | self._RunHookAction(hook_dict, []) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 340 | return |
| 341 | |
| 342 | # Run hooks on the basis of whether the files from the gclient operation |
| 343 | # match each hook's pattern. |
| 344 | for hook_dict in hooks: |
| 345 | pattern = re.compile(hook_dict['pattern']) |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 346 | matching_file_list = [f for f in file_list if pattern.search(f)] |
[email protected] | 71b4068 | 2009-07-31 23:40:09 | [diff] [blame] | 347 | if matching_file_list: |
| 348 | self._RunHookAction(hook_dict, matching_file_list) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 349 | |
[email protected] | eaf6106 | 2010-07-07 18:42:39 | [diff] [blame^] | 350 | def _RunHookAction(self, hook_dict, matching_file_list): |
| 351 | """Runs the action from a single hook.""" |
| 352 | logging.info(hook_dict) |
| 353 | logging.info(matching_file_list) |
| 354 | command = hook_dict['action'][:] |
| 355 | if command[0] == 'python': |
| 356 | # If the hook specified "python" as the first item, the action is a |
| 357 | # Python script. Run it by starting a new copy of the same |
| 358 | # interpreter. |
| 359 | command[0] = sys.executable |
| 360 | |
| 361 | if '$matching_files' in command: |
| 362 | splice_index = command.index('$matching_files') |
| 363 | command[splice_index:splice_index + 1] = matching_file_list |
| 364 | |
| 365 | # Use a discrete exit status code of 2 to indicate that a hook action |
| 366 | # failed. Users of this script may wish to treat hook action failures |
| 367 | # differently from VC failures. |
| 368 | return gclient_utils.SubprocessCall(command, self.root_dir(), fail_status=2) |
| 369 | |
[email protected] | 271375b | 2010-06-23 19:17:38 | [diff] [blame] | 370 | def root_dir(self): |
| 371 | return self.parent.root_dir() |
| 372 | |
| 373 | def enforced_os(self): |
| 374 | return self.parent.enforced_os() |
| 375 | |
[email protected] | d36fba8 | 2010-06-28 16:50:40 | [diff] [blame] | 376 | def recursion_limit(self): |
| 377 | return self.parent.recursion_limit() - 1 |
| 378 | |
| 379 | def tree(self, force_all): |
| 380 | return self.parent.tree(force_all) |
| 381 | |
| 382 | def get_custom_deps(self, name, url): |
| 383 | """Returns a custom deps if applicable.""" |
| 384 | if self.parent: |
| 385 | url = self.parent.get_custom_deps(name, url) |
| 386 | # None is a valid return value to disable a dependency. |
| 387 | return self.custom_deps.get(name, url) |
| 388 | |
| 389 | def __str__(self): |
| 390 | out = [] |
| 391 | for i in ('name', 'url', 'safesync_url', 'custom_deps', 'custom_vars', |
| 392 | 'deps_hooks'): |
| 393 | # 'deps_file' |
| 394 | if self.__dict__[i]: |
| 395 | out.append('%s: %s' % (i, self.__dict__[i])) |
| 396 | |
| 397 | for d in self.dependencies: |
| 398 | out.extend([' ' + x for x in str(d).splitlines()]) |
| 399 | out.append('') |
| 400 | return '\n'.join(out) |
| 401 | |
| 402 | def __repr__(self): |
| 403 | return '%s: %s' % (self.name, self.url) |
| 404 | |
[email protected] | 9a66ddf | 2010-06-16 16:54:16 | [diff] [blame] | 405 | |
| 406 | class GClient(Dependency): |
[email protected] | d36fba8 | 2010-06-28 16:50:40 | [diff] [blame] | 407 | """Object that represent a gclient checkout. A tree of Dependency(), one per |
| 408 | solution or DEPS entry.""" |
[email protected] | 9a66ddf | 2010-06-16 16:54:16 | [diff] [blame] | 409 | |
| 410 | DEPS_OS_CHOICES = { |
| 411 | "win32": "win", |
| 412 | "win": "win", |
| 413 | "cygwin": "win", |
| 414 | "darwin": "mac", |
| 415 | "mac": "mac", |
| 416 | "unix": "unix", |
| 417 | "linux": "unix", |
| 418 | "linux2": "unix", |
| 419 | } |
| 420 | |
| 421 | DEFAULT_CLIENT_FILE_TEXT = ("""\ |
| 422 | solutions = [ |
| 423 | { "name" : "%(solution_name)s", |
| 424 | "url" : "%(solution_url)s", |
| 425 | "custom_deps" : { |
| 426 | }, |
[email protected] | 73e2114 | 2010-07-05 13:32:01 | [diff] [blame] | 427 | "safesync_url": "%(safesync_url)s", |
[email protected] | 9a66ddf | 2010-06-16 16:54:16 | [diff] [blame] | 428 | }, |
| 429 | ] |
| 430 | """) |
| 431 | |
| 432 | DEFAULT_SNAPSHOT_SOLUTION_TEXT = ("""\ |
| 433 | { "name" : "%(solution_name)s", |
| 434 | "url" : "%(solution_url)s", |
| 435 | "custom_deps" : { |
[email protected] | 73e2114 | 2010-07-05 13:32:01 | [diff] [blame] | 436 | %(solution_deps)s }, |
| 437 | "safesync_url": "%(safesync_url)s", |
[email protected] | 9a66ddf | 2010-06-16 16:54:16 | [diff] [blame] | 438 | }, |
| 439 | """) |
| 440 | |
| 441 | DEFAULT_SNAPSHOT_FILE_TEXT = ("""\ |
| 442 | # Snapshot generated with gclient revinfo --snapshot |
| 443 | solutions = [ |
[email protected] | 73e2114 | 2010-07-05 13:32:01 | [diff] [blame] | 444 | %(solution_list)s] |
[email protected] | 9a66ddf | 2010-06-16 16:54:16 | [diff] [blame] | 445 | """) |
| 446 | |
| 447 | def __init__(self, root_dir, options): |
| 448 | Dependency.__init__(self, None, None, None) |
[email protected] | 0d42592 | 2010-06-21 19:22:24 | [diff] [blame] | 449 | self._options = options |
[email protected] | 271375b | 2010-06-23 19:17:38 | [diff] [blame] | 450 | if options.deps_os: |
| 451 | enforced_os = options.deps_os.split(',') |
| 452 | else: |
| 453 | enforced_os = [self.DEPS_OS_CHOICES.get(sys.platform, 'unix')] |
| 454 | if 'all' in enforced_os: |
| 455 | enforced_os = self.DEPS_OS_CHOICES.itervalues() |
| 456 | self._enforced_os = list(set(enforced_os)) |
| 457 | self._root_dir = root_dir |
[email protected] | 9a66ddf | 2010-06-16 16:54:16 | [diff] [blame] | 458 | self.config_content = None |
[email protected] | d36fba8 | 2010-06-28 16:50:40 | [diff] [blame] | 459 | # Do not change previous behavior. Only solution level and immediate DEPS |
| 460 | # are processed. |
| 461 | self._recursion_limit = 2 |
[email protected] | 9a66ddf | 2010-06-16 16:54:16 | [diff] [blame] | 462 | |
| 463 | def SetConfig(self, content): |
| 464 | assert self.dependencies == [] |
| 465 | config_dict = {} |
| 466 | self.config_content = content |
| 467 | try: |
| 468 | exec(content, config_dict) |
| 469 | except SyntaxError, e: |
[email protected] | 5990f9d | 2010-07-07 18:02:58 | [diff] [blame] | 470 | gclient_utils.SyntaxErrorToError('.gclient', e) |
[email protected] | 9a66ddf | 2010-06-16 16:54:16 | [diff] [blame] | 471 | for s in config_dict.get('solutions', []): |
[email protected] | 81843b8 | 2010-06-28 16:49:26 | [diff] [blame] | 472 | try: |
| 473 | self.dependencies.append(Dependency( |
| 474 | self, s['name'], s['url'], |
| 475 | s.get('safesync_url', None), |
| 476 | s.get('custom_deps', {}), |
| 477 | s.get('custom_vars', {}))) |
| 478 | except KeyError: |
| 479 | raise gclient_utils.Error('Invalid .gclient file. Solution is ' |
| 480 | 'incomplete: %s' % s) |
[email protected] | 9a66ddf | 2010-06-16 16:54:16 | [diff] [blame] | 481 | # .gclient can have hooks. |
| 482 | self.deps_hooks = config_dict.get('hooks', []) |
| 483 | |
| 484 | def SaveConfig(self): |
| 485 | gclient_utils.FileWrite(os.path.join(self.root_dir(), |
| 486 | self._options.config_filename), |
| 487 | self.config_content) |
| 488 | |
| 489 | @staticmethod |
| 490 | def LoadCurrentConfig(options): |
| 491 | """Searches for and loads a .gclient file relative to the current working |
| 492 | dir. Returns a GClient object.""" |
| 493 | path = gclient_utils.FindGclientRoot(os.getcwd(), options.config_filename) |
| 494 | if not path: |
| 495 | return None |
| 496 | client = GClient(path, options) |
| 497 | client.SetConfig(gclient_utils.FileRead( |
| 498 | os.path.join(path, options.config_filename))) |
| 499 | return client |
| 500 | |
| 501 | def SetDefaultConfig(self, solution_name, solution_url, safesync_url): |
| 502 | self.SetConfig(self.DEFAULT_CLIENT_FILE_TEXT % { |
| 503 | 'solution_name': solution_name, |
| 504 | 'solution_url': solution_url, |
| 505 | 'safesync_url' : safesync_url, |
| 506 | }) |
| 507 | |
| 508 | def _SaveEntries(self, entries): |
| 509 | """Creates a .gclient_entries file to record the list of unique checkouts. |
| 510 | |
| 511 | The .gclient_entries file lives in the same directory as .gclient. |
[email protected] | 9a66ddf | 2010-06-16 16:54:16 | [diff] [blame] | 512 | """ |
| 513 | # Sometimes pprint.pformat will use {', sometimes it'll use { ' ... It |
| 514 | # makes testing a bit too fun. |
| 515 | result = pprint.pformat(entries, 2) |
| 516 | if result.startswith('{\''): |
| 517 | result = '{ \'' + result[2:] |
[email protected] | 73e2114 | 2010-07-05 13:32:01 | [diff] [blame] | 518 | text = 'entries = \\\n' + result + '\n' |
[email protected] | 9a66ddf | 2010-06-16 16:54:16 | [diff] [blame] | 519 | file_path = os.path.join(self.root_dir(), self._options.entries_filename) |
| 520 | gclient_utils.FileWrite(file_path, text) |
| 521 | |
| 522 | def _ReadEntries(self): |
| 523 | """Read the .gclient_entries file for the given client. |
| 524 | |
| 525 | Returns: |
| 526 | A sequence of solution names, which will be empty if there is the |
| 527 | entries file hasn't been created yet. |
| 528 | """ |
| 529 | scope = {} |
| 530 | filename = os.path.join(self.root_dir(), self._options.entries_filename) |
| 531 | if not os.path.exists(filename): |
[email protected] | 73e2114 | 2010-07-05 13:32:01 | [diff] [blame] | 532 | return {} |
[email protected] | 5990f9d | 2010-07-07 18:02:58 | [diff] [blame] | 533 | try: |
| 534 | exec(gclient_utils.FileRead(filename), scope) |
| 535 | except SyntaxError, e: |
| 536 | gclient_utils.SyntaxErrorToError(filename, e) |
[email protected] | 9a66ddf | 2010-06-16 16:54:16 | [diff] [blame] | 537 | return scope['entries'] |
| 538 | |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 539 | def _EnforceRevisions(self): |
[email protected] | 918a9ae | 2010-05-28 15:50:30 | [diff] [blame] | 540 | """Checks for revision overrides.""" |
| 541 | revision_overrides = {} |
[email protected] | 307d179 | 2010-05-31 20:03:13 | [diff] [blame] | 542 | if self._options.head: |
| 543 | return revision_overrides |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 544 | for s in self.dependencies: |
| 545 | if not s.safesync_url: |
[email protected] | 307d179 | 2010-05-31 20:03:13 | [diff] [blame] | 546 | continue |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 547 | handle = urllib.urlopen(s.safesync_url) |
[email protected] | 307d179 | 2010-05-31 20:03:13 | [diff] [blame] | 548 | rev = handle.read().strip() |
| 549 | handle.close() |
| 550 | if len(rev): |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 551 | self._options.revisions.append('%s@%s' % (s.name, rev)) |
[email protected] | 307d179 | 2010-05-31 20:03:13 | [diff] [blame] | 552 | if not self._options.revisions: |
| 553 | return revision_overrides |
| 554 | # --revision will take over safesync_url. |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 555 | solutions_names = [s.name for s in self.dependencies] |
[email protected] | 307d179 | 2010-05-31 20:03:13 | [diff] [blame] | 556 | index = 0 |
| 557 | for revision in self._options.revisions: |
| 558 | if not '@' in revision: |
| 559 | # Support for --revision 123 |
| 560 | revision = '%s@%s' % (solutions_names[index], revision) |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 561 | sol, rev = revision.split('@', 1) |
[email protected] | 307d179 | 2010-05-31 20:03:13 | [diff] [blame] | 562 | if not sol in solutions_names: |
| 563 | #raise gclient_utils.Error('%s is not a valid solution.' % sol) |
| 564 | print >> sys.stderr, ('Please fix your script, having invalid ' |
| 565 | '--revision flags will soon considered an error.') |
| 566 | else: |
[email protected] | 918a9ae | 2010-05-28 15:50:30 | [diff] [blame] | 567 | revision_overrides[sol] = rev |
[email protected] | 307d179 | 2010-05-31 20:03:13 | [diff] [blame] | 568 | index += 1 |
[email protected] | 918a9ae | 2010-05-28 15:50:30 | [diff] [blame] | 569 | return revision_overrides |
| 570 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 571 | def RunOnDeps(self, command, args): |
| 572 | """Runs a command on each dependency in a client and its dependencies. |
| 573 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 574 | Args: |
| 575 | command: The command to use (e.g., 'status' or 'diff') |
| 576 | args: list of str - extra arguments to add to the command line. |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 577 | """ |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 578 | if not self.dependencies: |
[email protected] | 73e2114 | 2010-07-05 13:32:01 | [diff] [blame] | 579 | raise gclient_utils.Error('No solution specified') |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 580 | revision_overrides = self._EnforceRevisions() |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 581 | |
| 582 | # When running runhooks --force, there's no need to consult the SCM. |
| 583 | # All known hooks are expected to run unconditionally regardless of working |
| 584 | # copy state, so skip the SCM status check. |
| 585 | run_scm = not (command == 'runhooks' and self._options.force) |
| 586 | |
| 587 | entries = {} |
[email protected] | d2e9256 | 2010-04-27 01:55:18 | [diff] [blame] | 588 | file_list = [] |
| 589 | # Run on the base solutions first. |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 590 | for solution in self.dependencies: |
| 591 | name = solution.name |
[email protected] | d2e9256 | 2010-04-27 01:55:18 | [diff] [blame] | 592 | if name in entries: |
| 593 | raise gclient_utils.Error("solution %s specified more than once" % name) |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 594 | url = solution.url |
[email protected] | d2e9256 | 2010-04-27 01:55:18 | [diff] [blame] | 595 | entries[name] = url |
| 596 | if run_scm and url: |
| 597 | self._options.revision = revision_overrides.get(name) |
[email protected] | 75a5927 | 2010-06-11 22:34:03 | [diff] [blame] | 598 | scm = gclient_scm.CreateSCM(url, self.root_dir(), name) |
[email protected] | d2e9256 | 2010-04-27 01:55:18 | [diff] [blame] | 599 | scm.RunCommand(command, self._options, args, file_list) |
| 600 | file_list = [os.path.join(name, f.strip()) for f in file_list] |
| 601 | self._options.revision = None |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 602 | |
[email protected] | d2e9256 | 2010-04-27 01:55:18 | [diff] [blame] | 603 | # Process the dependencies next (sort alphanumerically to ensure that |
| 604 | # containing directories get populated first and for readability) |
[email protected] | 271375b | 2010-06-23 19:17:38 | [diff] [blame] | 605 | deps = self._ParseAllDeps(entries) |
[email protected] | d2e9256 | 2010-04-27 01:55:18 | [diff] [blame] | 606 | deps_to_process = deps.keys() |
| 607 | deps_to_process.sort() |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 608 | |
[email protected] | d2e9256 | 2010-04-27 01:55:18 | [diff] [blame] | 609 | # First pass for direct dependencies. |
| 610 | if command == 'update' and not self._options.verbose: |
| 611 | pm = Progress('Syncing projects', len(deps_to_process)) |
| 612 | for d in deps_to_process: |
[email protected] | 1f7a3d1 | 2010-02-04 15:11:50 | [diff] [blame] | 613 | if command == 'update' and not self._options.verbose: |
[email protected] | d2e9256 | 2010-04-27 01:55:18 | [diff] [blame] | 614 | pm.update() |
| 615 | if type(deps[d]) == str: |
| 616 | url = deps[d] |
| 617 | entries[d] = url |
| 618 | if run_scm: |
| 619 | self._options.revision = revision_overrides.get(d) |
[email protected] | 75a5927 | 2010-06-11 22:34:03 | [diff] [blame] | 620 | scm = gclient_scm.CreateSCM(url, self.root_dir(), d) |
[email protected] | d2e9256 | 2010-04-27 01:55:18 | [diff] [blame] | 621 | scm.RunCommand(command, self._options, args, file_list) |
| 622 | self._options.revision = None |
| 623 | elif isinstance(deps[d], self.FileImpl): |
[email protected] | 491c04b | 2010-05-17 18:17:44 | [diff] [blame] | 624 | file_dep = deps[d] |
| 625 | self._options.revision = file_dep.GetRevision() |
[email protected] | d2e9256 | 2010-04-27 01:55:18 | [diff] [blame] | 626 | if run_scm: |
[email protected] | 75a5927 | 2010-06-11 22:34:03 | [diff] [blame] | 627 | scm = gclient_scm.CreateSCM(file_dep.GetPath(), self.root_dir(), d) |
[email protected] | d2e9256 | 2010-04-27 01:55:18 | [diff] [blame] | 628 | scm.RunCommand("updatesingle", self._options, |
[email protected] | 491c04b | 2010-05-17 18:17:44 | [diff] [blame] | 629 | args + [file_dep.GetFilename()], file_list) |
[email protected] | 79692d6 | 2010-05-14 18:57:13 | [diff] [blame] | 630 | |
[email protected] | d2e9256 | 2010-04-27 01:55:18 | [diff] [blame] | 631 | if command == 'update' and not self._options.verbose: |
| 632 | pm.end() |
[email protected] | 6f36372 | 2010-04-27 00:41:09 | [diff] [blame] | 633 | |
[email protected] | d2e9256 | 2010-04-27 01:55:18 | [diff] [blame] | 634 | # Second pass for inherited deps (via the From keyword) |
| 635 | for d in deps_to_process: |
| 636 | if isinstance(deps[d], self.FromImpl): |
[email protected] | d2e9256 | 2010-04-27 01:55:18 | [diff] [blame] | 637 | # Getting the URL from the sub_deps file can involve having to resolve |
| 638 | # a File() or having to resolve a relative URL. To resolve relative |
| 639 | # URLs, we need to pass in the orignal sub deps URL. |
| 640 | sub_deps_base_url = deps[deps[d].module_name] |
[email protected] | 271375b | 2010-06-23 19:17:38 | [diff] [blame] | 641 | sub_deps = Dependency(self, deps[d].module_name, sub_deps_base_url |
| 642 | ).ParseDepsFile(False) |
[email protected] | 75a5927 | 2010-06-11 22:34:03 | [diff] [blame] | 643 | url = deps[d].GetUrl(d, sub_deps_base_url, self.root_dir(), sub_deps) |
[email protected] | d2e9256 | 2010-04-27 01:55:18 | [diff] [blame] | 644 | entries[d] = url |
| 645 | if run_scm: |
| 646 | self._options.revision = revision_overrides.get(d) |
[email protected] | 75a5927 | 2010-06-11 22:34:03 | [diff] [blame] | 647 | scm = gclient_scm.CreateSCM(url, self.root_dir(), d) |
[email protected] | d2e9256 | 2010-04-27 01:55:18 | [diff] [blame] | 648 | scm.RunCommand(command, self._options, args, file_list) |
| 649 | self._options.revision = None |
[email protected] | df2d590 | 2009-09-11 22:16:21 | [diff] [blame] | 650 | |
[email protected] | d83b2b2 | 2009-08-11 15:30:55 | [diff] [blame] | 651 | # Convert all absolute paths to relative. |
| 652 | for i in range(len(file_list)): |
| 653 | # TODO(phajdan.jr): We should know exactly when the paths are absolute. |
| 654 | # It depends on the command being executed (like runhooks vs sync). |
| 655 | if not os.path.isabs(file_list[i]): |
| 656 | continue |
| 657 | |
[email protected] | 75a5927 | 2010-06-11 22:34:03 | [diff] [blame] | 658 | prefix = os.path.commonprefix([self.root_dir().lower(), |
[email protected] | d83b2b2 | 2009-08-11 15:30:55 | [diff] [blame] | 659 | file_list[i].lower()]) |
| 660 | file_list[i] = file_list[i][len(prefix):] |
| 661 | |
| 662 | # Strip any leading path separators. |
| 663 | while file_list[i].startswith('\\') or file_list[i].startswith('/'): |
| 664 | file_list[i] = file_list[i][1:] |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 665 | |
[email protected] | 75a5927 | 2010-06-11 22:34:03 | [diff] [blame] | 666 | is_using_git = gclient_utils.IsUsingGit(self.root_dir(), entries.keys()) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 667 | self._RunHooks(command, file_list, is_using_git) |
| 668 | |
| 669 | if command == 'update': |
[email protected] | cdcee80 | 2009-06-23 15:30:42 | [diff] [blame] | 670 | # Notify the user if there is an orphaned entry in their working copy. |
| 671 | # Only delete the directory if there are no changes in it, and |
| 672 | # delete_unversioned_trees is set to true. |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 673 | prev_entries = self._ReadEntries() |
| 674 | for entry in prev_entries: |
[email protected] | c5e9aec | 2009-08-03 18:25:56 | [diff] [blame] | 675 | # Fix path separator on Windows. |
| 676 | entry_fixed = entry.replace('/', os.path.sep) |
[email protected] | 75a5927 | 2010-06-11 22:34:03 | [diff] [blame] | 677 | e_dir = os.path.join(self.root_dir(), entry_fixed) |
[email protected] | c5e9aec | 2009-08-03 18:25:56 | [diff] [blame] | 678 | # Use entry and not entry_fixed there. |
[email protected] | 0329e67 | 2009-05-13 18:41:04 | [diff] [blame] | 679 | if entry not in entries and os.path.exists(e_dir): |
[email protected] | 8301701 | 2009-09-28 18:52:12 | [diff] [blame] | 680 | modified_files = False |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 681 | if isinstance(prev_entries, list): |
[email protected] | 8301701 | 2009-09-28 18:52:12 | [diff] [blame] | 682 | # old .gclient_entries format was list, now dict |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 683 | modified_files = gclient_scm.scm.SVN.CaptureStatus(e_dir) |
[email protected] | 8301701 | 2009-09-28 18:52:12 | [diff] [blame] | 684 | else: |
| 685 | file_list = [] |
[email protected] | 75a5927 | 2010-06-11 22:34:03 | [diff] [blame] | 686 | scm = gclient_scm.CreateSCM(prev_entries[entry], self.root_dir(), |
[email protected] | 8301701 | 2009-09-28 18:52:12 | [diff] [blame] | 687 | entry_fixed) |
| 688 | scm.status(self._options, [], file_list) |
| 689 | modified_files = file_list != [] |
| 690 | if not self._options.delete_unversioned_trees or modified_files: |
[email protected] | c5e9aec | 2009-08-03 18:25:56 | [diff] [blame] | 691 | # There are modified files in this entry. Keep warning until |
| 692 | # removed. |
[email protected] | d36fba8 | 2010-06-28 16:50:40 | [diff] [blame] | 693 | print(('\nWARNING: \'%s\' is no longer part of this client. ' |
| 694 | 'It is recommended that you manually remove it.\n') % |
[email protected] | c5e9aec | 2009-08-03 18:25:56 | [diff] [blame] | 695 | entry_fixed) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 696 | else: |
| 697 | # Delete the entry |
[email protected] | 73e2114 | 2010-07-05 13:32:01 | [diff] [blame] | 698 | print('\n________ deleting \'%s\' in \'%s\'' % ( |
| 699 | entry_fixed, self.root_dir())) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 700 | gclient_utils.RemoveDirectory(e_dir) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 701 | # record the current list of entries for next time |
| 702 | self._SaveEntries(entries) |
[email protected] | 17cdf76 | 2010-05-28 17:30:52 | [diff] [blame] | 703 | return 0 |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 704 | |
| 705 | def PrintRevInfo(self): |
[email protected] | 5d63eb8 | 2010-03-24 23:22:09 | [diff] [blame] | 706 | """Output revision info mapping for the client and its dependencies. |
| 707 | |
| 708 | This allows the capture of an overall "revision" for the source tree that |
[email protected] | 918a9ae | 2010-05-28 15:50:30 | [diff] [blame] | 709 | can be used to reproduce the same tree in the future. It is only useful for |
| 710 | "unpinned dependencies", i.e. DEPS/deps references without a svn revision |
| 711 | number or a git hash. A git branch name isn't "pinned" since the actual |
| 712 | commit can change. |
[email protected] | 5d63eb8 | 2010-03-24 23:22:09 | [diff] [blame] | 713 | |
| 714 | The --snapshot option allows creating a .gclient file to reproduce the tree. |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 715 | """ |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 716 | if not self.dependencies: |
[email protected] | 73e2114 | 2010-07-05 13:32:01 | [diff] [blame] | 717 | raise gclient_utils.Error('No solution specified') |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 718 | |
[email protected] | 5d63eb8 | 2010-03-24 23:22:09 | [diff] [blame] | 719 | # Inner helper to generate base url and rev tuple |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 720 | def GetURLAndRev(name, original_url): |
[email protected] | 5d63eb8 | 2010-03-24 23:22:09 | [diff] [blame] | 721 | url, _ = gclient_utils.SplitUrlRevision(original_url) |
[email protected] | 75a5927 | 2010-06-11 22:34:03 | [diff] [blame] | 722 | scm = gclient_scm.CreateSCM(original_url, self.root_dir(), name) |
[email protected] | 5d63eb8 | 2010-03-24 23:22:09 | [diff] [blame] | 723 | return (url, scm.revinfo(self._options, [], None)) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 724 | |
[email protected] | e3da35f | 2010-03-09 21:40:45 | [diff] [blame] | 725 | # text of the snapshot gclient file |
| 726 | new_gclient = "" |
| 727 | # Dictionary of { path : SCM url } to ensure no duplicate solutions |
| 728 | solution_names = {} |
[email protected] | de8f352 | 2010-03-11 23:47:44 | [diff] [blame] | 729 | entries = {} |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 730 | # Run on the base solutions first. |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 731 | for solution in self.dependencies: |
[email protected] | e3da35f | 2010-03-09 21:40:45 | [diff] [blame] | 732 | # Dictionary of { path : SCM url } to describe the gclient checkout |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 733 | name = solution.name |
[email protected] | e3da35f | 2010-03-09 21:40:45 | [diff] [blame] | 734 | if name in solution_names: |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 735 | raise gclient_utils.Error("solution %s specified more than once" % name) |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 736 | (url, rev) = GetURLAndRev(name, solution.url) |
[email protected] | 770ff9e | 2009-09-23 17:18:18 | [diff] [blame] | 737 | entries[name] = "%s@%s" % (url, rev) |
[email protected] | e3da35f | 2010-03-09 21:40:45 | [diff] [blame] | 738 | solution_names[name] = "%s@%s" % (url, rev) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 739 | |
[email protected] | de8f352 | 2010-03-11 23:47:44 | [diff] [blame] | 740 | # Process the dependencies next (sort alphanumerically to ensure that |
| 741 | # containing directories get populated first and for readability) |
[email protected] | 271375b | 2010-06-23 19:17:38 | [diff] [blame] | 742 | deps = self._ParseAllDeps(entries) |
[email protected] | de8f352 | 2010-03-11 23:47:44 | [diff] [blame] | 743 | deps_to_process = deps.keys() |
| 744 | deps_to_process.sort() |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 745 | |
[email protected] | de8f352 | 2010-03-11 23:47:44 | [diff] [blame] | 746 | # First pass for direct dependencies. |
| 747 | for d in deps_to_process: |
| 748 | if type(deps[d]) == str: |
| 749 | (url, rev) = GetURLAndRev(d, deps[d]) |
| 750 | entries[d] = "%s@%s" % (url, rev) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 751 | |
[email protected] | de8f352 | 2010-03-11 23:47:44 | [diff] [blame] | 752 | # Second pass for inherited deps (via the From keyword) |
| 753 | for d in deps_to_process: |
[email protected] | 4b5b177 | 2010-04-08 01:52:56 | [diff] [blame] | 754 | if isinstance(deps[d], self.FromImpl): |
[email protected] | de8f352 | 2010-03-11 23:47:44 | [diff] [blame] | 755 | deps_parent_url = entries[deps[d].module_name] |
| 756 | if deps_parent_url.find("@") < 0: |
| 757 | raise gclient_utils.Error("From %s missing revisioned url" % |
| 758 | deps[d].module_name) |
[email protected] | 271375b | 2010-06-23 19:17:38 | [diff] [blame] | 759 | sub_deps_base_url = deps[deps[d].module_name] |
| 760 | sub_deps = Dependency(self, deps[d].module_name, sub_deps_base_url |
| 761 | ).ParseDepsFile(False) |
| 762 | url = deps[d].GetUrl(d, sub_deps_base_url, self.root_dir(), sub_deps) |
| 763 | (url, rev) = GetURLAndRev(d, url) |
[email protected] | de8f352 | 2010-03-11 23:47:44 | [diff] [blame] | 764 | entries[d] = "%s@%s" % (url, rev) |
[email protected] | e3da35f | 2010-03-09 21:40:45 | [diff] [blame] | 765 | |
[email protected] | de8f352 | 2010-03-11 23:47:44 | [diff] [blame] | 766 | # Build the snapshot configuration string |
| 767 | if self._options.snapshot: |
| 768 | url = entries.pop(name) |
[email protected] | 73e2114 | 2010-07-05 13:32:01 | [diff] [blame] | 769 | custom_deps = ''.join([' \"%s\": \"%s\",\n' % (x, entries[x]) |
| 770 | for x in sorted(entries.keys())]) |
[email protected] | e3da35f | 2010-03-09 21:40:45 | [diff] [blame] | 771 | |
[email protected] | 1f7d118 | 2010-05-17 18:17:38 | [diff] [blame] | 772 | new_gclient += self.DEFAULT_SNAPSHOT_SOLUTION_TEXT % { |
[email protected] | de8f352 | 2010-03-11 23:47:44 | [diff] [blame] | 773 | 'solution_name': name, |
| 774 | 'solution_url': url, |
[email protected] | 73e2114 | 2010-07-05 13:32:01 | [diff] [blame] | 775 | 'safesync_url' : '', |
[email protected] | de8f352 | 2010-03-11 23:47:44 | [diff] [blame] | 776 | 'solution_deps': custom_deps, |
| 777 | } |
| 778 | else: |
[email protected] | 73e2114 | 2010-07-05 13:32:01 | [diff] [blame] | 779 | print(';\n'.join(['%s: %s' % (x, entries[x]) |
[email protected] | de8f352 | 2010-03-11 23:47:44 | [diff] [blame] | 780 | for x in sorted(entries.keys())])) |
[email protected] | e3da35f | 2010-03-09 21:40:45 | [diff] [blame] | 781 | |
| 782 | # Print the snapshot configuration file |
| 783 | if self._options.snapshot: |
[email protected] | 491c04b | 2010-05-17 18:17:44 | [diff] [blame] | 784 | config = self.DEFAULT_SNAPSHOT_FILE_TEXT % {'solution_list': new_gclient} |
[email protected] | 75a5927 | 2010-06-11 22:34:03 | [diff] [blame] | 785 | snapclient = GClient(self.root_dir(), self._options) |
[email protected] | e3da35f | 2010-03-09 21:40:45 | [diff] [blame] | 786 | snapclient.SetConfig(config) |
[email protected] | 116704f | 2010-06-11 17:34:38 | [diff] [blame] | 787 | print(snapclient.config_content) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 788 | |
[email protected] | d36fba8 | 2010-06-28 16:50:40 | [diff] [blame] | 789 | def ParseDepsFile(self, direct_reference): |
| 790 | """No DEPS to parse for a .gclient file.""" |
| 791 | self.direct_reference = direct_reference |
| 792 | self.deps_parsed = True |
| 793 | |
[email protected] | 75a5927 | 2010-06-11 22:34:03 | [diff] [blame] | 794 | def root_dir(self): |
[email protected] | d36fba8 | 2010-06-28 16:50:40 | [diff] [blame] | 795 | """Root directory of gclient checkout.""" |
[email protected] | 75a5927 | 2010-06-11 22:34:03 | [diff] [blame] | 796 | return self._root_dir |
| 797 | |
[email protected] | 271375b | 2010-06-23 19:17:38 | [diff] [blame] | 798 | def enforced_os(self): |
[email protected] | d36fba8 | 2010-06-28 16:50:40 | [diff] [blame] | 799 | """What deps_os entries that are to be parsed.""" |
[email protected] | 271375b | 2010-06-23 19:17:38 | [diff] [blame] | 800 | return self._enforced_os |
| 801 | |
[email protected] | d36fba8 | 2010-06-28 16:50:40 | [diff] [blame] | 802 | def recursion_limit(self): |
| 803 | """How recursive can each dependencies in DEPS file can load DEPS file.""" |
| 804 | return self._recursion_limit |
| 805 | |
| 806 | def tree(self, force_all): |
| 807 | """Returns a flat list of all the dependencies.""" |
| 808 | def subtree(dep): |
| 809 | if not force_all and not dep.direct_reference: |
| 810 | # Was loaded from a From() keyword in a DEPS file, don't load all its |
| 811 | # dependencies. |
| 812 | return [] |
| 813 | result = dep.dependencies[:] |
| 814 | for d in dep.dependencies: |
| 815 | result.extend(subtree(d)) |
| 816 | return result |
| 817 | return subtree(self) |
| 818 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 819 | |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 820 | #### gclient commands. |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 821 | |
| 822 | |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 823 | def CMDcleanup(parser, args): |
| 824 | """Cleans up all working copies. |
[email protected] | ddff62d | 2010-05-17 21:02:36 | [diff] [blame] | 825 | |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 826 | Mostly svn-specific. Simply runs 'svn cleanup' for each module. |
[email protected] | 79692d6 | 2010-05-14 18:57:13 | [diff] [blame] | 827 | """ |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 828 | parser.add_option('--deps', dest='deps_os', metavar='OS_LIST', |
| 829 | help='override deps for the specified (comma-separated) ' |
| 830 | 'platform(s); \'all\' will process all deps_os ' |
| 831 | 'references') |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 832 | (options, args) = parser.parse_args(args) |
[email protected] | 2806acc | 2009-05-15 12:33:34 | [diff] [blame] | 833 | client = GClient.LoadCurrentConfig(options) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 834 | if not client: |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 835 | raise gclient_utils.Error('client not configured; see \'gclient config\'') |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 836 | if options.verbose: |
| 837 | # Print out the .gclient file. This is longer than if we just printed the |
| 838 | # client dict, but more legible, and it might contain helpful comments. |
[email protected] | 116704f | 2010-06-11 17:34:38 | [diff] [blame] | 839 | print(client.config_content) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 840 | return client.RunOnDeps('cleanup', args) |
| 841 | |
| 842 | |
[email protected] | 4b90e3a | 2010-07-01 20:28:26 | [diff] [blame] | 843 | @attr('usage', '[command] [args ...]') |
| 844 | def CMDrecurse(parser, args): |
| 845 | """Operates on all the entries. |
| 846 | |
| 847 | Runs a shell command on all entries. |
| 848 | """ |
| 849 | # Stop parsing at the first non-arg so that these go through to the command |
| 850 | parser.disable_interspersed_args() |
| 851 | parser.add_option('-s', '--scm', action='append', default=[], |
| 852 | help='choose scm types to operate upon') |
| 853 | options, args = parser.parse_args(args) |
| 854 | root, entries = gclient_utils.GetGClientRootAndEntries() |
| 855 | scm_set = set() |
| 856 | for scm in options.scm: |
| 857 | scm_set.update(scm.split(',')) |
| 858 | |
| 859 | # Pass in the SCM type as an env variable |
| 860 | env = os.environ.copy() |
| 861 | |
| 862 | for path, url in entries.iteritems(): |
| 863 | scm = gclient_scm.GetScmName(url) |
| 864 | if scm_set and scm not in scm_set: |
| 865 | continue |
| 866 | dir = os.path.normpath(os.path.join(root, path)) |
| 867 | env['GCLIENT_SCM'] = scm |
| 868 | env['GCLIENT_URL'] = url |
| 869 | subprocess.Popen(args, cwd=dir, env=env).communicate() |
| 870 | |
| 871 | |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 872 | @attr('usage', '[url] [safesync url]') |
| 873 | def CMDconfig(parser, args): |
[email protected] | ddff62d | 2010-05-17 21:02:36 | [diff] [blame] | 874 | """Create a .gclient file in the current directory. |
| 875 | |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 876 | This specifies the configuration for further commands. After update/sync, |
[email protected] | 79692d6 | 2010-05-14 18:57:13 | [diff] [blame] | 877 | top-level DEPS files in each module are read to determine dependent |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 878 | modules to operate on as well. If optional [url] parameter is |
[email protected] | 79692d6 | 2010-05-14 18:57:13 | [diff] [blame] | 879 | provided, then configuration is read from a specified Subversion server |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 880 | URL. |
[email protected] | 79692d6 | 2010-05-14 18:57:13 | [diff] [blame] | 881 | """ |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 882 | parser.add_option('--spec', |
| 883 | help='create a gclient file containing the provided ' |
| 884 | 'string. Due to Cygwin/Python brokenness, it ' |
| 885 | 'probably can\'t contain any newlines.') |
| 886 | parser.add_option('--name', |
| 887 | help='overrides the default name for the solution') |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 888 | (options, args) = parser.parse_args(args) |
[email protected] | 5fc2a33 | 2010-05-26 19:37:15 | [diff] [blame] | 889 | if ((options.spec and args) or len(args) > 2 or |
| 890 | (not options.spec and not args)): |
| 891 | parser.error('Inconsistent arguments. Use either --spec or one or 2 args') |
| 892 | |
[email protected] | 0329e67 | 2009-05-13 18:41:04 | [diff] [blame] | 893 | if os.path.exists(options.config_filename): |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 894 | raise gclient_utils.Error('%s file already exists in the current directory' |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 895 | % options.config_filename) |
[email protected] | 2806acc | 2009-05-15 12:33:34 | [diff] [blame] | 896 | client = GClient('.', options) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 897 | if options.spec: |
| 898 | client.SetConfig(options.spec) |
| 899 | else: |
[email protected] | 1ab7ffc | 2009-06-03 17:21:37 | [diff] [blame] | 900 | base_url = args[0].rstrip('/') |
[email protected] | 8cf7a39 | 2010-04-07 17:20:26 | [diff] [blame] | 901 | if not options.name: |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 902 | name = base_url.split('/')[-1] |
[email protected] | 8cf7a39 | 2010-04-07 17:20:26 | [diff] [blame] | 903 | else: |
| 904 | # specify an alternate relpath for the given URL. |
| 905 | name = options.name |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 906 | safesync_url = '' |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 907 | if len(args) > 1: |
| 908 | safesync_url = args[1] |
| 909 | client.SetDefaultConfig(name, base_url, safesync_url) |
| 910 | client.SaveConfig() |
[email protected] | 79692d6 | 2010-05-14 18:57:13 | [diff] [blame] | 911 | return 0 |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 912 | |
| 913 | |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 914 | def CMDexport(parser, args): |
[email protected] | ddff62d | 2010-05-17 21:02:36 | [diff] [blame] | 915 | """Wrapper for svn export for all managed directories.""" |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 916 | parser.add_option('--deps', dest='deps_os', metavar='OS_LIST', |
| 917 | help='override deps for the specified (comma-separated) ' |
| 918 | 'platform(s); \'all\' will process all deps_os ' |
| 919 | 'references') |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 920 | (options, args) = parser.parse_args(args) |
[email protected] | 644aa0c | 2009-07-17 20:20:41 | [diff] [blame] | 921 | if len(args) != 1: |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 922 | raise gclient_utils.Error('Need directory name') |
[email protected] | 644aa0c | 2009-07-17 20:20:41 | [diff] [blame] | 923 | client = GClient.LoadCurrentConfig(options) |
| 924 | |
| 925 | if not client: |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 926 | raise gclient_utils.Error('client not configured; see \'gclient config\'') |
[email protected] | 644aa0c | 2009-07-17 20:20:41 | [diff] [blame] | 927 | |
| 928 | if options.verbose: |
| 929 | # Print out the .gclient file. This is longer than if we just printed the |
| 930 | # client dict, but more legible, and it might contain helpful comments. |
[email protected] | 116704f | 2010-06-11 17:34:38 | [diff] [blame] | 931 | print(client.config_content) |
[email protected] | 644aa0c | 2009-07-17 20:20:41 | [diff] [blame] | 932 | return client.RunOnDeps('export', args) |
| 933 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 934 | |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 935 | @attr('epilog', """Example: |
| 936 | gclient pack > patch.txt |
| 937 | generate simple patch for configured client and dependences |
| 938 | """) |
| 939 | def CMDpack(parser, args): |
[email protected] | 79692d6 | 2010-05-14 18:57:13 | [diff] [blame] | 940 | """Generate a patch which can be applied at the root of the tree. |
[email protected] | ddff62d | 2010-05-17 21:02:36 | [diff] [blame] | 941 | |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 942 | Internally, runs 'svn diff'/'git diff' on each checked out module and |
[email protected] | 79692d6 | 2010-05-14 18:57:13 | [diff] [blame] | 943 | dependencies, and performs minimal postprocessing of the output. The |
| 944 | resulting patch is printed to stdout and can be applied to a freshly |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 945 | checked out tree via 'patch -p0 < patchfile'. |
[email protected] | 79692d6 | 2010-05-14 18:57:13 | [diff] [blame] | 946 | """ |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 947 | parser.add_option('--deps', dest='deps_os', metavar='OS_LIST', |
| 948 | help='override deps for the specified (comma-separated) ' |
| 949 | 'platform(s); \'all\' will process all deps_os ' |
| 950 | 'references') |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 951 | (options, args) = parser.parse_args(args) |
[email protected] | ab31859 | 2009-09-04 00:54:55 | [diff] [blame] | 952 | client = GClient.LoadCurrentConfig(options) |
| 953 | if not client: |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 954 | raise gclient_utils.Error('client not configured; see \'gclient config\'') |
[email protected] | ab31859 | 2009-09-04 00:54:55 | [diff] [blame] | 955 | if options.verbose: |
| 956 | # Print out the .gclient file. This is longer than if we just printed the |
| 957 | # client dict, but more legible, and it might contain helpful comments. |
[email protected] | 116704f | 2010-06-11 17:34:38 | [diff] [blame] | 958 | print(client.config_content) |
[email protected] | ab31859 | 2009-09-04 00:54:55 | [diff] [blame] | 959 | return client.RunOnDeps('pack', args) |
| 960 | |
| 961 | |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 962 | def CMDstatus(parser, args): |
| 963 | """Show modification status for every dependencies.""" |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 964 | parser.add_option('--deps', dest='deps_os', metavar='OS_LIST', |
| 965 | help='override deps for the specified (comma-separated) ' |
| 966 | 'platform(s); \'all\' will process all deps_os ' |
| 967 | 'references') |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 968 | (options, args) = parser.parse_args(args) |
[email protected] | 2806acc | 2009-05-15 12:33:34 | [diff] [blame] | 969 | client = GClient.LoadCurrentConfig(options) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 970 | if not client: |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 971 | raise gclient_utils.Error('client not configured; see \'gclient config\'') |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 972 | if options.verbose: |
| 973 | # Print out the .gclient file. This is longer than if we just printed the |
| 974 | # client dict, but more legible, and it might contain helpful comments. |
[email protected] | 116704f | 2010-06-11 17:34:38 | [diff] [blame] | 975 | print(client.config_content) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 976 | return client.RunOnDeps('status', args) |
| 977 | |
| 978 | |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 979 | @attr('epilog', """Examples: |
[email protected] | 79692d6 | 2010-05-14 18:57:13 | [diff] [blame] | 980 | gclient sync |
| 981 | update files from SCM according to current configuration, |
| 982 | *for modules which have changed since last update or sync* |
| 983 | gclient sync --force |
| 984 | update files from SCM according to current configuration, for |
| 985 | all modules (useful for recovering files deleted from local copy) |
| 986 | gclient sync --revision src@31000 |
| 987 | update src directory to r31000 |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 988 | """) |
| 989 | def CMDsync(parser, args): |
| 990 | """Checkout/update all modules.""" |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 991 | parser.add_option('-f', '--force', action='store_true', |
| 992 | help='force update even for unchanged modules') |
| 993 | parser.add_option('-n', '--nohooks', action='store_true', |
| 994 | help='don\'t run hooks after the update is complete') |
| 995 | parser.add_option('-r', '--revision', action='append', |
| 996 | dest='revisions', metavar='REV', default=[], |
| 997 | help='Enforces revision/hash for the solutions with the ' |
| 998 | 'format src@rev. The src@ part is optional and can be ' |
| 999 | 'skipped. -r can be used multiple times when .gclient ' |
| 1000 | 'has multiple solutions configured and will work even ' |
| 1001 | 'if the src@ part is skipped.') |
| 1002 | parser.add_option('-H', '--head', action='store_true', |
| 1003 | help='skips any safesync_urls specified in ' |
| 1004 | 'configured solutions and sync to head instead') |
| 1005 | parser.add_option('-D', '--delete_unversioned_trees', action='store_true', |
| 1006 | help='delete any unexpected unversioned trees ' |
| 1007 | 'that are in the checkout') |
| 1008 | parser.add_option('-R', '--reset', action='store_true', |
| 1009 | help='resets any local changes before updating (git only)') |
| 1010 | parser.add_option('--deps', dest='deps_os', metavar='OS_LIST', |
| 1011 | help='override deps for the specified (comma-separated) ' |
| 1012 | 'platform(s); \'all\' will process all deps_os ' |
| 1013 | 'references') |
| 1014 | parser.add_option('-m', '--manually_grab_svn_rev', action='store_true', |
| 1015 | help='Skip svn up whenever possible by requesting ' |
| 1016 | 'actual HEAD revision from the repository') |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 1017 | (options, args) = parser.parse_args(args) |
[email protected] | 2806acc | 2009-05-15 12:33:34 | [diff] [blame] | 1018 | client = GClient.LoadCurrentConfig(options) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1019 | |
| 1020 | if not client: |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 1021 | raise gclient_utils.Error('client not configured; see \'gclient config\'') |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1022 | |
[email protected] | 307d179 | 2010-05-31 20:03:13 | [diff] [blame] | 1023 | if options.revisions and options.head: |
| 1024 | # TODO(maruel): Make it a parser.error if it doesn't break any builder. |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 1025 | print('Warning: you cannot use both --head and --revision') |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1026 | |
| 1027 | if options.verbose: |
| 1028 | # Print out the .gclient file. This is longer than if we just printed the |
| 1029 | # client dict, but more legible, and it might contain helpful comments. |
[email protected] | 116704f | 2010-06-11 17:34:38 | [diff] [blame] | 1030 | print(client.config_content) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1031 | return client.RunOnDeps('update', args) |
| 1032 | |
| 1033 | |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 1034 | def CMDupdate(parser, args): |
[email protected] | ddff62d | 2010-05-17 21:02:36 | [diff] [blame] | 1035 | """Alias for the sync command. Deprecated.""" |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 1036 | return CMDsync(parser, args) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1037 | |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 1038 | def CMDdiff(parser, args): |
| 1039 | """Displays local diff for every dependencies.""" |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 1040 | parser.add_option('--deps', dest='deps_os', metavar='OS_LIST', |
| 1041 | help='override deps for the specified (comma-separated) ' |
| 1042 | 'platform(s); \'all\' will process all deps_os ' |
| 1043 | 'references') |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 1044 | (options, args) = parser.parse_args(args) |
[email protected] | 2806acc | 2009-05-15 12:33:34 | [diff] [blame] | 1045 | client = GClient.LoadCurrentConfig(options) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1046 | if not client: |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 1047 | raise gclient_utils.Error('client not configured; see \'gclient config\'') |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1048 | if options.verbose: |
| 1049 | # Print out the .gclient file. This is longer than if we just printed the |
| 1050 | # client dict, but more legible, and it might contain helpful comments. |
[email protected] | 116704f | 2010-06-11 17:34:38 | [diff] [blame] | 1051 | print(client.config_content) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1052 | return client.RunOnDeps('diff', args) |
| 1053 | |
| 1054 | |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 1055 | def CMDrevert(parser, args): |
| 1056 | """Revert all modifications in every dependencies.""" |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 1057 | parser.add_option('--deps', dest='deps_os', metavar='OS_LIST', |
| 1058 | help='override deps for the specified (comma-separated) ' |
| 1059 | 'platform(s); \'all\' will process all deps_os ' |
| 1060 | 'references') |
| 1061 | parser.add_option('-n', '--nohooks', action='store_true', |
| 1062 | help='don\'t run hooks after the revert is complete') |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 1063 | (options, args) = parser.parse_args(args) |
| 1064 | # --force is implied. |
| 1065 | options.force = True |
[email protected] | 2806acc | 2009-05-15 12:33:34 | [diff] [blame] | 1066 | client = GClient.LoadCurrentConfig(options) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1067 | if not client: |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 1068 | raise gclient_utils.Error('client not configured; see \'gclient config\'') |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1069 | return client.RunOnDeps('revert', args) |
| 1070 | |
| 1071 | |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 1072 | def CMDrunhooks(parser, args): |
| 1073 | """Runs hooks for files that have been modified in the local working copy.""" |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 1074 | parser.add_option('--deps', dest='deps_os', metavar='OS_LIST', |
| 1075 | help='override deps for the specified (comma-separated) ' |
| 1076 | 'platform(s); \'all\' will process all deps_os ' |
| 1077 | 'references') |
| 1078 | parser.add_option('-f', '--force', action='store_true', default=True, |
| 1079 | help='Deprecated. No effect.') |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 1080 | (options, args) = parser.parse_args(args) |
[email protected] | 2806acc | 2009-05-15 12:33:34 | [diff] [blame] | 1081 | client = GClient.LoadCurrentConfig(options) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1082 | if not client: |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 1083 | raise gclient_utils.Error('client not configured; see \'gclient config\'') |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1084 | if options.verbose: |
| 1085 | # Print out the .gclient file. This is longer than if we just printed the |
| 1086 | # client dict, but more legible, and it might contain helpful comments. |
[email protected] | 116704f | 2010-06-11 17:34:38 | [diff] [blame] | 1087 | print(client.config_content) |
[email protected] | 5df6a46 | 2009-08-28 18:52:26 | [diff] [blame] | 1088 | options.force = True |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 1089 | options.nohooks = False |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1090 | return client.RunOnDeps('runhooks', args) |
| 1091 | |
| 1092 | |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 1093 | def CMDrevinfo(parser, args): |
[email protected] | 9eda411 | 2010-06-11 18:56:10 | [diff] [blame] | 1094 | """Output revision info mapping for the client and its dependencies. |
| 1095 | |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 1096 | This allows the capture of an overall 'revision' for the source tree that |
[email protected] | 9eda411 | 2010-06-11 18:56:10 | [diff] [blame] | 1097 | can be used to reproduce the same tree in the future. It is only useful for |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 1098 | 'unpinned dependencies', i.e. DEPS/deps references without a svn revision |
| 1099 | number or a git hash. A git branch name isn't 'pinned' since the actual |
[email protected] | 9eda411 | 2010-06-11 18:56:10 | [diff] [blame] | 1100 | commit can change. |
| 1101 | """ |
| 1102 | parser.add_option('--deps', dest='deps_os', metavar='OS_LIST', |
| 1103 | help='override deps for the specified (comma-separated) ' |
| 1104 | 'platform(s); \'all\' will process all deps_os ' |
| 1105 | 'references') |
| 1106 | parser.add_option('-s', '--snapshot', action='store_true', |
| 1107 | help='creates a snapshot .gclient file of the current ' |
| 1108 | 'version of all repositories to reproduce the tree, ' |
| 1109 | 'implies -a') |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 1110 | (options, args) = parser.parse_args(args) |
[email protected] | 2806acc | 2009-05-15 12:33:34 | [diff] [blame] | 1111 | client = GClient.LoadCurrentConfig(options) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1112 | if not client: |
[email protected] | 0b6a084 | 2010-06-15 14:34:19 | [diff] [blame] | 1113 | raise gclient_utils.Error('client not configured; see \'gclient config\'') |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1114 | client.PrintRevInfo() |
[email protected] | 79692d6 | 2010-05-14 18:57:13 | [diff] [blame] | 1115 | return 0 |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1116 | |
| 1117 | |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 1118 | def Command(name): |
| 1119 | return getattr(sys.modules[__name__], 'CMD' + name, None) |
| 1120 | |
| 1121 | |
| 1122 | def CMDhelp(parser, args): |
| 1123 | """Prints list of commands or help for a specific command.""" |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 1124 | (_, args) = parser.parse_args(args) |
[email protected] | ddff62d | 2010-05-17 21:02:36 | [diff] [blame] | 1125 | if len(args) == 1: |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 1126 | return Main(args + ['--help']) |
[email protected] | ddff62d | 2010-05-17 21:02:36 | [diff] [blame] | 1127 | parser.print_help() |
| 1128 | return 0 |
| 1129 | |
| 1130 | |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 1131 | def GenUsage(parser, command): |
| 1132 | """Modify an OptParse object with the function's documentation.""" |
| 1133 | obj = Command(command) |
| 1134 | if command == 'help': |
| 1135 | command = '<command>' |
| 1136 | # OptParser.description prefer nicely non-formatted strings. |
| 1137 | parser.description = re.sub('[\r\n ]{2,}', ' ', obj.__doc__) |
| 1138 | usage = getattr(obj, 'usage', '') |
| 1139 | parser.set_usage('%%prog %s [options] %s' % (command, usage)) |
| 1140 | parser.epilog = getattr(obj, 'epilog', None) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1141 | |
| 1142 | |
| 1143 | def Main(argv): |
[email protected] | 5ca2769 | 2010-05-26 19:32:41 | [diff] [blame] | 1144 | """Doesn't parse the arguments here, just find the right subcommand to |
| 1145 | execute.""" |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 1146 | try: |
| 1147 | # Do it late so all commands are listed. |
| 1148 | CMDhelp.usage = ('\n\nCommands are:\n' + '\n'.join([ |
| 1149 | ' %-10s %s' % (fn[3:], Command(fn[3:]).__doc__.split('\n')[0].strip()) |
| 1150 | for fn in dir(sys.modules[__name__]) if fn.startswith('CMD')])) |
| 1151 | parser = optparse.OptionParser(version='%prog ' + __version__) |
[email protected] | f0fc991 | 2010-06-11 17:57:33 | [diff] [blame] | 1152 | parser.add_option('-v', '--verbose', action='count', default=0, |
| 1153 | help='Produces additional output for diagnostics. Can be ' |
| 1154 | 'used up to three times for more logging info.') |
| 1155 | parser.add_option('--gclientfile', dest='config_filename', |
| 1156 | default=os.environ.get('GCLIENT_FILE', '.gclient'), |
| 1157 | help='Specify an alternate %default file') |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 1158 | # Integrate standard options processing. |
| 1159 | old_parser = parser.parse_args |
| 1160 | def Parse(args): |
| 1161 | (options, args) = old_parser(args) |
[email protected] | f0fc991 | 2010-06-11 17:57:33 | [diff] [blame] | 1162 | level = None |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 1163 | if options.verbose == 2: |
[email protected] | f0fc991 | 2010-06-11 17:57:33 | [diff] [blame] | 1164 | level = logging.INFO |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 1165 | elif options.verbose > 2: |
[email protected] | f0fc991 | 2010-06-11 17:57:33 | [diff] [blame] | 1166 | level = logging.DEBUG |
| 1167 | logging.basicConfig(level=level, |
| 1168 | format='%(module)s(%(lineno)d) %(funcName)s:%(message)s') |
| 1169 | options.entries_filename = options.config_filename + '_entries' |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 1170 | if not hasattr(options, 'revisions'): |
| 1171 | # GClient.RunOnDeps expects it even if not applicable. |
| 1172 | options.revisions = [] |
| 1173 | if not hasattr(options, 'head'): |
| 1174 | options.head = None |
[email protected] | f0fc991 | 2010-06-11 17:57:33 | [diff] [blame] | 1175 | if not hasattr(options, 'nohooks'): |
| 1176 | options.nohooks = True |
| 1177 | if not hasattr(options, 'deps_os'): |
| 1178 | options.deps_os = None |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 1179 | return (options, args) |
| 1180 | parser.parse_args = Parse |
| 1181 | # We don't want wordwrapping in epilog (usually examples) |
| 1182 | parser.format_epilog = lambda _: parser.epilog or '' |
| 1183 | if argv: |
| 1184 | command = Command(argv[0]) |
| 1185 | if command: |
[email protected] | f0fc991 | 2010-06-11 17:57:33 | [diff] [blame] | 1186 | # 'fix' the usage and the description now that we know the subcommand. |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 1187 | GenUsage(parser, argv[0]) |
| 1188 | return command(parser, argv[1:]) |
| 1189 | # Not a known command. Default to help. |
| 1190 | GenUsage(parser, 'help') |
| 1191 | return CMDhelp(parser, argv) |
| 1192 | except gclient_utils.Error, e: |
[email protected] | f0fc991 | 2010-06-11 17:57:33 | [diff] [blame] | 1193 | print >> sys.stderr, 'Error: %s' % str(e) |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 1194 | return 1 |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1195 | |
| 1196 | |
[email protected] | f0fc991 | 2010-06-11 17:57:33 | [diff] [blame] | 1197 | if '__main__' == __name__: |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 1198 | sys.exit(Main(sys.argv[1:])) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1199 | |
| 1200 | # vim: ts=2:sw=2:tw=80:et: |