blob: e5ac4fb1684f8bc0552b609f09cdb0fd86d19e98 [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 Glass8b521cf2025-05-08 05:58:10 +020023def setup_parser():
24 """Set up command-line parser
Simon Glass22ce6412023-11-04 10:25:20 -060025
26 Returns:
Simon Glass8b521cf2025-05-08 05:58:10 +020027 argparse.Parser object
Simon Glass22ce6412023-11-04 10:25:20 -060028 """
29 epilog = '''Create patches from commits in a branch, check them and email
30 them as specified by tags you place in the commits. Use -n to do a dry
31 run first.'''
32
33 parser = argparse.ArgumentParser(epilog=epilog)
34 parser.add_argument('-b', '--branch', type=str,
35 help="Branch to process (by default, the current branch)")
36 parser.add_argument('-c', '--count', dest='count', type=int,
37 default=-1, help='Automatically create patches from top n commits')
38 parser.add_argument('-e', '--end', type=int, default=0,
39 help='Commits to skip at end of patch list')
40 parser.add_argument('-D', '--debug', action='store_true',
41 help='Enabling debugging (provides a full traceback on error)')
Simon Glassed831d12025-04-29 07:22:10 -060042 parser.add_argument(
43 '-N', '--no-capture', action='store_true',
44 help='Disable capturing of console output in tests')
Simon Glass22ce6412023-11-04 10:25:20 -060045 parser.add_argument('-p', '--project', default=project.detect_project(),
46 help="Project name; affects default option values and "
47 "aliases [default: %(default)s]")
48 parser.add_argument('-P', '--patchwork-url',
49 default='https://patchwork.ozlabs.org',
50 help='URL of patchwork server [default: %(default)s]')
51 parser.add_argument('-s', '--start', dest='start', type=int,
52 default=0, help='Commit to start creating patches from (0 = HEAD)')
53 parser.add_argument(
54 '-v', '--verbose', action='store_true', dest='verbose', default=False,
55 help='Verbose output of errors and warnings')
56 parser.add_argument(
Simon Glassed831d12025-04-29 07:22:10 -060057 '-X', '--test-preserve-dirs', action='store_true',
58 help='Preserve and display test-created directories')
59 parser.add_argument(
Simon Glass22ce6412023-11-04 10:25:20 -060060 '-H', '--full-help', action='store_true', dest='full_help',
61 default=False, help='Display the README file')
62
63 subparsers = parser.add_subparsers(dest='cmd')
64 send = subparsers.add_parser(
65 'send', help='Format, check and email patches (default command)')
66 send.add_argument('-i', '--ignore-errors', action='store_true',
67 dest='ignore_errors', default=False,
68 help='Send patches email even if patch errors are found')
69 send.add_argument('-l', '--limit-cc', dest='limit', type=int, default=None,
70 help='Limit the cc list to LIMIT entries [default: %(default)s]')
71 send.add_argument('-m', '--no-maintainers', action='store_false',
72 dest='add_maintainers', default=True,
73 help="Don't cc the file maintainers automatically")
74 send.add_argument(
75 '--get-maintainer-script', dest='get_maintainer_script', type=str,
76 action='store',
77 default=os.path.join(gitutil.get_top_level(), 'scripts',
78 'get_maintainer.pl') + ' --norolestats',
79 help='File name of the get_maintainer.pl (or compatible) script.')
80 send.add_argument('-n', '--dry-run', action='store_true', dest='dry_run',
81 default=False, help="Do a dry run (create but don't email patches)")
82 send.add_argument('-r', '--in-reply-to', type=str, action='store',
83 help="Message ID that this series is in reply to")
84 send.add_argument('-t', '--ignore-bad-tags', action='store_true',
85 default=False,
86 help='Ignore bad tags / aliases (default=warn)')
87 send.add_argument('-T', '--thread', action='store_true', dest='thread',
88 default=False, help='Create patches as a single thread')
89 send.add_argument('--cc-cmd', dest='cc_cmd', type=str, action='store',
90 default=None, help='Output cc list for patch file (used by git)')
91 send.add_argument('--no-binary', action='store_true', dest='ignore_binary',
92 default=False,
93 help="Do not output contents of changes in binary files")
94 send.add_argument('--no-check', action='store_false', dest='check_patch',
95 default=True,
96 help="Don't check for patch compliance")
97 send.add_argument(
98 '--tree', dest='check_patch_use_tree', default=False,
99 action='store_true',
100 help=("Set `tree` to True. If `tree` is False then we'll pass "
101 "'--no-tree' to checkpatch (default: tree=%(default)s)"))
102 send.add_argument('--no-tree', dest='check_patch_use_tree',
103 action='store_false', help="Set `tree` to False")
104 send.add_argument(
105 '--no-tags', action='store_false', dest='process_tags', default=True,
106 help="Don't process subject tags as aliases")
107 send.add_argument('--no-signoff', action='store_false', dest='add_signoff',
108 default=True, help="Don't add Signed-off-by to patches")
109 send.add_argument('--smtp-server', type=str,
110 help="Specify the SMTP server to 'git send-email'")
111 send.add_argument('--keep-change-id', action='store_true',
112 help='Preserve Change-Id tags in patches to send.')
113
114 send.add_argument('patchfiles', nargs='*')
115
116 # Only add the 'test' action if the test data files are available.
117 if HAS_TESTS:
118 test_parser = subparsers.add_parser('test', help='Run tests')
119 test_parser.add_argument('testname', type=str, default=None, nargs='?',
120 help="Specify the test to run")
121
122 status = subparsers.add_parser('status',
123 help='Check status of patches in patchwork')
124 status.add_argument('-C', '--show-comments', action='store_true',
125 help='Show comments from each patch')
126 status.add_argument(
127 '-d', '--dest-branch', type=str,
128 help='Name of branch to create with collected responses')
129 status.add_argument('-f', '--force', action='store_true',
130 help='Force overwriting an existing branch')
Simon Glass802eeea2025-04-29 07:22:25 -0600131 status.add_argument('-T', '--single-thread', action='store_true',
132 help='Disable multithreading when reading patchwork')
Simon Glass8b521cf2025-05-08 05:58:10 +0200133 return parser
134
135
136def parse_args(argv=None, config_fname=None, parser=None):
137 """Parse command line arguments from sys.argv[]
138
139 Args:
140 argv (str or None): Arguments to process, or None to use sys.argv[1:]
141 config_fname (str): Config file to read, or None for default, or False
142 for an empty config
143
144 Returns:
145 tuple containing:
146 options: command line options
147 args: command lin arguments
148 """
149 if not parser:
150 parser = setup_parser()
Simon Glass22ce6412023-11-04 10:25:20 -0600151
152 # Parse options twice: first to get the project and second to handle
153 # defaults properly (which depends on project)
154 # Use parse_known_args() in case 'cmd' is omitted
Simon Glass8b521cf2025-05-08 05:58:10 +0200155 if not argv:
156 argv = sys.argv[1:]
157
Simon Glass22ce6412023-11-04 10:25:20 -0600158 args, rest = parser.parse_known_args(argv)
159 if hasattr(args, 'project'):
Simon Glass8b521cf2025-05-08 05:58:10 +0200160 settings.Setup(parser, args.project, argv, config_fname)
Simon Glass22ce6412023-11-04 10:25:20 -0600161 args, rest = parser.parse_known_args(argv)
162
163 # If we have a command, it is safe to parse all arguments
164 if args.cmd:
165 args = parser.parse_args(argv)
166 else:
167 # No command, so insert it after the known arguments and before the ones
168 # that presumably relate to the 'send' subcommand
169 nargs = len(rest)
170 argv = argv[:-nargs] + ['send'] + rest
171 args = parser.parse_args(argv)
172
173 return args