blob: cb4e68c7cd85a1a7050e1e55aba75107fd16189c [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 Glass22ce6412023-11-04 10:25:20 -060016from patman import project
Simon Glassba1b3b92025-02-09 14:26:00 -070017from u_boot_pylib import gitutil
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 Glasse55a11c2025-05-08 06:09:46 +020024def add_send_subparser(subparsers):
25 """Add the 'send' subparser
Simon Glass22ce6412023-11-04 10:25:20 -060026
Simon Glasse55a11c2025-05-08 06:09:46 +020027 Args:
28 subparsers (argparse action): Subparser parent
Simon Glass22ce6412023-11-04 10:25:20 -060029
Simon Glasse55a11c2025-05-08 06:09:46 +020030 Return:
31 ArgumentParser: send subparser
32 """
Simon Glass22ce6412023-11-04 10:25:20 -060033 send = subparsers.add_parser(
34 'send', help='Format, check and email patches (default command)')
Simon Glasse55a11c2025-05-08 06:09:46 +020035 send.add_argument(
36 '-i', '--ignore-errors', action='store_true',
37 dest='ignore_errors', default=False,
38 help='Send patches email even if patch errors are found')
39 send.add_argument(
40 '-l', '--limit-cc', dest='limit', type=int, default=None,
41 help='Limit the cc list to LIMIT entries [default: %(default)s]')
42 send.add_argument(
43 '-m', '--no-maintainers', action='store_false',
44 dest='add_maintainers', default=True,
45 help="Don't cc the file maintainers automatically")
Simon Glass22ce6412023-11-04 10:25:20 -060046 send.add_argument(
47 '--get-maintainer-script', dest='get_maintainer_script', type=str,
48 action='store',
49 default=os.path.join(gitutil.get_top_level(), 'scripts',
50 'get_maintainer.pl') + ' --norolestats',
51 help='File name of the get_maintainer.pl (or compatible) script.')
Simon Glasse55a11c2025-05-08 06:09:46 +020052 send.add_argument(
53 '-n', '--dry-run', action='store_true', dest='dry_run',
54 default=False, help="Do a dry run (create but don't email patches)")
Simon Glass22ce6412023-11-04 10:25:20 -060055 send.add_argument('-r', '--in-reply-to', type=str, action='store',
56 help="Message ID that this series is in reply to")
57 send.add_argument('-t', '--ignore-bad-tags', action='store_true',
58 default=False,
59 help='Ignore bad tags / aliases (default=warn)')
60 send.add_argument('-T', '--thread', action='store_true', dest='thread',
61 default=False, help='Create patches as a single thread')
Simon Glasse55a11c2025-05-08 06:09:46 +020062 send.add_argument(
63 '--cc-cmd', dest='cc_cmd', type=str, action='store',
64 default=None, help='Output cc list for patch file (used by git)')
Simon Glass22ce6412023-11-04 10:25:20 -060065 send.add_argument('--no-binary', action='store_true', dest='ignore_binary',
66 default=False,
67 help="Do not output contents of changes in binary files")
68 send.add_argument('--no-check', action='store_false', dest='check_patch',
69 default=True,
70 help="Don't check for patch compliance")
71 send.add_argument(
72 '--tree', dest='check_patch_use_tree', default=False,
73 action='store_true',
74 help=("Set `tree` to True. If `tree` is False then we'll pass "
75 "'--no-tree' to checkpatch (default: tree=%(default)s)"))
76 send.add_argument('--no-tree', dest='check_patch_use_tree',
77 action='store_false', help="Set `tree` to False")
78 send.add_argument(
79 '--no-tags', action='store_false', dest='process_tags', default=True,
80 help="Don't process subject tags as aliases")
81 send.add_argument('--no-signoff', action='store_false', dest='add_signoff',
82 default=True, help="Don't add Signed-off-by to patches")
83 send.add_argument('--smtp-server', type=str,
84 help="Specify the SMTP server to 'git send-email'")
85 send.add_argument('--keep-change-id', action='store_true',
86 help='Preserve Change-Id tags in patches to send.')
87
88 send.add_argument('patchfiles', nargs='*')
Simon Glasse55a11c2025-05-08 06:09:46 +020089 return send
Simon Glass22ce6412023-11-04 10:25:20 -060090
Simon Glasse55a11c2025-05-08 06:09:46 +020091
92def add_status_subparser(subparsers):
93 """Add the 'status' subparser
94
95 Args:
96 subparsers (argparse action): Subparser parent
Simon Glass22ce6412023-11-04 10:25:20 -060097
Simon Glasse55a11c2025-05-08 06:09:46 +020098 Return:
99 ArgumentParser: status subparser
100 """
Simon Glass22ce6412023-11-04 10:25:20 -0600101 status = subparsers.add_parser('status',
102 help='Check status of patches in patchwork')
103 status.add_argument('-C', '--show-comments', action='store_true',
104 help='Show comments from each patch')
105 status.add_argument(
106 '-d', '--dest-branch', type=str,
107 help='Name of branch to create with collected responses')
108 status.add_argument('-f', '--force', action='store_true',
109 help='Force overwriting an existing branch')
Simon Glass802eeea2025-04-29 07:22:25 -0600110 status.add_argument('-T', '--single-thread', action='store_true',
111 help='Disable multithreading when reading patchwork')
Simon Glasse55a11c2025-05-08 06:09:46 +0200112 return status
113
114
115def setup_parser():
116 """Set up command-line parser
117
118 Returns:
119 argparse.Parser object
120 """
121 epilog = '''Create patches from commits in a branch, check them and email
122 them as specified by tags you place in the commits. Use -n to do a dry
123 run first.'''
124
125 parser = argparse.ArgumentParser(epilog=epilog)
126 parser.add_argument('-b', '--branch', type=str,
127 help="Branch to process (by default, the current branch)")
128 parser.add_argument('-c', '--count', dest='count', type=int,
129 default=-1, help='Automatically create patches from top n commits')
130 parser.add_argument('-e', '--end', type=int, default=0,
131 help='Commits to skip at end of patch list')
132 parser.add_argument('-D', '--debug', action='store_true',
133 help='Enabling debugging (provides a full traceback on error)')
134 parser.add_argument(
135 '-N', '--no-capture', action='store_true',
136 help='Disable capturing of console output in tests')
137 parser.add_argument('-p', '--project', default=project.detect_project(),
138 help="Project name; affects default option values and "
139 "aliases [default: %(default)s]")
140 parser.add_argument('-P', '--patchwork-url',
141 default='https://patchwork.ozlabs.org',
142 help='URL of patchwork server [default: %(default)s]')
143 parser.add_argument('-s', '--start', dest='start', type=int,
144 default=0, help='Commit to start creating patches from (0 = HEAD)')
145 parser.add_argument(
146 '-v', '--verbose', action='store_true', dest='verbose', default=False,
147 help='Verbose output of errors and warnings')
148 parser.add_argument(
149 '-X', '--test-preserve-dirs', action='store_true',
150 help='Preserve and display test-created directories')
151 parser.add_argument(
152 '-H', '--full-help', action='store_true', dest='full_help',
153 default=False, help='Display the README file')
154
155 subparsers = parser.add_subparsers(dest='cmd')
156 add_send_subparser(subparsers)
157 add_status_subparser(subparsers)
158
159 # Only add the 'test' action if the test data files are available.
160 if HAS_TESTS:
161 test_parser = subparsers.add_parser('test', help='Run tests')
162 test_parser.add_argument('testname', type=str, default=None, nargs='?',
163 help="Specify the test to run")
164
Simon Glass8b521cf2025-05-08 05:58:10 +0200165 return parser
166
167
168def parse_args(argv=None, config_fname=None, parser=None):
169 """Parse command line arguments from sys.argv[]
170
171 Args:
172 argv (str or None): Arguments to process, or None to use sys.argv[1:]
173 config_fname (str): Config file to read, or None for default, or False
174 for an empty config
175
176 Returns:
177 tuple containing:
178 options: command line options
179 args: command lin arguments
180 """
181 if not parser:
182 parser = setup_parser()
Simon Glass22ce6412023-11-04 10:25:20 -0600183
184 # Parse options twice: first to get the project and second to handle
185 # defaults properly (which depends on project)
186 # Use parse_known_args() in case 'cmd' is omitted
Simon Glass8b521cf2025-05-08 05:58:10 +0200187 if not argv:
188 argv = sys.argv[1:]
189
Simon Glass22ce6412023-11-04 10:25:20 -0600190 args, rest = parser.parse_known_args(argv)
191 if hasattr(args, 'project'):
Simon Glass8b521cf2025-05-08 05:58:10 +0200192 settings.Setup(parser, args.project, argv, config_fname)
Simon Glass22ce6412023-11-04 10:25:20 -0600193 args, rest = parser.parse_known_args(argv)
194
195 # If we have a command, it is safe to parse all arguments
196 if args.cmd:
197 args = parser.parse_args(argv)
198 else:
199 # No command, so insert it after the known arguments and before the ones
200 # that presumably relate to the 'send' subcommand
201 nargs = len(rest)
202 argv = argv[:-nargs] + ['send'] + rest
203 args = parser.parse_args(argv)
204
205 return args