From a5d621f2ce3cfc0a0892ee4cd1c11d00cc13a8ca Mon Sep 17 00:00:00 2001 From: "borenet@google.com" Date: Fri, 25 Jan 2013 20:55:35 +0000 Subject: [PATCH] Manually parse arguments in submit_try Argparse not supported with Python < 2.7. Review URL: https://codereview.appspot.com/7206054 git-svn-id: http://skia.googlecode.com/svn/trunk@7403 2bbb7eff-a529-9590-31e7-b0007b416f81 --- tools/submit_try | 106 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 80 insertions(+), 26 deletions(-) diff --git a/tools/submit_try b/tools/submit_try index 2d18fc653f..0a4cf2d043 100755 --- a/tools/submit_try +++ b/tools/submit_try @@ -13,7 +13,6 @@ adds some validation and supports both git and svn. """ -import argparse import httplib import json import os @@ -121,35 +120,90 @@ def RetrieveTrybotList(): return trybots -def ValidateArgs(trybots, is_svn=True): +def ValidateArgs(argv, trybots, is_svn=True): """ Parse and validate command-line arguments. If the arguments are valid, returns a tuple of (, ). trybots: A list of the known try builders. """ - if is_svn: - parser = argparse.ArgumentParser( - prog=os.path.basename(__file__), - description='%(prog)s: Submit a try request.', - usage=('%(prog)s [-h] --bot ' - '[ [ ...] + +--bot Builder on which to run the try. Required. +-h, --help Show this message. +-r Revision from which to run the try. +-l, --list_bots List the available try builders and exit. +""" % (' ' if is_svn else '')) + + def Error(msg=None): + if msg: + print msg + print usage + sys.exit(1) + + using_bots = None + changelist = None + revision = None + + while argv: + arg = argv.pop(0) + if arg == '-h' or arg == '--help': + Error() + elif arg == '-l' or arg == '--list_bots': + print 'submit_try: Available builders:\n %s' % '\n '.join(trybots) + sys.exit(0) + elif arg == '--bot': + if using_bots: + Error('--bot specified multiple times.') + if len(argv) < 1: + Error('You must specify a builder with "--bot".') + using_bots = [] + while argv and not argv[0].startswith('-'): + bot = argv.pop(0) + if bot == ALL_BUILDERS: + if using_bots: + Error('Cannot specify "all" with additional builder names.') + using_bots = trybots + break + else: + if not bot in trybots: + Error('Unrecognized builder: %s' % bot) + using_bots.append(bot) + elif arg == '-r': + if len(argv) < 1: + Error('You must specify a revision with "-r".') + revision = argv.pop(0) + else: + if changelist or not is_svn: + Error('Unknown argument: %s' % arg) + changelist = arg + if is_svn and not changelist: + Error('You must specify a changelist name.') + if not using_bots: + Error('You must specify one or more builders using --bot.') + return CollectedArgs(bots=using_bots, changelist=changelist, + revision=revision) def SubmitTryRequest(args, is_svn=True): @@ -166,7 +220,7 @@ def SubmitTryRequest(args, is_svn=True): - revision: optional, int; the revision number from which to run the try. is_svn: boolean; are we in an SVN repo? """ - botlist = ','.join(['%s%s' % (bot, TRYBOT_SUFFIX) for bot in args.bot]) + botlist = ','.join(['%s%s' % (bot, TRYBOT_SUFFIX) for bot in args.bots]) if is_svn: gcl_cmd = 'gcl.bat' if os.name == 'nt' else 'gcl' try_args = [gcl_cmd, 'try', args.changelist, @@ -202,7 +256,7 @@ def main(): is_svn = os.path.isdir('.svn') # Parse and validate the command-line arguments. - args = ValidateArgs(trybots=trybots, is_svn=is_svn) + args = ValidateArgs(sys.argv[1:], trybots=trybots, is_svn=is_svn) # Submit the try request. SubmitTryRequest(args, is_svn=is_svn)