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