From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 103161 invoked by alias); 13 Aug 2019 07:51:43 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 103053 invoked by uid 89); 13 Aug 2019 07:51:42 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-17.7 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,SPF_PASS autolearn=ham version=3.3.1 spammy= X-HELO: mx1.suse.de Received: from mx2.suse.de (HELO mx1.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 13 Aug 2019 07:51:41 +0000 Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 683EDAE8D for ; Tue, 13 Aug 2019 07:51:39 +0000 (UTC) Resent-From: =?UTF-8?Q?Martin_Li=c5=a1ka?= Resent-To: GCC Patches Resent-Date: Tue, 13 Aug 2019 09:51:38 +0200 Resent-Message-ID: Message-Id: <72ea403622c6fa241cd1abf88bef1de0ffeea00a.1565682592.git.mliska@suse.cz> In-Reply-To: References: From: Martin Liska Date: Tue, 13 Aug 2019 08:18:00 -0000 Subject: [PATCH 1/3] Use argparse.ArgumentParser for mklog. To: gcc-patches@gcc.gnu.org MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------2.22.0" X-IsSubscribed: yes X-SW-Source: 2019-08/txt/msg00800.txt.bz2 This is a multi-part message in MIME format. --------------2.22.0 Content-Type: text/plain; charset=UTF-8; format=fixed Content-Transfer-Encoding: 8bit Content-length: 235 contrib/ChangeLog: 2019-08-02 Martin Liska * mklog: Use argparse instead of getopt. --- contrib/mklog | 73 ++++++++++++++++++--------------------------------- 1 file changed, 25 insertions(+), 48 deletions(-) --------------2.22.0 Content-Type: text/x-patch; name="0001-Use-argparse.ArgumentParser-for-mklog.patch" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename="0001-Use-argparse.ArgumentParser-for-mklog.patch" Content-length: 3627 diff --git a/contrib/mklog b/contrib/mklog index 15558cfbfe3..e7a513fad5c 100755 --- a/contrib/mklog +++ b/contrib/mklog @@ -28,11 +28,11 @@ # # Author: Yury Gribov +import argparse import sys import re import os.path import os -import getopt import tempfile import time import shutil @@ -66,21 +66,6 @@ class RegexCache(object): 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): """Simple wrapper for Popen.""" proc = Popen(cmd.split(' '), stderr = PIPE, stdout = PIPE) @@ -356,38 +341,30 @@ def parse_patch(contents): def main(): name, email = read_user_info() - try: - opts, args = getopt.getopt(sys.argv[1:], 'hiv', ['help', 'verbose', 'inline']) - except getopt.GetoptError as err: - error(str(err)) - - inline = False - verbose = 0 - - for o, a in opts: - 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 = ['-'] + help_message = """\ +Generate ChangeLog template for PATCH. +PATCH must be generated using diff(1)'s -up or -cp options +(or their equivalent in Subversion/git). +""" - 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") + inline_message = """\ +Prepends ChangeLog to PATCH. +If PATCH is not stdin, modifies PATCH in-place, +otherwise writes to stdout.' +""" + 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() diffs = parse_patch(contents) - if verbose: + if args.verbose: print("Parse results:") for d in diffs: d.dump() @@ -431,7 +408,7 @@ def main(): 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 # the open to truncate. fd, tmp = tempfile.mkstemp("tmp.XXXXXXXX") @@ -439,7 +416,7 @@ def main(): # Copy permissions to temp file # (old Pythons do not support shutil.copymode) - shutil.copymode(args[0], tmp) + shutil.copymode(args.input, tmp) # Open the temp file, clearing contents. out = open(tmp, 'w') @@ -457,14 +434,14 @@ def main(): %s\n""" % (log_name, date, name, email, msg)) - if inline: + if args.inline: # Append patch body out.write(contents) - if args[0] != '-': + if args.input: # Write new contents atomically out.close() - shutil.move(tmp, args[0]) + shutil.move(tmp, args.input) if __name__ == '__main__': main() --------------2.22.0--