Skip to contentSkip to conversation

patman: Split out arg parsing into its own file

PATCH In review Simon Glass 1 participant 10 messages Nov 4, 2023 – Nov 15, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 patman: Split out arg parsing into its own file

Move this code into a separate cmdline module, as is done with the other tools.

Use the same HAS_TESTS check as buildman

---

 tools/patman/__main__.py | 116 +-----------------------------
 tools/patman/cmdline.py  | 147 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 150 insertions(+), 113 deletions(-)
 create mode 100644 tools/patman/cmdline.py
tools/patman/__main__.py +3 -113
index 197ac1aad103..0e559b5810be 100755
@@ -6,7 +6,6 @@
 
 """See README for more information"""
 
-from argparse import ArgumentParser
 try:
     import importlib.resources
 except ImportError:
@@ -23,128 +22,19 @@ if __name__ == "__main__":
     sys.path.append(os.path.join(our_path, '..'))
 
 # Our modules
+from patman import cmdline
 from patman import control
 from patman import func_test
-from patman import gitutil
-from patman import project
-from patman import settings
 from u_boot_pylib import terminal
 from u_boot_pylib import test_util
 from u_boot_pylib import tools
 
-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 = ArgumentParser(epilog=epilog)
-parser.add_argument('-b', '--branch', type=str,
-    help="Branch to process (by default, the current branch)")
-parser.add_argument('-c', '--count', dest='count', type=int,
-    default=-1, help='Automatically create patches from top n commits')
-parser.add_argument('-e', '--end', type=int, default=0,
-    help='Commits to skip at end of patch list')
-parser.add_argument('-D', '--debug', action='store_true',
-    help='Enabling debugging (provides a full traceback on error)')
-parser.add_argument('-p', '--project', default=project.detect_project(),
-                    help="Project name; affects default option values and "
-                    "aliases [default: %(default)s]")
-parser.add_argument('-P', '--patchwork-url',
-                    default='https://patchwork.ozlabs.org',
-                    help='URL of patchwork server [default: %(default)s]')
-parser.add_argument('-s', '--start', dest='start', type=int,
-    default=0, help='Commit to start creating patches from (0 = HEAD)')
-parser.add_argument('-v', '--verbose', action='store_true', dest='verbose',
-                    default=False, help='Verbose output of errors and warnings')
-parser.add_argument('-H', '--full-help', action='store_true', dest='full_help',
-                    default=False, help='Display the README file')
-
-subparsers = parser.add_subparsers(dest='cmd')
-send = subparsers.add_parser(
-    'send', help='Format, check and email patches (default command)')
-send.add_argument('-i', '--ignore-errors', action='store_true',
-       dest='ignore_errors', default=False,
-       help='Send patches email even if patch errors are found')
-send.add_argument('-l', '--limit-cc', dest='limit', type=int, default=None,
-       help='Limit the cc list to LIMIT entries [default: %(default)s]')
-send.add_argument('-m', '--no-maintainers', action='store_false',
-       dest='add_maintainers', default=True,
-       help="Don't cc the file maintainers automatically")
-send.add_argument(
-    '--get-maintainer-script', dest='get_maintainer_script', type=str,
-    action='store',
-    default=os.path.join(gitutil.get_top_level(), 'scripts',
-                         'get_maintainer.pl') + ' --norolestats',
-    help='File name of the get_maintainer.pl (or compatible) script.')
-send.add_argument('-n', '--dry-run', action='store_true', dest='dry_run',
-       default=False, help="Do a dry run (create but don't email patches)")
-send.add_argument('-r', '--in-reply-to', type=str, action='store',
-                  help="Message ID that this series is in reply to")
-send.add_argument('-t', '--ignore-bad-tags', action='store_true',
-                  default=False,
-                  help='Ignore bad tags / aliases (default=warn)')
-send.add_argument('-T', '--thread', action='store_true', dest='thread',
-                  default=False, help='Create patches as a single thread')
-send.add_argument('--cc-cmd', dest='cc_cmd', type=str, action='store',
-       default=None, help='Output cc list for patch file (used by git)')
-send.add_argument('--no-binary', action='store_true', dest='ignore_binary',
-                  default=False,
-                  help="Do not output contents of changes in binary files")
-send.add_argument('--no-check', action='store_false', dest='check_patch',
-                  default=True,
-                  help="Don't check for patch compliance")
-send.add_argument('--tree', dest='check_patch_use_tree', default=False,
-                  action='store_true',
-                  help=("Set `tree` to True. If `tree` is False then we'll "
-                  "pass '--no-tree' to checkpatch (default: tree=%(default)s)"))
-send.add_argument('--no-tree', dest='check_patch_use_tree',
-                  action='store_false', help="Set `tree` to False")
-send.add_argument('--no-tags', action='store_false', dest='process_tags',
-                  default=True, help="Don't process subject tags as aliases")
-send.add_argument('--no-signoff', action='store_false', dest='add_signoff',
-                  default=True, help="Don't add Signed-off-by to patches")
-send.add_argument('--smtp-server', type=str,
-                  help="Specify the SMTP server to 'git send-email'")
-send.add_argument('--keep-change-id', action='store_true',
-                  help='Preserve Change-Id tags in patches to send.')
-
-send.add_argument('patchfiles', nargs='*')
-
-# Only add the 'test' action if the test data files are available.
-if os.path.exists(func_test.TEST_DATA_DIR):
-    test_parser = subparsers.add_parser('test', help='Run tests')
-    test_parser.add_argument('testname', type=str, default=None, nargs='?',
-                             help="Specify the test to run")
-
-status = subparsers.add_parser('status',
-                               help='Check status of patches in patchwork')
-status.add_argument('-C', '--show-comments', action='store_true',
-                    help='Show comments from each patch')
-status.add_argument('-d', '--dest-branch', type=str,
-                    help='Name of branch to create with collected responses')
-status.add_argument('-f', '--force', action='store_true',
-                    help='Force overwriting an existing branch')
-
-# Parse options twice: first to get the project and second to handle
-# defaults properly (which depends on project)
-# Use parse_known_args() in case 'cmd' is omitted
-argv = sys.argv[1:]
-args, rest = parser.parse_known_args(argv)
-if hasattr(args, 'project'):
-    settings.Setup(parser, args.project)
-    args, rest = parser.parse_known_args(argv)
-
-# If we have a command, it is safe to parse all arguments
-if args.cmd:
-    args = parser.parse_args(argv)
-else:
-    # No command, so insert it after the known arguments and before the ones
-    # that presumably relate to the 'send' subcommand
-    nargs = len(rest)
-    argv = argv[:-nargs] + ['send'] + rest
-    args = parser.parse_args(argv)
 
 if __name__ != "__main__":
     pass
 
+args = cmdline.parse_args()
+
 if not args.debug:
     sys.tracebacklimit = 0
 
tools/patman/cmdline.py new file +147 -0

147 lines not on this page · raw message

Signature · 1 line
2.42.0.869.gea05f2083d-goog
#1 lore raw

PATCH 2/5 patman: Move the main program into a function

Add a new run_patman() function to hold the main logic.

---

 tools/patman/__main__.py | 127 +++++++++++++++++++++------------------
 1 file changed, 67 insertions(+), 60 deletions(-)
tools/patman/__main__.py +67 -60
index 0e559b5810be..87850295e704 100755
@@ -30,63 +30,70 @@ from u_boot_pylib import test_util
 from u_boot_pylib import tools
 
 
-if __name__ != "__main__":
-    pass
-
-args = cmdline.parse_args()
-
-if not args.debug:
-    sys.tracebacklimit = 0
-
-# Run our meagre tests
-if args.cmd == 'test':
-    from patman import func_test
-    from patman import test_checkpatch
-
-    result = test_util.run_test_suites(
-        'patman', False, False, False, None, None, None,
-        [test_checkpatch.TestPatch, func_test.TestFunctional,
-         'gitutil', 'settings'])
-
-    sys.exit(0 if result.wasSuccessful() else 1)
-
-# Process commits, produce patches files, check them, email them
-elif args.cmd == 'send':
-    # Called from git with a patch filename as argument
-    # Printout a list of additional CC recipients for this patch
-    if 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.patchfiles[0]:
-                for cc in match.group(2).split('\0'):
-                    cc = cc.strip()
-                    if cc:
-                        print(cc)
-        fd.close()
-
-    elif args.full_help:
-        with importlib.resources.path('patman', 'README.rst') as readme:
-            tools.print_full_help(str(readme))
-    else:
-        # If we are not processing tags, no need to warning about bad ones
-        if not args.process_tags:
-            args.ignore_bad_tags = True
-        control.send(args)
-
-# Check status of patches in patchwork
-elif args.cmd == 'status':
-    ret_code = 0
-    try:
-        control.patchwork_status(args.branch, args.count, args.start, args.end,
-                                 args.dest_branch, args.force,
-                                 args.show_comments, args.patchwork_url)
-    except Exception as e:
-        terminal.tprint('patman: %s: %s' % (type(e).__name__, e),
-                        colour=terminal.Color.RED)
-        if args.debug:
-            print()
-            traceback.print_exc()
-        ret_code = 1
-    sys.exit(ret_code)
+def run_patman():
+    """Run patamn
+
+    This is the main program. It collects arguments and runs either the tests or
+    the control module.
+    """
+    args = cmdline.parse_args()
+
+    if not args.debug:
+        sys.tracebacklimit = 0
+
+    # Run our meagre tests
+    if args.cmd == 'test':
+        from patman import func_test
+        from patman import test_checkpatch
+
+        result = test_util.run_test_suites(
+            'patman', False, False, False, None, None, None,
+            [test_checkpatch.TestPatch, func_test.TestFunctional,
+             'gitutil', 'settings'])
+
+        sys.exit(0 if result.wasSuccessful() else 1)
+
+    # Process commits, produce patches files, check them, email them
+    elif args.cmd == 'send':
+        # Called from git with a patch filename as argument
+        # Printout a list of additional CC recipients for this patch
+        if 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.patchfiles[0]:
+                    for cc in match.group(2).split('\0'):
+                        cc = cc.strip()
+                        if cc:
+                            print(cc)
+            fd.close()
+
+        elif args.full_help:
+            with importlib.resources.path('patman', 'README.rst') as readme:
+                tools.print_full_help(str(readme))
+        else:
+            # If we are not processing tags, no need to warning about bad ones
+            if not args.process_tags:
+                args.ignore_bad_tags = True
+            control.send(args)
+
+    # Check status of patches in patchwork
+    elif args.cmd == 'status':
+        ret_code = 0
+        try:
+            control.patchwork_status(args.branch, args.count, args.start, args.end,
+                                     args.dest_branch, args.force,
+                                     args.show_comments, args.patchwork_url)
+        except Exception as e:
+            terminal.tprint('patman: %s: %s' % (type(e).__name__, e),
+                            colour=terminal.Color.RED)
+            if args.debug:
+                print()
+                traceback.print_exc()
+            ret_code = 1
+        sys.exit(ret_code)
+
+
+if __name__ == "__main__":
+    sys.exit(run_patman())
Signature · 1 line
2.42.0.869.gea05f2083d-goog
#2 lore raw

Re: [PATCH 2/5] patman: Move the main program into a function

Add a new run_patman() function to hold the main logic.

---

 tools/patman/__main__.py | 127 +++++++++++++++++++++------------------
 1 file changed, 67 insertions(+), 60 deletions(-)

Applied to u-boot-dm, thanks!

#3 lore raw

PATCH 3/5 patman: Correct easy pylint warnings in __main__

In reply to Simon Glass

Tidy up the code a little to reduce the number of pylint warnings.

---

 tools/patman/__main__.py | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)
tools/patman/__main__.py +16 -16
index 87850295e704..3ca858cd3157 100755
@@ -16,10 +16,10 @@ import re
 import sys
 import traceback
 
-if __name__ == "__main__":
-    # Allow 'from patman import xxx to work'
-    our_path = os.path.dirname(os.path.realpath(__file__))
-    sys.path.append(os.path.join(our_path, '..'))
+# Allow 'from patman import xxx to work'
+# pylint: disable=C0413
+our_path = os.path.dirname(os.path.realpath(__file__))
+sys.path.append(os.path.join(our_path, '..'))
 
 # Our modules
 from patman import cmdline
@@ -43,6 +43,7 @@ def run_patman():
 
     # Run our meagre tests
     if args.cmd == 'test':
+        # pylint: disable=C0415
         from patman import func_test
         from patman import test_checkpatch
 
@@ -58,16 +59,15 @@ def run_patman():
         # Called from git with a patch filename as argument
         # Printout a list of additional CC recipients for this patch
         if 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.patchfiles[0]:
-                    for cc in match.group(2).split('\0'):
-                        cc = cc.strip()
-                        if cc:
-                            print(cc)
-            fd.close()
+            re_line = re.compile(r'(\S*) (.*)')
+            with open(args.cc_cmd, 'r', encoding='utf-8') as inf:
+                for line in inf.readlines():
+                    match = re_line.match(line)
+                    if match and match.group(1) == args.patchfiles[0]:
+                        for cca in match.group(2).split('\0'):
+                            cca = cca.strip()
+                            if cca:
+                                print(cca)
 
         elif args.full_help:
             with importlib.resources.path('patman', 'README.rst') as readme:
@@ -85,8 +85,8 @@ def run_patman():
             control.patchwork_status(args.branch, args.count, args.start, args.end,
                                      args.dest_branch, args.force,
                                      args.show_comments, args.patchwork_url)
-        except Exception as e:
-            terminal.tprint('patman: %s: %s' % (type(e).__name__, e),
+        except Exception as exc:
+            terminal.tprint(f'patman: {type(exc).__name__}: {exc}',
                             colour=terminal.Color.RED)
             if args.debug:
                 print()
Signature · 1 line
2.42.0.869.gea05f2083d-goog
#4 lore raw

Re: [PATCH 3/5] patman: Correct easy pylint warnings in __main__

Tidy up the code a little to reduce the number of pylint warnings.

---

 tools/patman/__main__.py | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)

Applied to u-boot-dm, thanks!

#5 lore raw

PATCH 4/5 patman: Avoid using func_test at top level

In reply to Simon Glass

Import this only when it is needed, since it is not present when installed via 'pip install'.

---

 tools/patman/__main__.py | 1 -
 1 file changed, 1 deletion(-)
tools/patman/__main__.py +0 -1
index 3ca858cd3157..d53f686a2d45 100755
@@ -24,7 +24,6 @@ sys.path.append(os.path.join(our_path, '..'))
 # Our modules
 from patman import cmdline
 from patman import control
-from patman import func_test
 from u_boot_pylib import terminal
 from u_boot_pylib import test_util
 from u_boot_pylib import tools
Signature · 1 line
2.42.0.869.gea05f2083d-goog
#7 lore raw

PATCH 5/5 patman: Correct Python 3.6 behaviour

In reply to Simon Glass

The importlib_resources import is not actually used. Fix this so that patman can run on Python 3.6 to some extent, once
'pip3 install importlib-resources' has been run.

---

 tools/patman/__main__.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/patman/__main__.py +3 -3
index d53f686a2d45..f645b38b6470 100755
@@ -7,10 +7,10 @@
 """See README for more information"""
 
 try:
-    import importlib.resources
+    from importlib import resources
 except ImportError:
     # for Python 3.6
-    import importlib_resources
+    import importlib_resources as resources
 import os
 import re
 import sys
@@ -69,7 +69,7 @@ def run_patman():
                                 print(cca)
 
         elif args.full_help:
-            with importlib.resources.path('patman', 'README.rst') as readme:
+            with resources.path('patman', 'README.rst') as readme:
                 tools.print_full_help(str(readme))
         else:
             # If we are not processing tags, no need to warning about bad ones
Signature · 1 line
2.42.0.869.gea05f2083d-goog
#8 lore raw

Re: [PATCH 5/5] patman: Correct Python 3.6 behaviour

The importlib_resources import is not actually used. Fix this so that patman can run on Python 3.6 to some extent, once
'pip3 install importlib-resources' has been run.

---

 tools/patman/__main__.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Applied to u-boot-dm, thanks!

#9 lore raw
In reply to Simon Glass

Move this code into a separate cmdline module, as is done with the other tools.

Use the same HAS_TESTS check as buildman

---

 tools/patman/__main__.py | 116 +-----------------------------
 tools/patman/cmdline.py  | 147 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 150 insertions(+), 113 deletions(-)
 create mode 100644 tools/patman/cmdline.py

Applied to u-boot-dm, thanks!