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 | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame^] | 47 | par.add_argument( |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 48 | '--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 Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame^] | 53 | 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 Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 70 | '--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 Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame^] | 74 | 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 Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 78 | '--no-tags', action='store_false', dest='process_tags', default=True, |
| 79 | help="Don't process subject tags as aliases") |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame^] | 80 | 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 Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 89 | |
Simon Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame^] | 90 | |
| 91 | def 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 Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 112 | send.add_argument('patchfiles', nargs='*') |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 113 | return send |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 114 | |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 115 | |
| 116 | def add_status_subparser(subparsers): |
| 117 | """Add the 'status' subparser |
| 118 | |
| 119 | Args: |
| 120 | subparsers (argparse action): Subparser parent |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 121 | |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 122 | Return: |
| 123 | ArgumentParser: status subparser |
| 124 | """ |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 125 | 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 Glass | 802eeea | 2025-04-29 07:22:25 -0600 | [diff] [blame] | 134 | status.add_argument('-T', '--single-thread', action='store_true', |
| 135 | help='Disable multithreading when reading patchwork') |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 136 | return status |
| 137 | |
| 138 | |
| 139 | def 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 Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame^] | 150 | parser.add_argument( |
| 151 | '-D', '--debug', action='store_true', |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 152 | 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 Glass | 44ecdd4 | 2025-05-08 06:21:33 +0200 | [diff] [blame^] | 162 | parser.add_argument( |
| 163 | '-T', '--thread', action='store_true', dest='thread', |
| 164 | default=False, help='Create patches as a single thread') |
Simon Glass | e55a11c | 2025-05-08 06:09:46 +0200 | [diff] [blame] | 165 | 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 Glass | 8b521cf | 2025-05-08 05:58:10 +0200 | [diff] [blame] | 185 | return parser |
| 186 | |
| 187 | |
| 188 | def 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 Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 203 | |
| 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 Glass | 8b521cf | 2025-05-08 05:58:10 +0200 | [diff] [blame] | 207 | if not argv: |
| 208 | argv = sys.argv[1:] |
| 209 | |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 210 | args, rest = parser.parse_known_args(argv) |
| 211 | if hasattr(args, 'project'): |
Simon Glass | 8b521cf | 2025-05-08 05:58:10 +0200 | [diff] [blame] | 212 | settings.Setup(parser, args.project, argv, config_fname) |
Simon Glass | 22ce641 | 2023-11-04 10:25:20 -0600 | [diff] [blame] | 213 | 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) |
| 218 | else: |
| 219 | # 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 |