Merge tag 'u-boot-atmel-fixes-2021.01-b' of https://gitlab.denx.de/u-boot/custodians...
[platform/kernel/u-boot.git] / tools / patman / main.py
index 72c67b8..342fd44 100755 (executable)
@@ -6,10 +6,11 @@
 
 """See README for more information"""
 
-from optparse import OptionParser
+from argparse import ArgumentParser
 import os
 import re
 import sys
+import traceback
 import unittest
 
 if __name__ == "__main__":
@@ -18,167 +19,175 @@ if __name__ == "__main__":
     sys.path.append(os.path.join(our_path, '..'))
 
 # Our modules
-from patman import checkpatch
 from patman import command
+from patman import control
 from patman import gitutil
-from patman import patchstream
 from patman import project
 from patman import settings
 from patman import terminal
-from patman import test
-
-
-parser = OptionParser()
-parser.add_option('-H', '--full-help', action='store_true', dest='full_help',
-       default=False, help='Display the README file')
-parser.add_option('-c', '--count', dest='count', type='int',
-       default=-1, help='Automatically create patches from top n commits')
-parser.add_option('-i', '--ignore-errors', action='store_true',
+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 = 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.DetectProject(),
+                    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')
+send.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',
+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")
-parser.add_option('-n', '--dry-run', action='store_true', dest='dry_run',
+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)")
-parser.add_option('-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',
+send.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',
-       default=0, help='Commit to start creating patches from (0 = HEAD)')
-parser.add_option('-t', '--ignore-bad-tags', action='store_true',
+send.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',
-       default=False, help='Verbose output of errors and warnings')
-parser.add_option('-T', '--thread', action='store_true', dest='thread',
+send.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',
+send.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-check', action='store_false', dest='check_patch',
+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")
-parser.add_option('--no-tags', action='store_false', dest='process_tags',
-                  default=True, help="Don't process subject tags as aliaes")
-parser.add_option('--smtp-server', type='str',
+send.add_argument('--no-tags', action='store_false', dest='process_tags',
+                  default=True, help="Don't process subject tags as aliases")
+send.add_argument('--smtp-server', type=str,
                   help="Specify the SMTP server to 'git send-email'")
-parser.add_option('--test', action='store_true', dest='test',
-                  default=False, help='run tests')
 
-parser.usage += """
+send.add_argument('patchfiles', nargs='*')
 
-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."""
+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).
-(options, args) = parser.parse_args()
-settings.Setup(parser, options.project, '')
-(options, args) = parser.parse_args()
+# 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(gitutil, 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
 
+if not args.debug:
+    sys.tracebacklimit = 0
+
 # Run our meagre tests
-elif options.test:
+if args.cmd == 'test':
     import doctest
     from patman import func_test
 
     sys.argv = [sys.argv[0]]
     result = unittest.TestResult()
-    for module in (test.TestPatch, func_test.TestFunctional):
-        suite = unittest.TestLoader().loadTestsFromTestCase(module)
-        suite.run(result)
+    suite = unittest.TestSuite()
+    loader = unittest.TestLoader()
+    for module in (test_checkpatch.TestPatch, func_test.TestFunctional):
+        if args.testname:
+            try:
+                suite.addTests(loader.loadTestsFromName(args.testname, module))
+            except AttributeError:
+                continue
+        else:
+            suite.addTests(loader.loadTestsFromTestCase(module))
+    suite.run(result)
 
     for module in ['gitutil', 'settings', 'terminal']:
         suite = doctest.DocTestSuite(module)
         suite.run(result)
 
-    # TODO: Surely we can just 'print' result?
-    print(result)
-    for test, err in result.errors:
-        print(err)
-    for test, err in result.failures:
-        print(err)
-
-# 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')
-    re_line = re.compile('(\S*) (.*)')
-    for line in fd.readlines():
-        match = re_line.match(line)
-        if match and match.group(1) == args[0]:
-            for cc in match.group(2).split('\0'):
-                cc = cc.strip()
-                if cc:
-                    print(cc)
-    fd.close()
-
-elif options.full_help:
-    pager = os.getenv('PAGER')
-    if not pager:
-        pager = 'more'
-    fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
-                         'README')
-    command.Run(pager, fname)
+    sys.exit(test_util.ReportResult('patman', args.testname, result))
 
 # Process commits, produce patches files, check them, email them
-else:
-    gitutil.Setup()
-
-    if options.count == -1:
-        # Work out how many patches to send if we can
-        options.count = gitutil.CountCommitsToBranch() - options.start
-
-    col = terminal.Color()
-    if not options.count:
-        str = 'No commits found to process - please use -c flag'
-        sys.exit(col.Color(col.RED, str))
-
-    # Read the metadata from the commits
-    if options.count:
-        series = patchstream.GetMetaData(options.start, options.count)
-        cover_fname, args = gitutil.CreatePatches(options.start, options.count,
-                series)
-
-    # Fix up the patch files to our liking, and insert the cover letter
-    patchstream.FixPatches(series, args)
-    if cover_fname and series.get('cover'):
-        patchstream.InsertCoverLetter(cover_fname, series, options.count)
-
-    # Do a few checks on the series
-    series.DoChecks()
-
-    # Check the patches, and run them through 'git am' just to be sure
-    if options.check_patch:
-        ok = checkpatch.CheckPatches(options.verbose, args)
-    else:
-        ok = True
-
-    cc_file = series.MakeCcFile(options.process_tags, cover_fname,
-                                not options.ignore_bad_tags,
-                                options.add_maintainers, options.limit)
-
-    # Email the patches out (giving the user time to check / cancel)
-    cmd = ''
-    its_a_go = ok or options.ignore_errors
-    if its_a_go:
-        cmd = gitutil.EmailPatches(series, cover_fname, args,
-                options.dry_run, not options.ignore_bad_tags, cc_file,
-                in_reply_to=options.in_reply_to, thread=options.thread,
-                smtp_server=options.smtp_server)
-    else:
-        print(col.Color(col.RED, "Not sending emails due to errors/warnings"))
+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:
+        pager = os.getenv('PAGER')
+        if not pager:
+            pager = 'more'
+        fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
+                             'README')
+        command.Run(pager, fname)
 
-    # For a dry run, just show our actions as a sanity check
-    if options.dry_run:
-        series.ShowActions(args, cmd, options.process_tags)
-        if not its_a_go:
-            print(col.Color(col.RED, "Email would not be sent"))
-
-    os.remove(cc_file)
+    else:
+        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.Print('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)