Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Side by Side Diff: gclient.py

Issue 7766005: Move the parser creation code in its own function to enable unit testing. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2011 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 """Meta checkout manager supporting both Subversion and GIT. 6 """Meta checkout manager supporting both Subversion and GIT.
7 7
8 Files 8 Files
9 .gclient : Current client configuration, written by 'config' command. 9 .gclient : Current client configuration, written by 'config' command.
10 Format is a Python script defining 'solutions', a list whose 10 Format is a Python script defining 'solutions', a list whose
(...skipping 1211 matching lines...) Expand 10 before | Expand all | Expand 10 after
1222 obj = Command(command) 1222 obj = Command(command)
1223 if command == 'help': 1223 if command == 'help':
1224 command = '<command>' 1224 command = '<command>'
1225 # OptParser.description prefer nicely non-formatted strings. 1225 # OptParser.description prefer nicely non-formatted strings.
1226 parser.description = re.sub('[\r\n ]{2,}', ' ', obj.__doc__) 1226 parser.description = re.sub('[\r\n ]{2,}', ' ', obj.__doc__)
1227 usage = getattr(obj, 'usage', '') 1227 usage = getattr(obj, 'usage', '')
1228 parser.set_usage('%%prog %s [options] %s' % (command, usage)) 1228 parser.set_usage('%%prog %s [options] %s' % (command, usage))
1229 parser.epilog = getattr(obj, 'epilog', None) 1229 parser.epilog = getattr(obj, 'epilog', None)
1230 1230
1231 1231
1232 def Parser():
1233 """Returns the default parser."""
1234 parser = optparse.OptionParser(version='%prog ' + __version__)
1235 parser.add_option('-j', '--jobs', default=1, type='int',
1236 help='Specify how many SCM commands can run in parallel; '
1237 'default=%default')
1238 parser.add_option('-v', '--verbose', action='count', default=0,
1239 help='Produces additional output for diagnostics. Can be '
1240 'used up to three times for more logging info.')
1241 parser.add_option('--gclientfile', dest='config_filename',
1242 default=os.environ.get('GCLIENT_FILE', '.gclient'),
1243 help='Specify an alternate %default file')
1244 # Integrate standard options processing.
1245 old_parser = parser.parse_args
1246 def Parse(args):
1247 (options, args) = old_parser(args)
1248 level = None
1249 if options.verbose == 2:
1250 level = logging.INFO
1251 elif options.verbose > 2:
1252 level = logging.DEBUG
1253 logging.basicConfig(level=level,
1254 format='%(module)s(%(lineno)d) %(funcName)s:%(message)s')
1255 options.entries_filename = options.config_filename + '_entries'
1256 if options.jobs < 1:
1257 parser.error('--jobs must be 1 or higher')
1258
1259 # These hacks need to die.
1260 if not hasattr(options, 'revisions'):
1261 # GClient.RunOnDeps expects it even if not applicable.
1262 options.revisions = []
1263 if not hasattr(options, 'head'):
1264 options.head = None
1265 if not hasattr(options, 'nohooks'):
1266 options.nohooks = True
1267 if not hasattr(options, 'deps_os'):
1268 options.deps_os = None
1269 if not hasattr(options, 'manually_grab_svn_rev'):
1270 options.manually_grab_svn_rev = None
1271 if not hasattr(options, 'force'):
1272 options.force = None
1273 return (options, args)
1274 parser.parse_args = Parse
1275 # We don't want wordwrapping in epilog (usually examples)
1276 parser.format_epilog = lambda _: parser.epilog or ''
1277 return parser
1278
1279
1232 def Main(argv): 1280 def Main(argv):
1233 """Doesn't parse the arguments here, just find the right subcommand to 1281 """Doesn't parse the arguments here, just find the right subcommand to
1234 execute.""" 1282 execute."""
1235 if sys.hexversion < 0x02050000: 1283 if sys.hexversion < 0x02050000:
1236 print >> sys.stderr, ( 1284 print >> sys.stderr, (
1237 '\nYour python version is unsupported, please upgrade.\n') 1285 '\nYour python version is unsupported, please upgrade.\n')
1238 try: 1286 try:
1239 # Make stdout auto-flush so buildbot doesn't kill us during lengthy 1287 # Make stdout auto-flush so buildbot doesn't kill us during lengthy
1240 # operations. Python as a strong tendency to buffer sys.stdout. 1288 # operations. Python as a strong tendency to buffer sys.stdout.
1241 sys.stdout = gclient_utils.MakeFileAutoFlush(sys.stdout) 1289 sys.stdout = gclient_utils.MakeFileAutoFlush(sys.stdout)
1242 # Make stdout annotated with the thread ids. 1290 # Make stdout annotated with the thread ids.
1243 sys.stdout = gclient_utils.MakeFileAnnotated(sys.stdout) 1291 sys.stdout = gclient_utils.MakeFileAnnotated(sys.stdout)
1244 # Do it late so all commands are listed. 1292 # Do it late so all commands are listed.
1245 # Unused variable 'usage' 1293 # Unused variable 'usage'
1246 # pylint: disable=W0612 1294 # pylint: disable=W0612
1247 CMDhelp.usage = ('\n\nCommands are:\n' + '\n'.join([ 1295 CMDhelp.usage = ('\n\nCommands are:\n' + '\n'.join([
1248 ' %-10s %s' % (fn[3:], Command(fn[3:]).__doc__.split('\n')[0].strip()) 1296 ' %-10s %s' % (fn[3:], Command(fn[3:]).__doc__.split('\n')[0].strip())
1249 for fn in dir(sys.modules[__name__]) if fn.startswith('CMD')])) 1297 for fn in dir(sys.modules[__name__]) if fn.startswith('CMD')]))
1250 parser = optparse.OptionParser(version='%prog ' + __version__) 1298 parser = Parser()
1251 parser.add_option('-j', '--jobs', default=1, type='int',
1252 help='Specify how many SCM commands can run in parallel; '
1253 'default=%default')
1254 parser.add_option('-v', '--verbose', action='count', default=0,
1255 help='Produces additional output for diagnostics. Can be '
1256 'used up to three times for more logging info.')
1257 parser.add_option('--gclientfile', dest='config_filename',
1258 default=os.environ.get('GCLIENT_FILE', '.gclient'),
1259 help='Specify an alternate %default file')
1260 # Integrate standard options processing.
1261 old_parser = parser.parse_args
1262 def Parse(args):
1263 (options, args) = old_parser(args)
1264 level = None
1265 if options.verbose == 2:
1266 level = logging.INFO
1267 elif options.verbose > 2:
1268 level = logging.DEBUG
1269 logging.basicConfig(level=level,
1270 format='%(module)s(%(lineno)d) %(funcName)s:%(message)s')
1271 options.entries_filename = options.config_filename + '_entries'
1272 if options.jobs < 1:
1273 parser.error('--jobs must be 1 or higher')
1274
1275 # These hacks need to die.
1276 if not hasattr(options, 'revisions'):
1277 # GClient.RunOnDeps expects it even if not applicable.
1278 options.revisions = []
1279 if not hasattr(options, 'head'):
1280 options.head = None
1281 if not hasattr(options, 'nohooks'):
1282 options.nohooks = True
1283 if not hasattr(options, 'deps_os'):
1284 options.deps_os = None
1285 if not hasattr(options, 'manually_grab_svn_rev'):
1286 options.manually_grab_svn_rev = None
1287 if not hasattr(options, 'force'):
1288 options.force = None
1289 return (options, args)
1290 parser.parse_args = Parse
1291 # We don't want wordwrapping in epilog (usually examples)
1292 parser.format_epilog = lambda _: parser.epilog or ''
1293 if argv: 1299 if argv:
1294 command = Command(argv[0]) 1300 command = Command(argv[0])
1295 if command: 1301 if command:
1296 # 'fix' the usage and the description now that we know the subcommand. 1302 # 'fix' the usage and the description now that we know the subcommand.
1297 GenUsage(parser, argv[0]) 1303 GenUsage(parser, argv[0])
1298 return command(parser, argv[1:]) 1304 return command(parser, argv[1:])
1299 # Not a known command. Default to help. 1305 # Not a known command. Default to help.
1300 GenUsage(parser, 'help') 1306 GenUsage(parser, 'help')
1301 return CMDhelp(parser, argv) 1307 return CMDhelp(parser, argv)
1302 except (gclient_utils.Error, subprocess2.CalledProcessError), e: 1308 except (gclient_utils.Error, subprocess2.CalledProcessError), e:
1303 print >> sys.stderr, 'Error: %s' % str(e) 1309 print >> sys.stderr, 'Error: %s' % str(e)
1304 return 1 1310 return 1
1305 1311
1306 1312
1307 if '__main__' == __name__: 1313 if '__main__' == __name__:
1308 fix_encoding.fix_encoding() 1314 fix_encoding.fix_encoding()
1309 sys.exit(Main(sys.argv[1:])) 1315 sys.exit(Main(sys.argv[1:]))
1310 1316
1311 # vim: ts=2:sw=2:tw=80:et: 1317 # vim: ts=2:sw=2:tw=80:et:
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698