blob: 108fa52d8200eb5d72d907c484e3ece3bf2d4f18 [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 Glass0e43cad2025-05-10 13:04:55 +020047 default_arg = None
48 top_level = gitutil.get_top_level()
49 if top_level:
50 default_arg = os.path.join(top_level, 'scripts',
51 'get_maintainer.pl') + ' --norolestats'
Simon Glass44ecdd42025-05-08 06:21:33 +020052 par.add_argument(
Simon Glass22ce6412023-11-04 10:25:20 -060053 '--get-maintainer-script', dest='get_maintainer_script', type=str,
54 action='store',
Simon Glass0e43cad2025-05-10 13:04:55 +020055 default=default_arg,
Simon Glass22ce6412023-11-04 10:25:20 -060056 help='File name of the get_maintainer.pl (or compatible) script.')
Simon Glass44ecdd42025-05-08 06:21:33 +020057 par.add_argument(
58 '-r', '--in-reply-to', type=str, action='store',
59 help="Message ID that this series is in reply to")
60 par.add_argument(
61 '-s', '--start', dest='start', type=int, default=0,
62 help='Commit to start creating patches from (0 = HEAD)')
63 par.add_argument(
64 '-t', '--ignore-bad-tags', action='store_true', default=False,
65 help='Ignore bad tags / aliases (default=warn)')
66 par.add_argument(
67 '--no-binary', action='store_true', dest='ignore_binary',
68 default=False,
69 help="Do not output contents of changes in binary files")
70 par.add_argument(
71 '--no-check', action='store_false', dest='check_patch', default=True,
72 help="Don't check for patch compliance")
73 par.add_argument(
Simon Glass22ce6412023-11-04 10:25:20 -060074 '--tree', dest='check_patch_use_tree', default=False,
75 action='store_true',
76 help=("Set `tree` to True. If `tree` is False then we'll pass "
77 "'--no-tree' to checkpatch (default: tree=%(default)s)"))
Simon Glass44ecdd42025-05-08 06:21:33 +020078 par.add_argument(
79 '--no-tree', dest='check_patch_use_tree', action='store_false',
80 help="Set `tree` to False")
81 par.add_argument(
Simon Glass22ce6412023-11-04 10:25:20 -060082 '--no-tags', action='store_false', dest='process_tags', default=True,
83 help="Don't process subject tags as aliases")
Simon Glass44ecdd42025-05-08 06:21:33 +020084 par.add_argument(
85 '--no-signoff', action='store_false', dest='add_signoff',
86 default=True, help="Don't add Signed-off-by to patches")
87 par.add_argument(
88 '--smtp-server', type=str,
89 help="Specify the SMTP server to 'git send-email'")
90 par.add_argument(
91 '--keep-change-id', action='store_true',
92 help='Preserve Change-Id tags in patches to send.')
Simon Glass22ce6412023-11-04 10:25:20 -060093
Simon Glass44ecdd42025-05-08 06:21:33 +020094
95def add_send_subparser(subparsers):
96 """Add the 'send' subparser
97
98 Args:
99 subparsers (argparse action): Subparser parent
100
101 Return:
102 ArgumentParser: send subparser
103 """
104 send = subparsers.add_parser(
105 'send', help='Format, check and email patches (default command)')
106 send.add_argument(
107 '-b', '--branch', type=str,
108 help="Branch to process (by default, the current branch)")
109 send.add_argument(
110 '-n', '--dry-run', action='store_true', dest='dry_run',
111 default=False, help="Do a dry run (create but don't email patches)")
112 send.add_argument(
113 '--cc-cmd', dest='cc_cmd', type=str, action='store',
114 default=None, help='Output cc list for patch file (used by git)')
115 add_send_args(send)
Simon Glass22ce6412023-11-04 10:25:20 -0600116 send.add_argument('patchfiles', nargs='*')
Simon Glasse55a11c2025-05-08 06:09:46 +0200117 return send
Simon Glass22ce6412023-11-04 10:25:20 -0600118
Simon Glasse55a11c2025-05-08 06:09:46 +0200119
120def add_status_subparser(subparsers):
121 """Add the 'status' subparser
122
123 Args:
124 subparsers (argparse action): Subparser parent
Simon Glass22ce6412023-11-04 10:25:20 -0600125
Simon Glasse55a11c2025-05-08 06:09:46 +0200126 Return:
127 ArgumentParser: status subparser
128 """
Simon Glass22ce6412023-11-04 10:25:20 -0600129 status = subparsers.add_parser('status',
130 help='Check status of patches in patchwork')
131 status.add_argument('-C', '--show-comments', action='store_true',
132 help='Show comments from each patch')
133 status.add_argument(
134 '-d', '--dest-branch', type=str,
135 help='Name of branch to create with collected responses')
136 status.add_argument('-f', '--force', action='store_true',
137 help='Force overwriting an existing branch')
Simon Glass802eeea2025-04-29 07:22:25 -0600138 status.add_argument('-T', '--single-thread', action='store_true',
139 help='Disable multithreading when reading patchwork')
Simon Glasse55a11c2025-05-08 06:09:46 +0200140 return status
141
142
143def setup_parser():
144 """Set up command-line parser
145
146 Returns:
147 argparse.Parser object
148 """
149 epilog = '''Create patches from commits in a branch, check them and email
150 them as specified by tags you place in the commits. Use -n to do a dry
151 run first.'''
152
153 parser = argparse.ArgumentParser(epilog=epilog)
Simon Glass44ecdd42025-05-08 06:21:33 +0200154 parser.add_argument(
155 '-D', '--debug', action='store_true',
Simon Glasse55a11c2025-05-08 06:09:46 +0200156 help='Enabling debugging (provides a full traceback on error)')
157 parser.add_argument(
158 '-N', '--no-capture', action='store_true',
159 help='Disable capturing of console output in tests')
160 parser.add_argument('-p', '--project', default=project.detect_project(),
161 help="Project name; affects default option values and "
162 "aliases [default: %(default)s]")
163 parser.add_argument('-P', '--patchwork-url',
164 default='https://patchwork.ozlabs.org',
165 help='URL of patchwork server [default: %(default)s]')
Simon Glass44ecdd42025-05-08 06:21:33 +0200166 parser.add_argument(
167 '-T', '--thread', action='store_true', dest='thread',
168 default=False, help='Create patches as a single thread')
Simon Glasse55a11c2025-05-08 06:09:46 +0200169 parser.add_argument(
170 '-v', '--verbose', action='store_true', dest='verbose', default=False,
171 help='Verbose output of errors and warnings')
172 parser.add_argument(
173 '-X', '--test-preserve-dirs', action='store_true',
174 help='Preserve and display test-created directories')
175 parser.add_argument(
176 '-H', '--full-help', action='store_true', dest='full_help',
177 default=False, help='Display the README file')
178
179 subparsers = parser.add_subparsers(dest='cmd')
180 add_send_subparser(subparsers)
181 add_status_subparser(subparsers)
182
183 # Only add the 'test' action if the test data files are available.
184 if HAS_TESTS:
185 test_parser = subparsers.add_parser('test', help='Run tests')
186 test_parser.add_argument('testname', type=str, default=None, nargs='?',
187 help="Specify the test to run")
188
Simon Glass8b521cf2025-05-08 05:58:10 +0200189 return parser
190
191
192def parse_args(argv=None, config_fname=None, parser=None):
193 """Parse command line arguments from sys.argv[]
194
195 Args:
196 argv (str or None): Arguments to process, or None to use sys.argv[1:]
197 config_fname (str): Config file to read, or None for default, or False
198 for an empty config
199
200 Returns:
201 tuple containing:
202 options: command line options
203 args: command lin arguments
204 """
205 if not parser:
206 parser = setup_parser()
Simon Glass22ce6412023-11-04 10:25:20 -0600207
208 # Parse options twice: first to get the project and second to handle
209 # defaults properly (which depends on project)
210 # Use parse_known_args() in case 'cmd' is omitted
Simon Glass8b521cf2025-05-08 05:58:10 +0200211 if not argv:
212 argv = sys.argv[1:]
213
Simon Glass22ce6412023-11-04 10:25:20 -0600214 args, rest = parser.parse_known_args(argv)
215 if hasattr(args, 'project'):
Simon Glass8b521cf2025-05-08 05:58:10 +0200216 settings.Setup(parser, args.project, argv, config_fname)
Simon Glass22ce6412023-11-04 10:25:20 -0600217 args, rest = parser.parse_known_args(argv)
218
219 # If we have a command, it is safe to parse all arguments
220 if args.cmd:
221 args = parser.parse_args(argv)
Simon Glassb8a14f92025-05-08 06:30:14 +0200222 elif not args.full_help:
Simon Glass22ce6412023-11-04 10:25:20 -0600223 # No command, so insert it after the known arguments and before the ones
224 # that presumably relate to the 'send' subcommand
225 nargs = len(rest)
226 argv = argv[:-nargs] + ['send'] + rest
227 args = parser.parse_args(argv)
228
229 return args