Use argparse.ArgumentParser for mklog.

2019-09-04  Martin Liska  <mliska@suse.cz>

	* mklog: Use argparse instead of getopt.

From-SVN: r275367
This commit is contained in:
Martin Liska 2019-09-04 10:07:37 +02:00 committed by Martin Liska
parent dc91c65378
commit e94e92dcda
2 changed files with 29 additions and 48 deletions

View File

@ -1,3 +1,7 @@
2019-09-04 Martin Liska <mliska@suse.cz>
* mklog: Use argparse instead of getopt.
2019-09-03 Ulrich Weigand <uweigand@de.ibm.com> 2019-09-03 Ulrich Weigand <uweigand@de.ibm.com>
* compare-all-tests (all_targets): Remove references to spu. * compare-all-tests (all_targets): Remove references to spu.

View File

@ -28,11 +28,11 @@
# #
# Author: Yury Gribov <tetra2005@gmail.com> # Author: Yury Gribov <tetra2005@gmail.com>
import argparse
import sys import sys
import re import re
import os.path import os.path
import os import os
import getopt
import tempfile import tempfile
import time import time
import shutil import shutil
@ -66,21 +66,6 @@ class RegexCache(object):
cache = RegexCache() cache = RegexCache()
def print_help_and_exit():
print("""\
Usage: %s [-i | --inline] [PATCH]
Generate ChangeLog template for PATCH.
PATCH must be generated using diff(1)'s -up or -cp options
(or their equivalent in Subversion/git).
When PATCH is - or missing, read standard input.
When -i is used, prepends ChangeLog to PATCH.
If PATCH is not stdin, modifies PATCH in-place, otherwise writes
to stdout.
""" % me)
sys.exit(1)
def run(cmd, die_on_error): def run(cmd, die_on_error):
"""Simple wrapper for Popen.""" """Simple wrapper for Popen."""
proc = Popen(cmd.split(' '), stderr = PIPE, stdout = PIPE) proc = Popen(cmd.split(' '), stderr = PIPE, stdout = PIPE)
@ -356,38 +341,30 @@ def parse_patch(contents):
def main(): def main():
name, email = read_user_info() name, email = read_user_info()
try: help_message = """\
opts, args = getopt.getopt(sys.argv[1:], 'hiv', ['help', 'verbose', 'inline']) Generate ChangeLog template for PATCH.
except getopt.GetoptError as err: PATCH must be generated using diff(1)'s -up or -cp options
error(str(err)) (or their equivalent in Subversion/git).
"""
inline = False inline_message = """\
verbose = 0 Prepends ChangeLog to PATCH.
If PATCH is not stdin, modifies PATCH in-place,
for o, a in opts: otherwise writes to stdout.'
if o in ('-h', '--help'): """
print_help_and_exit()
elif o in ('-i', '--inline'):
inline = True
elif o in ('-v', '--verbose'):
verbose += 1
else:
assert False, "unhandled option"
if len(args) == 0:
args = ['-']
if len(args) == 1 and args[0] == '-':
input = sys.stdin
elif len(args) == 1:
input = open(args[0])
else:
error("too many arguments; for more details run with -h")
parser = argparse.ArgumentParser(description = help_message)
parser.add_argument('-v', '--verbose', action = 'store_true', help = 'Verbose messages')
parser.add_argument('-i', '--inline', action = 'store_true', help = inline_message)
parser.add_argument('input', nargs = '?', help = 'Patch file (or missing, read standard input)')
args = parser.parse_args()
if args.input == '-':
args.input = None
input = open(args.input) if args.input else sys.stdin
contents = input.read() contents = input.read()
diffs = parse_patch(contents) diffs = parse_patch(contents)
if verbose: if args.verbose:
print("Parse results:") print("Parse results:")
for d in diffs: for d in diffs:
d.dump() d.dump()
@ -431,7 +408,7 @@ def main():
logs[log_name] += change_msg if change_msg else ":\n" logs[log_name] += change_msg if change_msg else ":\n"
if inline and args[0] != '-': if args.inline and args.input:
# Get a temp filename, rather than an open filehandle, because we use # Get a temp filename, rather than an open filehandle, because we use
# the open to truncate. # the open to truncate.
fd, tmp = tempfile.mkstemp("tmp.XXXXXXXX") fd, tmp = tempfile.mkstemp("tmp.XXXXXXXX")
@ -439,7 +416,7 @@ def main():
# Copy permissions to temp file # Copy permissions to temp file
# (old Pythons do not support shutil.copymode) # (old Pythons do not support shutil.copymode)
shutil.copymode(args[0], tmp) shutil.copymode(args.input, tmp)
# Open the temp file, clearing contents. # Open the temp file, clearing contents.
out = open(tmp, 'w') out = open(tmp, 'w')
@ -457,14 +434,14 @@ def main():
%s\n""" % (log_name, date, name, email, msg)) %s\n""" % (log_name, date, name, email, msg))
if inline: if args.inline:
# Append patch body # Append patch body
out.write(contents) out.write(contents)
if args[0] != '-': if args.input:
# Write new contents atomically # Write new contents atomically
out.close() out.close()
shutil.move(tmp, args[0]) shutil.move(tmp, args.input)
if __name__ == '__main__': if __name__ == '__main__':
main() main()