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