patman: Convert to ArgumentParser
[platform/kernel/u-boot.git] / tools / patman / main.py
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: GPL-2.0+
3 #
4 # Copyright (c) 2011 The Chromium OS Authors.
5 #
6
7 """See README for more information"""
8
9 from argparse import ArgumentParser
10 import os
11 import re
12 import sys
13 import unittest
14
15 if __name__ == "__main__":
16     # Allow 'from patman import xxx to work'
17     our_path = os.path.dirname(os.path.realpath(__file__))
18     sys.path.append(os.path.join(our_path, '..'))
19
20 # Our modules
21 from patman import command
22 from patman import control
23 from patman import gitutil
24 from patman import project
25 from patman import settings
26 from patman import terminal
27 from patman import test_util
28 from patman import test_checkpatch
29
30 epilog = '''Create patches from commits in a branch, check them and email them
31 as specified by tags you place in the commits. Use -n to do a dry run first.'''
32
33 parser = ArgumentParser(epilog=epilog)
34 parser.add_argument('-H', '--full-help', action='store_true', dest='full_help',
35        default=False, help='Display the README file')
36 parser.add_argument('-b', '--branch', type=str,
37                   help="Branch to process (by default, the current branch)")
38 parser.add_argument('-c', '--count', dest='count', type=int,
39        default=-1, help='Automatically create patches from top n commits')
40 parser.add_argument('-e', '--end', type=int, default=0,
41                   help='Commits to skip at end of patch list')
42 parser.add_argument('-i', '--ignore-errors', action='store_true',
43        dest='ignore_errors', default=False,
44        help='Send patches email even if patch errors are found')
45 parser.add_argument('-l', '--limit-cc', dest='limit', type=int, default=None,
46        help='Limit the cc list to LIMIT entries [default: %(default)]')
47 parser.add_argument('-m', '--no-maintainers', action='store_false',
48        dest='add_maintainers', default=True,
49        help="Don't cc the file maintainers automatically")
50 parser.add_argument('-n', '--dry-run', action='store_true', dest='dry_run',
51        default=False, help="Do a dry run (create but don't email patches)")
52 parser.add_argument('-p', '--project', default=project.DetectProject(),
53                     help="Project name; affects default option values and "
54                     "aliases [default: %(default)]")
55 parser.add_argument('-r', '--in-reply-to', type=str, action='store',
56                   help="Message ID that this series is in reply to")
57 parser.add_argument('-s', '--start', dest='start', type=int,
58        default=0, help='Commit to start creating patches from (0 = HEAD)')
59 parser.add_argument('-t', '--ignore-bad-tags', action='store_true',
60                     default=False, help='Ignore bad tags / aliases')
61 parser.add_argument('-v', '--verbose', action='store_true', dest='verbose',
62        default=False, help='Verbose output of errors and warnings')
63 parser.add_argument('-T', '--thread', action='store_true', dest='thread',
64                     default=False, help='Create patches as a single thread')
65 parser.add_argument('--cc-cmd', dest='cc_cmd', type=str, action='store',
66        default=None, help='Output cc list for patch file (used by git)')
67 parser.add_argument('--no-binary', action='store_true', dest='ignore_binary',
68                     default=False,
69                     help="Do not output contents of changes in binary files")
70 parser.add_argument('--no-check', action='store_false', dest='check_patch',
71                     default=True,
72                     help="Don't check for patch compliance")
73 parser.add_argument('--no-tags', action='store_false', dest='process_tags',
74                     default=True, help="Don't process subject tags as aliases")
75 parser.add_argument('--smtp-server', type=str,
76                     help="Specify the SMTP server to 'git send-email'")
77 parser.add_argument('--test', action='store_true', dest='test',
78                     default=False, help='run tests')
79
80 parser.add_argument('patchfiles', nargs='*')
81
82 # Parse options twice: first to get the project and second to handle
83 # defaults properly (which depends on project).
84 argv = sys.argv[1:]
85 args = parser.parse_args(argv)
86 settings.Setup(gitutil, parser, args.project, '')
87 args = parser.parse_args(argv)
88
89 if __name__ != "__main__":
90     pass
91
92 # Run our meagre tests
93 elif args.test:
94     import doctest
95     from patman import func_test
96
97     sys.argv = [sys.argv[0]]
98     result = unittest.TestResult()
99     for module in (test_checkpatch.TestPatch, func_test.TestFunctional):
100         suite = unittest.TestLoader().loadTestsFromTestCase(module)
101         suite.run(result)
102
103     for module in ['gitutil', 'settings', 'terminal']:
104         suite = doctest.DocTestSuite(module)
105         suite.run(result)
106
107     sys.exit(test_util.ReportResult('patman', None, result))
108
109 # Called from git with a patch filename as argument
110 # Printout a list of additional CC recipients for this patch
111 elif args.cc_cmd:
112     fd = open(args.cc_cmd, 'r')
113     re_line = re.compile('(\S*) (.*)')
114     for line in fd.readlines():
115         match = re_line.match(line)
116         if match and match.group(1) == args.patchfiles[0]:
117             for cc in match.group(2).split('\0'):
118                 cc = cc.strip()
119                 if cc:
120                     print(cc)
121     fd.close()
122
123 elif args.full_help:
124     pager = os.getenv('PAGER')
125     if not pager:
126         pager = 'more'
127     fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
128                          'README')
129     command.Run(pager, fname)
130
131 # Process commits, produce patches files, check them, email them
132 else:
133     control.send(args)