blob: 0ae92f88c4b5c1cd27e09e374d1d91cccaa20f52 [file] [log] [blame]
Simon Glass22ce6412023-11-04 10:25:20 -06001# SPDX-License-Identifier: GPL-2.0+
2#
3# Copyright 2023 Google LLC
4#
5
6"""Handles parsing of buildman arguments
7
8This creates the argument parser and uses it to parse the arguments passed in
9"""
10
11import argparse
12import os
13import pathlib
14import sys
15
Simon Glassba1b3b92025-02-09 14:26:00 -070016from u_boot_pylib import gitutil
Simon Glass44ecdd42025-05-08 06:21:33 +020017from patman import project
Simon Glass22ce6412023-11-04 10:25:20 -060018from patman import settings
19
20PATMAN_DIR = pathlib.Path(__file__).parent
21HAS_TESTS = os.path.exists(PATMAN_DIR / "func_test.py")
22
Simon Glass22ce6412023-11-04 10:25:20 -060023
Simon Glass44ecdd42025-05-08 06:21:33 +020024def add_send_args(par):
25 """Add arguments for the 'send' command
Simon Glass22ce6412023-11-04 10:25:20 -060026
Simon Glass44ecdd42025-05-08 06:21:33 +020027 Arguments:
28 par (ArgumentParser): Parser to add to
Simon Glasse55a11c2025-05-08 06:09:46 +020029 """
Simon Glass44ecdd42025-05-08 06:21:33 +020030 par.add_argument(
31 '-c', '--count', dest='count', type=int, default=-1,
32 help='Automatically create patches from top n commits')
33 par.add_argument(
34 '-e', '--end', type=int, default=0,
35 help='Commits to skip at end of patch list')
36 par.add_argument(
Simon Glasse55a11c2025-05-08 06:09:46 +020037 '-i', '--ignore-errors', action='store_true',
38 dest='ignore_errors', default=False,
39 help='Send patches email even if patch errors are found')
Simon Glass44ecdd42025-05-08 06:21:33 +020040 par.add_argument(
Simon Glasse55a11c2025-05-08 06:09:46 +020041 '-l', '--limit-cc', dest='limit', type=int, default=None,
42 help='Limit the cc list to LIMIT entries [default: %(default)s]')
Simon Glass44ecdd42025-05-08 06:21:33 +020043 par.add_argument(
Simon Glasse55a11c2025-05-08 06:09:46 +020044 '-m', '--no-maintainers', action='store_false',
45 dest='add_maintainers', default=True,
46 help="Don't cc the file maintainers automatically")
Simon Glass44ecdd42025-05-08 06:21:33 +020047 par.add_argument(
Simon Glass22ce6412023-11-04 10:25:20 -060048 '--get-maintainer-script', dest='get_maintainer_script', type=str,
49 action='store',
50 default=os.path.join(gitutil.get_top_level(), 'scripts',
51 'get_maintainer.pl') + ' --norolestats',
52 help='File name of the get_maintainer.pl (or compatible) script.')
Simon Glass44ecdd42025-05-08 06:21:33 +020053 par.add_argument(
54 '-r', '--in-reply-to', type=str, action='store',
55 help="Message ID that this series is in reply to")
56 par.add_argument(
57 '-s', '--start', dest='start', type=int, default=0,
58 help='Commit to start creating patches from (0 = HEAD)')
59 par.add_argument(
60 '-t', '--ignore-bad-tags', action='store_true', default=False,
61 help='Ignore bad tags / aliases (default=warn)')
62 par.add_argument(
63 '--no-binary', action='store_true', dest='ignore_binary',
64 default=False,
65 help="Do not output contents of changes in binary files")
66 par.add_argument(
67 '--no-check', action='store_false', dest='check_patch', default=True,
68 help="Don't check for patch compliance")
69 par.add_argument(
Simon Glass22ce6412023-11-04 10:25:20 -060070 '--tree', dest='check_patch_use_tree', default=False,
71 action='store_true',
72 help=("Set `tree` to True. If `tree` is False then we'll pass "
73 "'--no-tree' to checkpatch (default: tree=%(default)s)"))
Simon Glass44ecdd42025-05-08 06:21:33 +020074 par.add_argument(
75 '--no-tree', dest='check_patch_use_tree', action='store_false',
76 help="Set `tree` to False")
77 par.add_argument(
Simon Glass22ce6412023-11-04 10:25:20 -060078 '--no-tags', action='store_false', dest='process_tags', default=True,
79 help="Don't process subject tags as aliases")
Simon Glass44ecdd42025-05-08 06:21:33 +020080 par.add_argument(
81 '--no-signoff', action='store_false', dest='add_signoff',
82 default=True, help="Don't add Signed-off-by to patches")
83 par.add_argument(
84 '--smtp-server', type=str,
85 help="Specify the SMTP server to 'git send-email'")
86 par.add_argument(
87 '--keep-change-id', action='store_true',
88 help='Preserve Change-Id tags in patches to send.')
Simon Glass22ce6412023-11-04 10:25:20 -060089
Simon Glass44ecdd42025-05-08 06:21:33 +020090
91def add_send_subparser(subparsers):
92 """Add the 'send' subparser
93
94 Args:
95 subparsers (argparse action): Subparser parent
96
97 Return:
98 ArgumentParser: send subparser
99 """
100 send = subparsers.add_parser(
101 'send', help='Format, check and email patches (default command)')
102 send.add_argument(
103 '-b', '--branch', type=str,
104 help="Branch to process (by default, the current branch)")
105 send.add_argument(
106 '-n', '--dry-run', action='store_true', dest='dry_run',
107 default=False, help="Do a dry run (create but don't email patches)")
108 send.add_argument(
109 '--cc-cmd', dest='cc_cmd', type=str, action='store',
110 default=None, help='Output cc list for patch file (used by git)')
111 add_send_args(send)
Simon Glass22ce6412023-11-04 10:25:20 -0600112 send.add_argument('patchfiles', nargs='*')
Simon Glasse55a11c2025-05-08 06:09:46 +0200113 return send
Simon Glass22ce6412023-11-04 10:25:20 -0600114
Simon Glasse55a11c2025-05-08 06:09:46 +0200115
116def add_status_subparser(subparsers):
117 """Add the 'status' subparser
118
119 Args:
120 subparsers (argparse action): Subparser parent
Simon Glass22ce6412023-11-04 10:25:20 -0600121
Simon Glasse55a11c2025-05-08 06:09:46 +0200122 Return:
123 ArgumentParser: status subparser
124 """
Simon Glass22ce6412023-11-04 10:25:20 -0600125 status = subparsers.add_parser('status',
126 help='Check status of patches in patchwork')
127 status.add_argument('-C', '--show-comments', action='store_true',
128 help='Show comments from each patch')
129 status.add_argument(
130 '-d', '--dest-branch', type=str,
131 help='Name of branch to create with collected responses')
132 status.add_argument('-f', '--force', action='store_true',
133 help='Force overwriting an existing branch')
Simon Glass802eeea2025-04-29 07:22:25 -0600134 status.add_argument('-T', '--single-thread', action='store_true',
135 help='Disable multithreading when reading patchwork')
Simon Glasse55a11c2025-05-08 06:09:46 +0200136 return status
137
138
139def setup_parser():
140 """Set up command-line parser
141
142 Returns:
143 argparse.Parser object
144 """
145 epilog = '''Create patches from commits in a branch, check them and email
146 them as specified by tags you place in the commits. Use -n to do a dry
147 run first.'''
148
149 parser = argparse.ArgumentParser(epilog=epilog)
Simon Glass44ecdd42025-05-08 06:21:33 +0200150 parser.add_argument(
151 '-D', '--debug', action='store_true',
Simon Glasse55a11c2025-05-08 06:09:46 +0200152 help='Enabling debugging (provides a full traceback on error)')
153 parser.add_argument(
154 '-N', '--no-capture', action='store_true',
155 help='Disable capturing of console output in tests')
156 parser.add_argument('-p', '--project', default=project.detect_project(),
157 help="Project name; affects default option values and "
158 "aliases [default: %(default)s]")
159 parser.add_argument('-P', '--patchwork-url',
160 default='https://patchwork.ozlabs.org',
161 help='URL of patchwork server [default: %(default)s]')
Simon Glass44ecdd42025-05-08 06:21:33 +0200162 parser.add_argument(
163 '-T', '--thread', action='store_true', dest='thread',
164 default=False, help='Create patches as a single thread')
Simon Glasse55a11c2025-05-08 06:09:46 +0200165 parser.add_argument(
166 '-v', '--verbose', action='store_true', dest='verbose', default=False,
167 help='Verbose output of errors and warnings')
168 parser.add_argument(
169 '-X', '--test-preserve-dirs', action='store_true',
170 help='Preserve and display test-created directories')
171 parser.add_argument(
172 '-H', '--full-help', action='store_true', dest='full_help',
173 default=False, help='Display the README file')
174
175 subparsers = parser.add_subparsers(dest='cmd')
176 add_send_subparser(subparsers)
177 add_status_subparser(subparsers)
178
179 # Only add the 'test' action if the test data files are available.
180 if HAS_TESTS:
181 test_parser = subparsers.add_parser('test', help='Run tests')
182 test_parser.add_argument('testname', type=str, default=None, nargs='?',
183 help="Specify the test to run")
184
Simon Glass8b521cf2025-05-08 05:58:10 +0200185 return parser
186
187
188def parse_args(argv=None, config_fname=None, parser=None):
189 """Parse command line arguments from sys.argv[]
190
191 Args:
192 argv (str or None): Arguments to process, or None to use sys.argv[1:]
193 config_fname (str): Config file to read, or None for default, or False
194 for an empty config
195
196 Returns:
197 tuple containing:
198 options: command line options
199 args: command lin arguments
200 """
201 if not parser:
202 parser = setup_parser()
Simon Glass22ce6412023-11-04 10:25:20 -0600203
204 # Parse options twice: first to get the project and second to handle
205 # defaults properly (which depends on project)
206 # Use parse_known_args() in case 'cmd' is omitted
Simon Glass8b521cf2025-05-08 05:58:10 +0200207 if not argv:
208 argv = sys.argv[1:]
209
Simon Glass22ce6412023-11-04 10:25:20 -0600210 args, rest = parser.parse_known_args(argv)
211 if hasattr(args, 'project'):
Simon Glass8b521cf2025-05-08 05:58:10 +0200212 settings.Setup(parser, args.project, argv, config_fname)
Simon Glass22ce6412023-11-04 10:25:20 -0600213 args, rest = parser.parse_known_args(argv)
214
215 # If we have a command, it is safe to parse all arguments
216 if args.cmd:
217 args = parser.parse_args(argv)
Simon Glassb8a14f92025-05-08 06:30:14 +0200218 elif not args.full_help:
Simon Glass22ce6412023-11-04 10:25:20 -0600219 # No command, so insert it after the known arguments and before the ones
220 # that presumably relate to the 'send' subcommand
221 nargs = len(rest)
222 argv = argv[:-nargs] + ['send'] + rest
223 args = parser.parse_args(argv)
224
225 return args