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