Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
| 2 | # |
| 3 | # Copyright 2023 Google LLC |
| 4 | # |
| 5 | |
| 6 | """Handles parsing of buildman arguments |
| 7 | |
| 8 | This creates the argument parser and uses it to parse the arguments passed in |
| 9 | """ |
| 10 | |
| 11 | import argparse |
| 12 | import os |
| 13 | import pathlib |
| 14 | import sys |
| 15 | |
Simon Glass | ba1b3b9 | 2025-02-09 14:26:00 -0700 | [diff] [blame] | 16 | from u_boot_pylib import gitutil |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 17 | from patman import project |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 18 | from patman import settings |
| 19 | |
| 20 | PATMAN_DIR = pathlib.Path(__file__).parent |
| 21 | HAS_TESTS = os.path.exists(PATMAN_DIR / "func_test.py") |
| 22 | |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 23 | |
Simon Glass | d9ec747 | 2025-05-10 13:05:07 +0200 | [diff] [blame] | 24 | class ErrorCatchingArgumentParser(argparse.ArgumentParser): |
| 25 | def __init__(self, **kwargs): |
| 26 | self.exit_state = None |
| 27 | self.catch_error = False |
| 28 | super().__init__(**kwargs) |
| 29 | |
| 30 | def error(self, message): |
| 31 | if self.catch_error: |
| 32 | self.message = message |
| 33 | else: |
| 34 | super().error(message) |
| 35 | |
| 36 | def exit(self, status=0, message=None): |
| 37 | if self.catch_error: |
| 38 | self.exit_state = True |
| 39 | else: |
| 40 | super().exit(status, message) |
| 41 | |
| 42 | |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 43 | def add_send_args(par): |
| 44 | """Add arguments for the 'send' command |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 45 | |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 46 | Arguments: |
| 47 | par (ArgumentParser): Parser to add to |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 48 | """ |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 49 | par.add_argument( |
| 50 | '-c', '--count', dest='count', type=int, default=-1, |
| 51 | help='Automatically create patches from top n commits') |
| 52 | par.add_argument( |
| 53 | '-e', '--end', type=int, default=0, |
| 54 | help='Commits to skip at end of patch list') |
| 55 | par.add_argument( |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 56 | '-i', '--ignore-errors', action='store_true', |
| 57 | dest='ignore_errors', default=False, |
| 58 | help='Send patches email even if patch errors are found') |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 59 | par.add_argument( |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 60 | '-l', '--limit-cc', dest='limit', type=int, default=None, |
| 61 | help='Limit the cc list to LIMIT entries [default: %(default)s]') |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 62 | par.add_argument( |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 63 | '-m', '--no-maintainers', action='store_false', |
| 64 | dest='add_maintainers', default=True, |
| 65 | help="Don't cc the file maintainers automatically") |
Simon Glass | 0e43cad | 2025-05-10 13:04:55 +0200 | [diff] [blame] | 66 | default_arg = None |
| 67 | top_level = gitutil.get_top_level() |
| 68 | if top_level: |
| 69 | default_arg = os.path.join(top_level, 'scripts', |
| 70 | 'get_maintainer.pl') + ' --norolestats' |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 71 | par.add_argument( |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 72 | '--get-maintainer-script', dest='get_maintainer_script', type=str, |
| 73 | action='store', |
Simon Glass | 0e43cad | 2025-05-10 13:04:55 +0200 | [diff] [blame] | 74 | default=default_arg, |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 75 | help='File name of the get_maintainer.pl (or compatible) script.') |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 76 | par.add_argument( |
| 77 | '-r', '--in-reply-to', type=str, action='store', |
| 78 | help="Message ID that this series is in reply to") |
| 79 | par.add_argument( |
| 80 | '-s', '--start', dest='start', type=int, default=0, |
| 81 | help='Commit to start creating patches from (0 = HEAD)') |
| 82 | par.add_argument( |
| 83 | '-t', '--ignore-bad-tags', action='store_true', default=False, |
| 84 | help='Ignore bad tags / aliases (default=warn)') |
| 85 | par.add_argument( |
| 86 | '--no-binary', action='store_true', dest='ignore_binary', |
| 87 | default=False, |
| 88 | help="Do not output contents of changes in binary files") |
| 89 | par.add_argument( |
| 90 | '--no-check', action='store_false', dest='check_patch', default=True, |
| 91 | help="Don't check for patch compliance") |
| 92 | par.add_argument( |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 93 | '--tree', dest='check_patch_use_tree', default=False, |
| 94 | action='store_true', |
| 95 | help=("Set `tree` to True. If `tree` is False then we'll pass " |
| 96 | "'--no-tree' to checkpatch (default: tree=%(default)s)")) |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 97 | par.add_argument( |
| 98 | '--no-tree', dest='check_patch_use_tree', action='store_false', |
| 99 | help="Set `tree` to False") |
| 100 | par.add_argument( |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 101 | '--no-tags', action='store_false', dest='process_tags', default=True, |
| 102 | help="Don't process subject tags as aliases") |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 103 | par.add_argument( |
| 104 | '--no-signoff', action='store_false', dest='add_signoff', |
| 105 | default=True, help="Don't add Signed-off-by to patches") |
| 106 | par.add_argument( |
| 107 | '--smtp-server', type=str, |
| 108 | help="Specify the SMTP server to 'git send-email'") |
| 109 | par.add_argument( |
| 110 | '--keep-change-id', action='store_true', |
| 111 | help='Preserve Change-Id tags in patches to send.') |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 112 | |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 113 | |
Simon Glass | e9f14cd | 2025-05-10 13:05:08 +0200 | [diff] [blame^] | 114 | def _add_show_comments(parser): |
| 115 | parser.add_argument('-c', '--show-comments', action='store_true', |
| 116 | help='Show comments from each patch') |
| 117 | |
| 118 | |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 119 | def add_send_subparser(subparsers): |
| 120 | """Add the 'send' subparser |
| 121 | |
| 122 | Args: |
| 123 | subparsers (argparse action): Subparser parent |
| 124 | |
| 125 | Return: |
| 126 | ArgumentParser: send subparser |
| 127 | """ |
| 128 | send = subparsers.add_parser( |
| 129 | 'send', help='Format, check and email patches (default command)') |
| 130 | send.add_argument( |
| 131 | '-b', '--branch', type=str, |
| 132 | help="Branch to process (by default, the current branch)") |
| 133 | send.add_argument( |
| 134 | '-n', '--dry-run', action='store_true', dest='dry_run', |
| 135 | default=False, help="Do a dry run (create but don't email patches)") |
| 136 | send.add_argument( |
| 137 | '--cc-cmd', dest='cc_cmd', type=str, action='store', |
| 138 | default=None, help='Output cc list for patch file (used by git)') |
| 139 | add_send_args(send) |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 140 | send.add_argument('patchfiles', nargs='*') |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 141 | return send |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 142 | |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 143 | |
| 144 | def add_status_subparser(subparsers): |
| 145 | """Add the 'status' subparser |
| 146 | |
| 147 | Args: |
| 148 | subparsers (argparse action): Subparser parent |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 149 | |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 150 | Return: |
| 151 | ArgumentParser: status subparser |
| 152 | """ |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 153 | status = subparsers.add_parser('status', |
| 154 | help='Check status of patches in patchwork') |
Simon Glass | e9f14cd | 2025-05-10 13:05:08 +0200 | [diff] [blame^] | 155 | _add_show_comments(status) |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 156 | status.add_argument( |
| 157 | '-d', '--dest-branch', type=str, |
| 158 | help='Name of branch to create with collected responses') |
| 159 | status.add_argument('-f', '--force', action='store_true', |
| 160 | help='Force overwriting an existing branch') |
Simon Glass | 802eeea | 2025-04-29 07:22:25 -0600 | [diff] [blame] | 161 | status.add_argument('-T', '--single-thread', action='store_true', |
| 162 | help='Disable multithreading when reading patchwork') |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 163 | return status |
| 164 | |
| 165 | |
| 166 | def setup_parser(): |
| 167 | """Set up command-line parser |
| 168 | |
| 169 | Returns: |
| 170 | argparse.Parser object |
| 171 | """ |
| 172 | epilog = '''Create patches from commits in a branch, check them and email |
| 173 | them as specified by tags you place in the commits. Use -n to do a dry |
| 174 | run first.''' |
| 175 | |
Simon Glass | d9ec747 | 2025-05-10 13:05:07 +0200 | [diff] [blame] | 176 | parser = ErrorCatchingArgumentParser(epilog=epilog) |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 177 | parser.add_argument( |
| 178 | '-D', '--debug', action='store_true', |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 179 | help='Enabling debugging (provides a full traceback on error)') |
| 180 | parser.add_argument( |
| 181 | '-N', '--no-capture', action='store_true', |
| 182 | help='Disable capturing of console output in tests') |
| 183 | parser.add_argument('-p', '--project', default=project.detect_project(), |
| 184 | help="Project name; affects default option values and " |
| 185 | "aliases [default: %(default)s]") |
| 186 | parser.add_argument('-P', '--patchwork-url', |
| 187 | default='https://patchwork.ozlabs.org', |
| 188 | help='URL of patchwork server [default: %(default)s]') |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 189 | parser.add_argument( |
| 190 | '-T', '--thread', action='store_true', dest='thread', |
| 191 | default=False, help='Create patches as a single thread') |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 192 | parser.add_argument( |
| 193 | '-v', '--verbose', action='store_true', dest='verbose', default=False, |
| 194 | help='Verbose output of errors and warnings') |
| 195 | parser.add_argument( |
| 196 | '-X', '--test-preserve-dirs', action='store_true', |
| 197 | help='Preserve and display test-created directories') |
| 198 | parser.add_argument( |
| 199 | '-H', '--full-help', action='store_true', dest='full_help', |
| 200 | default=False, help='Display the README file') |
| 201 | |
| 202 | subparsers = parser.add_subparsers(dest='cmd') |
| 203 | add_send_subparser(subparsers) |
| 204 | add_status_subparser(subparsers) |
| 205 | |
| 206 | # Only add the 'test' action if the test data files are available. |
| 207 | if HAS_TESTS: |
| 208 | test_parser = subparsers.add_parser('test', help='Run tests') |
| 209 | test_parser.add_argument('testname', type=str, default=None, nargs='?', |
| 210 | help="Specify the test to run") |
| 211 | |
Simon Glass | 8b521cf | 2025-05-08 05:58:10 +0200 | [diff] [blame] | 212 | return parser |
| 213 | |
| 214 | |
| 215 | def parse_args(argv=None, config_fname=None, parser=None): |
| 216 | """Parse command line arguments from sys.argv[] |
| 217 | |
| 218 | Args: |
| 219 | argv (str or None): Arguments to process, or None to use sys.argv[1:] |
| 220 | config_fname (str): Config file to read, or None for default, or False |
| 221 | for an empty config |
| 222 | |
| 223 | Returns: |
| 224 | tuple containing: |
| 225 | options: command line options |
| 226 | args: command lin arguments |
| 227 | """ |
| 228 | if not parser: |
| 229 | parser = setup_parser() |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 230 | |
| 231 | # Parse options twice: first to get the project and second to handle |
| 232 | # defaults properly (which depends on project) |
| 233 | # Use parse_known_args() in case 'cmd' is omitted |
Simon Glass | 8b521cf | 2025-05-08 05:58:10 +0200 | [diff] [blame] | 234 | if not argv: |
| 235 | argv = sys.argv[1:] |
| 236 | |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 237 | args, rest = parser.parse_known_args(argv) |
| 238 | if hasattr(args, 'project'): |
Simon Glass | 8b521cf | 2025-05-08 05:58:10 +0200 | [diff] [blame] | 239 | settings.Setup(parser, args.project, argv, config_fname) |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 240 | args, rest = parser.parse_known_args(argv) |
| 241 | |
| 242 | # If we have a command, it is safe to parse all arguments |
| 243 | if args.cmd: |
| 244 | args = parser.parse_args(argv) |
Simon Glass | b8a14f9 | 2025-05-08 06:30:14 +0200 | [diff] [blame] | 245 | elif not args.full_help: |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 246 | # No command, so insert it after the known arguments and before the ones |
| 247 | # that presumably relate to the 'send' subcommand |
| 248 | nargs = len(rest) |
| 249 | argv = argv[:-nargs] + ['send'] + rest |
| 250 | args = parser.parse_args(argv) |
| 251 | |
| 252 | return args |