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 | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 24 | def add_send_args(par): |
| 25 | """Add arguments for the 'send' command |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 26 | |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 27 | Arguments: |
| 28 | par (ArgumentParser): Parser to add to |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 29 | """ |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 30 | 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 Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 37 | '-i', '--ignore-errors', action='store_true', |
| 38 | dest='ignore_errors', default=False, |
| 39 | help='Send patches email even if patch errors are found') |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 40 | par.add_argument( |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 41 | '-l', '--limit-cc', dest='limit', type=int, default=None, |
| 42 | help='Limit the cc list to LIMIT entries [default: %(default)s]') |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 43 | par.add_argument( |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 44 | '-m', '--no-maintainers', action='store_false', |
| 45 | dest='add_maintainers', default=True, |
| 46 | help="Don't cc the file maintainers automatically") |
Simon Glass | 0e43cad | 2025-05-10 13:04:55 +0200 | [diff] [blame] | 47 | 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 Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 52 | par.add_argument( |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 53 | '--get-maintainer-script', dest='get_maintainer_script', type=str, |
| 54 | action='store', |
Simon Glass | 0e43cad | 2025-05-10 13:04:55 +0200 | [diff] [blame] | 55 | default=default_arg, |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 56 | help='File name of the get_maintainer.pl (or compatible) script.') |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 57 | 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 Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 74 | '--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 Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 78 | 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 Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 82 | '--no-tags', action='store_false', dest='process_tags', default=True, |
| 83 | help="Don't process subject tags as aliases") |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 84 | 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 Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 93 | |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 94 | |
| 95 | def 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 Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 116 | send.add_argument('patchfiles', nargs='*') |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 117 | return send |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 118 | |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 119 | |
| 120 | def add_status_subparser(subparsers): |
| 121 | """Add the 'status' subparser |
| 122 | |
| 123 | Args: |
| 124 | subparsers (argparse action): Subparser parent |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 125 | |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 126 | Return: |
| 127 | ArgumentParser: status subparser |
| 128 | """ |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 129 | 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 Glass | 802eeea | 2025-04-29 07:22:25 -0600 | [diff] [blame] | 138 | status.add_argument('-T', '--single-thread', action='store_true', |
| 139 | help='Disable multithreading when reading patchwork') |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 140 | return status |
| 141 | |
| 142 | |
| 143 | def 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 Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 154 | parser.add_argument( |
| 155 | '-D', '--debug', action='store_true', |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 156 | 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 Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame] | 166 | parser.add_argument( |
| 167 | '-T', '--thread', action='store_true', dest='thread', |
| 168 | default=False, help='Create patches as a single thread') |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 169 | 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 Glass | 8b521cf | 2025-05-08 05:58:10 +0200 | [diff] [blame] | 189 | return parser |
| 190 | |
| 191 | |
| 192 | def 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 Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 207 | |
| 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 Glass | 8b521cf | 2025-05-08 05:58:10 +0200 | [diff] [blame] | 211 | if not argv: |
| 212 | argv = sys.argv[1:] |
| 213 | |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 214 | args, rest = parser.parse_known_args(argv) |
| 215 | if hasattr(args, 'project'): |
Simon Glass | 8b521cf | 2025-05-08 05:58:10 +0200 | [diff] [blame] | 216 | settings.Setup(parser, args.project, argv, config_fname) |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 217 | 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 Glass | b8a14f9 | 2025-05-08 06:30:14 +0200 | [diff] [blame] | 222 | elif not args.full_help: |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 223 | # 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 |