patman: Convert to ArgumentParser

Convert from OptionParser to ArgumentParser to match binman. With this we
can easily add sub-commands.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2020-07-05 21:41:53 -06:00
parent 137947e05b
commit fda1e372d3
3 changed files with 64 additions and 63 deletions

View file

@ -152,24 +152,24 @@ def email_patches(col, series, cover_fname, patch_files, process_tags, its_a_go,
os.remove(cc_file)
def send(options):
def send(args):
"""Create, check and send patches by email
Args:
options (optparse.Values): Arguments to patman
args (argparse.Namespace): Arguments to patman
"""
setup()
col = terminal.Color()
series, cover_fname, patch_files = prepare_patches(
col, options.branch, options.count, options.start, options.end,
options.ignore_binary)
ok = check_patches(series, patch_files, options.check_patch,
options.verbose)
col, args.branch, args.count, args.start, args.end,
args.ignore_binary)
ok = check_patches(series, patch_files, args.check_patch,
args.verbose)
its_a_go = ok or options.ignore_errors
its_a_go = ok or args.ignore_errors
if its_a_go:
email_patches(
col, series, cover_fname, patch_files, options.process_tags,
its_a_go, options.ignore_bad_tags, options.add_maintainers,
options.limit, options.dry_run, options.in_reply_to, options.thread,
options.smtp_server)
col, series, cover_fname, patch_files, args.process_tags,
its_a_go, args.ignore_bad_tags, args.add_maintainers,
args.limit, args.dry_run, args.in_reply_to, args.thread,
args.smtp_server)

View file

@ -6,7 +6,7 @@
"""See README for more information"""
from optparse import OptionParser
from argparse import ArgumentParser
import os
import re
import sys
@ -27,71 +27,70 @@ from patman import terminal
from patman import test_util
from patman import test_checkpatch
epilog = '''Create patches from commits in a branch, check them and email them
as specified by tags you place in the commits. Use -n to do a dry run first.'''
parser = OptionParser()
parser.add_option('-H', '--full-help', action='store_true', dest='full_help',
parser = ArgumentParser(epilog=epilog)
parser.add_argument('-H', '--full-help', action='store_true', dest='full_help',
default=False, help='Display the README file')
parser.add_option('-b', '--branch', type='str',
parser.add_argument('-b', '--branch', type=str,
help="Branch to process (by default, the current branch)")
parser.add_option('-c', '--count', dest='count', type='int',
parser.add_argument('-c', '--count', dest='count', type=int,
default=-1, help='Automatically create patches from top n commits')
parser.add_option('-e', '--end', type='int', default=0,
parser.add_argument('-e', '--end', type=int, default=0,
help='Commits to skip at end of patch list')
parser.add_option('-i', '--ignore-errors', action='store_true',
parser.add_argument('-i', '--ignore-errors', action='store_true',
dest='ignore_errors', default=False,
help='Send patches email even if patch errors are found')
parser.add_option('-l', '--limit-cc', dest='limit', type='int',
default=None, help='Limit the cc list to LIMIT entries [default: %default]')
parser.add_option('-m', '--no-maintainers', action='store_false',
parser.add_argument('-l', '--limit-cc', dest='limit', type=int, default=None,
help='Limit the cc list to LIMIT entries [default: %(default)]')
parser.add_argument('-m', '--no-maintainers', action='store_false',
dest='add_maintainers', default=True,
help="Don't cc the file maintainers automatically")
parser.add_option('-n', '--dry-run', action='store_true', dest='dry_run',
parser.add_argument('-n', '--dry-run', action='store_true', dest='dry_run',
default=False, help="Do a dry run (create but don't email patches)")
parser.add_option('-p', '--project', default=project.DetectProject(),
parser.add_argument('-p', '--project', default=project.DetectProject(),
help="Project name; affects default option values and "
"aliases [default: %default]")
parser.add_option('-r', '--in-reply-to', type='string', action='store',
"aliases [default: %(default)]")
parser.add_argument('-r', '--in-reply-to', type=str, action='store',
help="Message ID that this series is in reply to")
parser.add_option('-s', '--start', dest='start', type='int',
parser.add_argument('-s', '--start', dest='start', type=int,
default=0, help='Commit to start creating patches from (0 = HEAD)')
parser.add_option('-t', '--ignore-bad-tags', action='store_true',
parser.add_argument('-t', '--ignore-bad-tags', action='store_true',
default=False, help='Ignore bad tags / aliases')
parser.add_option('-v', '--verbose', action='store_true', dest='verbose',
parser.add_argument('-v', '--verbose', action='store_true', dest='verbose',
default=False, help='Verbose output of errors and warnings')
parser.add_option('-T', '--thread', action='store_true', dest='thread',
parser.add_argument('-T', '--thread', action='store_true', dest='thread',
default=False, help='Create patches as a single thread')
parser.add_option('--cc-cmd', dest='cc_cmd', type='string', action='store',
parser.add_argument('--cc-cmd', dest='cc_cmd', type=str, action='store',
default=None, help='Output cc list for patch file (used by git)')
parser.add_option('--no-binary', action='store_true', dest='ignore_binary',
parser.add_argument('--no-binary', action='store_true', dest='ignore_binary',
default=False,
help="Do not output contents of changes in binary files")
parser.add_option('--no-check', action='store_false', dest='check_patch',
parser.add_argument('--no-check', action='store_false', dest='check_patch',
default=True,
help="Don't check for patch compliance")
parser.add_option('--no-tags', action='store_false', dest='process_tags',
parser.add_argument('--no-tags', action='store_false', dest='process_tags',
default=True, help="Don't process subject tags as aliases")
parser.add_option('--smtp-server', type='str',
parser.add_argument('--smtp-server', type=str,
help="Specify the SMTP server to 'git send-email'")
parser.add_option('--test', action='store_true', dest='test',
parser.add_argument('--test', action='store_true', dest='test',
default=False, help='run tests')
parser.usage += """
Create patches from commits in a branch, check them and email them as
specified by tags you place in the commits. Use -n to do a dry run first."""
parser.add_argument('patchfiles', nargs='*')
# Parse options twice: first to get the project and second to handle
# defaults properly (which depends on project).
(options, args) = parser.parse_args()
settings.Setup(gitutil, parser, options.project, '')
(options, args) = parser.parse_args()
argv = sys.argv[1:]
args = parser.parse_args(argv)
settings.Setup(gitutil, parser, args.project, '')
args = parser.parse_args(argv)
if __name__ != "__main__":
pass
# Run our meagre tests
elif options.test:
elif args.test:
import doctest
from patman import func_test
@ -109,19 +108,19 @@ elif options.test:
# Called from git with a patch filename as argument
# Printout a list of additional CC recipients for this patch
elif options.cc_cmd:
fd = open(options.cc_cmd, 'r')
elif args.cc_cmd:
fd = open(args.cc_cmd, 'r')
re_line = re.compile('(\S*) (.*)')
for line in fd.readlines():
match = re_line.match(line)
if match and match.group(1) == args[0]:
if match and match.group(1) == args.patchfiles[0]:
for cc in match.group(2).split('\0'):
cc = cc.strip()
if cc:
print(cc)
fd.close()
elif options.full_help:
elif args.full_help:
pager = os.getenv('PAGER')
if not pager:
pager = 'more'
@ -131,4 +130,4 @@ elif options.full_help:
# Process commits, produce patches files, check them, email them
else:
control.send(options)
control.send(args)

View file

@ -233,17 +233,19 @@ def _UpdateDefaults(parser, config):
config: An instance of _ProjectConfigParser that we will query
for settings.
"""
defaults = parser.get_default_values()
defaults = parser.parse_known_args()[0]
defaults = vars(defaults)
for name, val in config.items('settings'):
if hasattr(defaults, name):
default_val = getattr(defaults, name)
if name in defaults:
default_val = defaults[name]
if isinstance(default_val, bool):
val = config.getboolean('settings', name)
elif isinstance(default_val, int):
val = config.getint('settings', name)
parser.set_default(name, val)
defaults[name] = val
else:
print("WARNING: Unknown setting %s" % name)
parser.set_defaults(**defaults)
def _ReadAliasFile(fname):
"""Read in the U-Boot git alias file if it exists.