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