Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
|
binji
2014/11/13 23:57:02
I'd prefer doing the sdk_updater as a separate CL.
Sam Clegg
2014/11/30 17:55:11
Done.
| |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 # CMD code copied from git_cl.py in depot_tools. | 6 # CMD code copied from git_cl.py in depot_tools. |
| 7 | 7 |
| 8 import config | 8 import config |
| 9 import cStringIO | 9 import cStringIO |
| 10 import download | 10 import download |
| 11 import logging | 11 import logging |
| 12 import optparse | 12 import argparse |
| 13 import os | 13 import os |
| 14 import re | 14 import re |
| 15 import sdk_update_common | 15 import sdk_update_common |
| 16 from sdk_update_common import Error | 16 from sdk_update_common import Error |
| 17 import sys | 17 import sys |
| 18 import urllib2 | 18 import urllib2 |
| 19 | 19 |
| 20 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) | 20 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 21 PARENT_DIR = os.path.dirname(SCRIPT_DIR) | 21 PARENT_DIR = os.path.dirname(SCRIPT_DIR) |
| 22 | 22 |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 165 manifest.MergeManifest(LoadRemoteManifest(source)) | 165 manifest.MergeManifest(LoadRemoteManifest(source)) |
| 166 return manifest | 166 return manifest |
| 167 | 167 |
| 168 | 168 |
| 169 # Commands ##################################################################### | 169 # Commands ##################################################################### |
| 170 | 170 |
| 171 | 171 |
| 172 @usage('<bundle names...>') | 172 @usage('<bundle names...>') |
| 173 def CMDinfo(parser, args): | 173 def CMDinfo(parser, args): |
| 174 """display information about a bundle""" | 174 """display information about a bundle""" |
| 175 options, args = parser.parse_args(args) | 175 parser.add_argument('bundles', nargs='+') |
| 176 if not args: | 176 options = parser.parse_args(args) |
| 177 parser.error('No bundles given') | |
| 178 return 0 | |
| 179 cfg = LoadConfig() | 177 cfg = LoadConfig() |
| 180 remote_manifest = LoadCombinedRemoteManifest(options.manifest_url, cfg) | 178 remote_manifest = LoadCombinedRemoteManifest(options.manifest_url, cfg) |
| 181 command.info.Info(remote_manifest, args) | 179 command.info.Info(remote_manifest, options.bundles) |
| 182 return 0 | 180 return 0 |
| 183 | 181 |
| 184 | 182 |
| 185 def CMDlist(parser, args): | 183 def CMDlist(parser, args): |
| 186 """list all available bundles""" | 184 """list all available bundles""" |
| 187 parser.add_option('-r', '--revision', action='store_true', | 185 parser.add_argument('-r', '--revision', action='store_true', |
| 188 help='display revision numbers') | 186 help='display revision numbers') |
| 189 options, args = parser.parse_args(args) | 187 options = parser.parse_args(args) |
| 190 if args: | |
| 191 parser.error('Unsupported argument(s): %s' % ', '.join(args)) | |
| 192 local_manifest = LoadLocalManifest() | 188 local_manifest = LoadLocalManifest() |
| 193 cfg = LoadConfig() | 189 cfg = LoadConfig() |
| 194 remote_manifest = LoadCombinedRemoteManifest(options.manifest_url, cfg) | 190 remote_manifest = LoadCombinedRemoteManifest(options.manifest_url, cfg) |
| 195 command.list.List(remote_manifest, local_manifest, options.revision) | 191 command.list.List(remote_manifest, local_manifest, options.revision) |
| 196 return 0 | 192 return 0 |
| 197 | 193 |
| 198 | 194 |
| 199 @usage('<bundle names...>') | 195 @usage('<bundle names...>') |
| 200 def CMDupdate(parser, args): | 196 def CMDupdate(parser, args): |
| 201 """update a bundle in the SDK to the latest version""" | 197 """update a bundle in the SDK to the latest version""" |
| 202 parser.add_option('-F', '--force', action='store_true', | 198 parser.add_argument('-F', '--force', action='store_true', |
| 203 help='Force updating bundles that already exist. The bundle will not be ' | 199 help='Force updating bundles that already exist. The bundle will not be ' |
| 204 'updated if the local revision matches the remote revision.') | 200 'updated if the local revision matches the remote revision.') |
| 205 options, args = parser.parse_args(args) | 201 parser.add_argument('bundles', nargs='*', |
| 202 help='bundles to update', | |
| 203 default=[command.update.RECOMMENDED]) | |
| 204 options = parser.parse_args(args) | |
| 206 local_manifest = LoadLocalManifest() | 205 local_manifest = LoadLocalManifest() |
| 207 cfg = LoadConfig() | 206 cfg = LoadConfig() |
| 208 remote_manifest = LoadCombinedRemoteManifest(options.manifest_url, cfg) | 207 remote_manifest = LoadCombinedRemoteManifest(options.manifest_url, cfg) |
| 209 | 208 |
| 210 if not args: | |
| 211 args = [command.update.RECOMMENDED] | |
| 212 | |
| 213 try: | 209 try: |
| 214 delegate = command.update.RealUpdateDelegate(USER_DATA_DIR, | 210 delegate = command.update.RealUpdateDelegate(USER_DATA_DIR, |
| 215 DEFAULT_SDK_ROOT, cfg) | 211 DEFAULT_SDK_ROOT, cfg) |
| 216 command.update.Update(delegate, remote_manifest, local_manifest, args, | 212 command.update.Update(delegate, remote_manifest, local_manifest, |
| 217 options.force) | 213 options.bundles, options.force) |
| 218 finally: | 214 finally: |
| 219 # Always write out the local manifest, we may have successfully updated one | 215 # Always write out the local manifest, we may have successfully updated one |
| 220 # or more bundles before failing. | 216 # or more bundles before failing. |
| 221 try: | 217 try: |
| 222 WriteLocalManifest(local_manifest) | 218 WriteLocalManifest(local_manifest) |
| 223 except Error as e: | 219 except Error as e: |
| 224 # Log the error writing to the manifest, but propagate the original | 220 # Log the error writing to the manifest, but propagate the original |
| 225 # exception. | 221 # exception. |
| 226 logging.error(str(e)) | 222 logging.error(str(e)) |
| 227 | 223 |
| 228 return 0 | 224 return 0 |
| 229 | 225 |
| 230 | 226 |
| 231 def CMDinstall(parser, args): | 227 def CMDinstall(parser, args): |
| 232 """install a bundle in the SDK""" | 228 """install a bundle in the SDK""" |
| 233 # For now, forward to CMDupdate. We may want different behavior for this | 229 # For now, forward to CMDupdate. We may want different behavior for this |
| 234 # in the future, though... | 230 # in the future, though... |
| 235 return CMDupdate(parser, args) | 231 return CMDupdate(parser, args) |
| 236 | 232 |
| 237 | 233 |
| 238 @usage('<bundle names...>') | 234 @usage('<bundle names...>') |
| 239 def CMDuninstall(parser, args): | 235 def CMDuninstall(parser, args): |
| 240 """uninstall the given bundles""" | 236 """uninstall the given bundles""" |
| 241 _, args = parser.parse_args(args) | 237 parser.add_argument('bundles', nargs='+', help='bundles to uninstall') |
| 242 if not args: | 238 options = parser.parse_args(args) |
| 243 parser.error('No bundles given') | |
| 244 return 0 | |
| 245 local_manifest = LoadLocalManifest() | 239 local_manifest = LoadLocalManifest() |
| 246 command.uninstall.Uninstall(DEFAULT_SDK_ROOT, local_manifest, args) | 240 command.uninstall.Uninstall(DEFAULT_SDK_ROOT, local_manifest, options.bundles) |
| 247 WriteLocalManifest(local_manifest) | 241 WriteLocalManifest(local_manifest) |
| 248 return 0 | 242 return 0 |
| 249 | 243 |
| 250 | 244 |
| 251 @usage('<bundle names...>') | 245 @usage('<bundle names...>') |
| 252 def CMDreinstall(parser, args): | 246 def CMDreinstall(parser, args): |
| 253 """restore the given bundles to their original state | 247 """restore the given bundles to their original state |
| 254 | 248 |
| 255 Note that if there is an update to a given bundle, reinstall will not | 249 Note that if there is an update to a given bundle, reinstall will not |
| 256 automatically update to the newest version. | 250 automatically update to the newest version. |
| 257 """ | 251 """ |
| 258 _, args = parser.parse_args(args) | 252 parser.add_argument('bundles', nargs='+') |
| 253 options = parser.parse_args(args) | |
| 259 local_manifest = LoadLocalManifest() | 254 local_manifest = LoadLocalManifest() |
| 260 | 255 |
| 261 if not args: | |
| 262 parser.error('No bundles given') | |
| 263 return 0 | |
| 264 | |
| 265 cfg = LoadConfig() | 256 cfg = LoadConfig() |
| 266 try: | 257 try: |
| 267 delegate = command.update.RealUpdateDelegate(USER_DATA_DIR, | 258 delegate = command.update.RealUpdateDelegate(USER_DATA_DIR, |
| 268 DEFAULT_SDK_ROOT, cfg) | 259 DEFAULT_SDK_ROOT, cfg) |
| 269 command.update.Reinstall(delegate, local_manifest, args) | 260 command.update.Reinstall(delegate, local_manifest, options.bundles) |
| 270 finally: | 261 finally: |
| 271 # Always write out the local manifest, we may have successfully updated one | 262 # Always write out the local manifest, we may have successfully updated one |
| 272 # or more bundles before failing. | 263 # or more bundles before failing. |
| 273 try: | 264 try: |
| 274 WriteLocalManifest(local_manifest) | 265 WriteLocalManifest(local_manifest) |
| 275 except Error as e: | 266 except Error as e: |
| 276 # Log the error writing to the manifest, but propagate the original | 267 # Log the error writing to the manifest, but propagate the original |
| 277 # exception. | 268 # exception. |
| 278 logging.error(str(e)) | 269 logging.error(str(e)) |
| 279 | 270 |
| 280 return 0 | 271 return 0 |
| 281 | 272 |
| 282 | 273 |
| 283 def CMDsources(parser, args): | 274 def CMDsources(parser, args): |
| 284 """manage external package sources""" | 275 """manage external package sources""" |
| 285 parser.add_option('-a', '--add', dest='url_to_add', | 276 parser.add_argument('-a', '--add', dest='url_to_add', |
| 286 help='Add an additional package source') | 277 help='Add an additional package source') |
| 287 parser.add_option( | 278 parser.add_argument( |
| 288 '-r', '--remove', dest='url_to_remove', | 279 '-r', '--remove', dest='url_to_remove', |
| 289 help='Remove package source (use \'all\' for all additional sources)') | 280 help='Remove package source (use \'all\' for all additional sources)') |
| 290 parser.add_option('-l', '--list', dest='do_list', action='store_true', | 281 parser.add_argument('-l', '--list', dest='do_list', action='store_true', |
| 291 help='List additional package sources') | 282 help='List additional package sources') |
| 292 options, args = parser.parse_args(args) | 283 options = parser.parse_args(args) |
| 293 | 284 |
| 294 cfg = LoadConfig(True) | 285 cfg = LoadConfig(True) |
| 295 write_config = False | 286 write_config = False |
| 296 if options.url_to_add: | 287 if options.url_to_add: |
| 297 command.sources.AddSource(cfg, options.url_to_add) | 288 command.sources.AddSource(cfg, options.url_to_add) |
| 298 write_config = True | 289 write_config = True |
| 299 elif options.url_to_remove: | 290 elif options.url_to_remove: |
| 300 command.sources.RemoveSource(cfg, options.url_to_remove) | 291 command.sources.RemoveSource(cfg, options.url_to_remove) |
| 301 write_config = True | 292 write_config = True |
| 302 elif options.do_list: | 293 elif options.do_list: |
| 303 command.sources.ListSources(cfg) | 294 command.sources.ListSources(cfg) |
| 304 else: | 295 else: |
| 305 parser.print_help() | 296 parser.print_help() |
| 306 | 297 |
| 307 if write_config: | 298 if write_config: |
| 308 WriteConfig(cfg) | 299 WriteConfig(cfg) |
| 309 | 300 |
| 310 return 0 | 301 return 0 |
| 311 | 302 |
| 312 | 303 |
| 313 def CMDversion(parser, args): | 304 def CMDversion(parser, args): |
| 314 """display version information""" | 305 """display version information""" |
| 315 _, _ = parser.parse_args(args) | 306 parser.parse_args(args) |
| 316 print "Native Client SDK Updater, version r%s" % REVISION | 307 print "Native Client SDK Updater, version r%s" % REVISION |
| 317 return 0 | 308 return 0 |
| 318 | 309 |
| 319 | 310 |
| 320 def CMDhelp(parser, args): | 311 def CMDhelp(parser, args): |
| 321 """print list of commands or help for a specific command""" | 312 """print list of commands or help for a specific command""" |
| 322 _, args = parser.parse_args(args) | 313 parser.add_argument('command', nargs='?', help=argparse.SUPPRESS) |
| 323 if len(args) == 1: | 314 options = parser.parse_args(args) |
| 324 return main(args + ['--help']) | 315 if options.command: |
| 316 return main(options.command + ['--help']) | |
| 325 parser.print_help() | 317 parser.print_help() |
| 326 return 0 | 318 return 0 |
| 327 | 319 |
| 328 | 320 |
| 329 def Command(name): | 321 def Command(name): |
| 330 return globals().get('CMD' + name, None) | 322 return globals().get('CMD' + name, None) |
| 331 | 323 |
| 332 | 324 |
| 333 def GenUsage(parser, cmd): | 325 def GenUsage(parser, cmd): |
| 334 """Modify an OptParse object with the function's documentation.""" | 326 """Modify an OptParse object with the function's documentation.""" |
| 335 obj = Command(cmd) | 327 obj = Command(cmd) |
| 336 more = getattr(obj, 'usage_more', '') | 328 more = getattr(obj, 'usage_more', '') |
| 337 if cmd == 'help': | 329 if cmd == 'help': |
| 338 cmd = '<command>' | 330 cmd = '<command>' |
| 339 else: | 331 else: |
| 340 # OptParser.description prefer nicely non-formatted strings. | 332 # OptParser.description prefer nicely non-formatted strings. |
| 341 parser.description = re.sub('[\r\n ]{2,}', ' ', obj.__doc__) | 333 parser.description = re.sub('[\r\n ]{2,}', ' ', obj.__doc__) |
| 342 parser.set_usage('usage: %%prog %s [options] %s' % (cmd, more)) | 334 parser.usage = '%%(prog)s %s [options] %s' % (cmd, more) |
| 343 | 335 |
| 344 | 336 |
| 345 def UpdateSDKTools(options, args): | 337 def UpdateSDKTools(options, args): |
| 346 """update the sdk_tools bundle""" | 338 """update the sdk_tools bundle""" |
| 347 | 339 |
| 348 local_manifest = LoadLocalManifest() | 340 local_manifest = LoadLocalManifest() |
| 349 cfg = LoadConfig() | 341 cfg = LoadConfig() |
| 350 remote_manifest = LoadCombinedRemoteManifest(options.manifest_url, cfg) | 342 remote_manifest = LoadCombinedRemoteManifest(options.manifest_url, cfg) |
| 351 | 343 |
| 352 try: | 344 try: |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 369 # Get all commands... | 361 # Get all commands... |
| 370 cmds = [fn[3:] for fn in dir(sys.modules[__name__]) if fn.startswith('CMD')] | 362 cmds = [fn[3:] for fn in dir(sys.modules[__name__]) if fn.startswith('CMD')] |
| 371 # Remove hidden commands... | 363 # Remove hidden commands... |
| 372 cmds = filter(lambda fn: not getattr(Command(fn), 'hide', 0), cmds) | 364 cmds = filter(lambda fn: not getattr(Command(fn), 'hide', 0), cmds) |
| 373 # Format for CMDhelp usage. | 365 # Format for CMDhelp usage. |
| 374 CMDhelp.usage_more = ('\n\nCommands are:\n' + '\n'.join([ | 366 CMDhelp.usage_more = ('\n\nCommands are:\n' + '\n'.join([ |
| 375 ' %-10s %s' % (fn, Command(fn).__doc__.split('\n')[0].strip()) | 367 ' %-10s %s' % (fn, Command(fn).__doc__.split('\n')[0].strip()) |
| 376 for fn in cmds])) | 368 for fn in cmds])) |
| 377 | 369 |
| 378 # Create the option parse and add --verbose support. | 370 # Create the option parse and add --verbose support. |
| 379 parser = optparse.OptionParser() | 371 parser = argparse.ArgumentParser(description=__doc__) |
| 380 parser.add_option( | 372 parser.add_argument( |
| 381 '-v', '--verbose', action='count', default=0, | 373 '-v', '--verbose', action='count', default=0, |
| 382 help='Use 2 times for more debugging info') | 374 help='Use 2 times for more debugging info') |
| 383 parser.add_option('-U', '--manifest-url', dest='manifest_url', | 375 parser.add_argument('-U', '--manifest-url', dest='manifest_url', |
| 384 default=GSTORE_URL + '/nacl/nacl_sdk/' + MANIFEST_FILENAME, | 376 default=GSTORE_URL + '/nacl/nacl_sdk/' + MANIFEST_FILENAME, |
| 385 metavar='URL', help='override the default URL for the NaCl manifest file') | 377 metavar='URL', help='override the default URL for the NaCl manifest file') |
| 386 parser.add_option('--update-sdk-tools', action='store_true', | 378 parser.add_argument('--update-sdk-tools', action='store_true', |
| 387 dest='update_sdk_tools', help=optparse.SUPPRESS_HELP) | 379 dest='update_sdk_tools', help=argparse.SUPPRESS) |
| 388 | 380 |
| 389 old_parser_args = parser.parse_args | 381 old_parser_args = parser.parse_args |
| 390 def Parse(args): | 382 def Parse(args): |
| 391 options, args = old_parser_args(args) | 383 options = old_parser_args(args) |
| 392 if options.verbose >= 2: | 384 if options.verbose >= 2: |
| 393 loglevel = logging.DEBUG | 385 loglevel = logging.DEBUG |
| 394 elif options.verbose: | 386 elif options.verbose: |
| 395 loglevel = logging.INFO | 387 loglevel = logging.INFO |
| 396 else: | 388 else: |
| 397 loglevel = logging.WARNING | 389 loglevel = logging.WARNING |
| 398 | 390 |
| 399 fmt = '%(levelname)s:%(message)s' | 391 logging.basicConfig(stream=sys.stdout, level=loglevel, |
| 400 logging.basicConfig(stream=sys.stdout, level=loglevel, format=fmt) | 392 format='%(levelname)s:%(message)s') |
| 401 | 393 |
| 402 # If --update-sdk-tools is passed, circumvent any other command running. | 394 # If --update-sdk-tools is passed, circumvent any other command running. |
| 403 if options.update_sdk_tools: | 395 if options.update_sdk_tools: |
| 404 UpdateSDKTools(options, args) | 396 UpdateSDKTools(options, args) |
| 405 sys.exit(1) | 397 sys.exit(1) |
| 406 | 398 |
| 407 return options, args | 399 return options |
| 408 parser.parse_args = Parse | 400 parser.parse_args = Parse |
| 409 | 401 |
| 410 if argv: | 402 if argv: |
| 411 cmd = Command(argv[0]) | 403 cmd = Command(argv[0]) |
| 412 if cmd: | 404 if cmd: |
| 413 # "fix" the usage and the description now that we know the subcommand. | 405 # "fix" the usage and the description now that we know the subcommand. |
| 414 GenUsage(parser, argv[0]) | 406 GenUsage(parser, argv[0]) |
| 415 return cmd(parser, argv[1:]) | 407 return cmd(parser, argv[1:]) |
| 416 | 408 |
| 417 # Not a known command. Default to help. | 409 # Not a known command. Default to help. |
| 418 GenUsage(parser, 'help') | 410 GenUsage(parser, 'help') |
| 419 return CMDhelp(parser, argv) | 411 return CMDhelp(parser, argv) |
| 420 | 412 |
| 421 | 413 |
| 422 if __name__ == '__main__': | 414 if __name__ == '__main__': |
| 423 try: | 415 try: |
| 424 sys.exit(main(sys.argv[1:])) | 416 sys.exit(main(sys.argv[1:])) |
| 425 except Error as e: | 417 except Error as e: |
| 426 logging.error(str(e)) | 418 logging.error(str(e)) |
| 427 sys.exit(1) | 419 sys.exit(1) |
| 428 except KeyboardInterrupt: | 420 except KeyboardInterrupt: |
| 429 sys.stderr.write('naclsdk: interrupted\n') | 421 sys.stderr.write('naclsdk: interrupted\n') |
| 430 sys.exit(1) | 422 sys.exit(1) |
| OLD | NEW |