diff options
Diffstat (limited to 'tools/patman/control.py')
-rw-r--r-- | tools/patman/control.py | 424 |
1 files changed, 255 insertions, 169 deletions
diff --git a/tools/patman/control.py b/tools/patman/control.py index b8a45912058..3e09b16e87b 100644 --- a/tools/patman/control.py +++ b/tools/patman/control.py @@ -8,186 +8,47 @@ This module provides various functions called by the main program to implement the features of patman. """ -import os -import sys +import re +import traceback + +try: + from importlib import resources +except ImportError: + # for Python 3.6 + import importlib_resources as resources -from patman import checkpatch -from patman import patchstream from u_boot_pylib import gitutil from u_boot_pylib import terminal +from u_boot_pylib import tools +from u_boot_pylib import tout +from patman import cseries +from patman import cser_helper +from patman import patchstream +from patman.patchwork import Patchwork +from patman import send +from patman import settings def setup(): """Do required setup before doing anything""" gitutil.setup() + alias_fname = gitutil.get_alias_file() + if alias_fname: + settings.ReadGitAliases(alias_fname) -def prepare_patches(col, branch, count, start, end, ignore_binary, signoff, - keep_change_id=False): - """Figure out what patches to generate, then generate them - - The patch files are written to the current directory, e.g. 0001_xxx.patch - 0002_yyy.patch - - Args: - col (terminal.Color): Colour output object - branch (str): Branch to create patches from (None = current) - count (int): Number of patches to produce, or -1 to produce patches for - the current branch back to the upstream commit - start (int): Start partch to use (0=first / top of branch) - end (int): End patch to use (0=last one in series, 1=one before that, - etc.) - ignore_binary (bool): Don't generate patches for binary files - keep_change_id (bool): Preserve the Change-Id tag. - - Returns: - Tuple: - Series object for this series (set of patches) - Filename of the cover letter as a string (None if none) - patch_files: List of patch filenames, each a string, e.g. - ['0001_xxx.patch', '0002_yyy.patch'] - """ - if count == -1: - # Work out how many patches to send if we can - count = (gitutil.count_commits_to_branch(branch) - start) - - if not count: - str = 'No commits found to process - please use -c flag, or run:\n' \ - ' git branch --set-upstream-to remote/branch' - sys.exit(col.build(col.RED, str)) - - # Read the metadata from the commits - to_do = count - end - series = patchstream.get_metadata(branch, start, to_do) - cover_fname, patch_files = gitutil.create_patches( - branch, start, to_do, ignore_binary, series, signoff) - - # Fix up the patch files to our liking, and insert the cover letter - patchstream.fix_patches(series, patch_files, keep_change_id, - insert_base_commit=not cover_fname) - if cover_fname and series.get('cover'): - patchstream.insert_cover_letter(cover_fname, series, to_do) - return series, cover_fname, patch_files - - -def check_patches(series, patch_files, run_checkpatch, verbose, use_tree): - """Run some checks on a set of patches - - This santiy-checks the patman tags like Series-version and runs the patches - through checkpatch - - Args: - series (Series): Series object for this series (set of patches) - patch_files (list): List of patch filenames, each a string, e.g. - ['0001_xxx.patch', '0002_yyy.patch'] - run_checkpatch (bool): True to run checkpatch.pl - verbose (bool): True to print out every line of the checkpatch output as - it is parsed - use_tree (bool): If False we'll pass '--no-tree' to checkpatch. - - Returns: - bool: True if the patches had no errors, False if they did - """ - # Do a few checks on the series - series.DoChecks() - - # Check the patches - if run_checkpatch: - ok = checkpatch.check_patches(verbose, patch_files, use_tree) - else: - ok = True - return ok - - -def email_patches(col, series, cover_fname, patch_files, process_tags, its_a_go, - ignore_bad_tags, add_maintainers, get_maintainer_script, limit, - dry_run, in_reply_to, thread, smtp_server): - """Email patches to the recipients - - This emails out the patches and cover letter using 'git send-email'. Each - patch is copied to recipients identified by the patch tag and output from - the get_maintainer.pl script. The cover letter is copied to all recipients - of any patch. - - To make this work a CC file is created holding the recipients for each patch - and the cover letter. See the main program 'cc_cmd' for this logic. - - Args: - col (terminal.Color): Colour output object - series (Series): Series object for this series (set of patches) - cover_fname (str): Filename of the cover letter as a string (None if - none) - patch_files (list): List of patch filenames, each a string, e.g. - ['0001_xxx.patch', '0002_yyy.patch'] - process_tags (bool): True to process subject tags in each patch, e.g. - for 'dm: spi: Add SPI support' this would be 'dm' and 'spi'. The - tags are looked up in the configured sendemail.aliasesfile and also - in ~/.patman (see README) - its_a_go (bool): True if we are going to actually send the patches, - False if the patches have errors and will not be sent unless - @ignore_errors - ignore_bad_tags (bool): True to just print a warning for unknown tags, - False to halt with an error - add_maintainers (bool): Run the get_maintainer.pl script for each patch - get_maintainer_script (str): The script used to retrieve which - maintainers to cc - limit (int): Limit on the number of people that can be cc'd on a single - patch or the cover letter (None if no limit) - dry_run (bool): Don't actually email the patches, just print out what - would be sent - in_reply_to (str): If not None we'll pass this to git as --in-reply-to. - Should be a message ID that this is in reply to. - thread (bool): True to add --thread to git send-email (make all patches - reply to cover-letter or first patch in series) - smtp_server (str): SMTP server to use to send patches (None for default) - """ - cc_file = series.MakeCcFile(process_tags, cover_fname, not ignore_bad_tags, - add_maintainers, limit, get_maintainer_script) - - # Email the patches out (giving the user time to check / cancel) - cmd = '' - if its_a_go: - cmd = gitutil.email_patches( - series, cover_fname, patch_files, dry_run, not ignore_bad_tags, - cc_file, in_reply_to=in_reply_to, thread=thread, - smtp_server=smtp_server) - else: - print(col.build(col.RED, "Not sending emails due to errors/warnings")) - - # For a dry run, just show our actions as a sanity check - if dry_run: - series.ShowActions(patch_files, cmd, process_tags) - if not its_a_go: - print(col.build(col.RED, "Email would not be sent")) - - os.remove(cc_file) - -def send(args): +def do_send(args): """Create, check and send patches by email Args: args (argparse.Namespace): Arguments to patman """ setup() - col = terminal.Color() - series, cover_fname, patch_files = prepare_patches( - col, args.branch, args.count, args.start, args.end, - args.ignore_binary, args.add_signoff, - keep_change_id=args.keep_change_id) - ok = check_patches(series, patch_files, args.check_patch, - args.verbose, args.check_patch_use_tree) - - ok = ok and gitutil.check_suppress_cc_config() - - its_a_go = ok or args.ignore_errors - email_patches( - col, series, cover_fname, patch_files, args.process_tags, - its_a_go, args.ignore_bad_tags, args.add_maintainers, - args.get_maintainer_script, args.limit, args.dry_run, - args.in_reply_to, args.thread, args.smtp_server) + send.send(args) + def patchwork_status(branch, count, start, end, dest_branch, force, - show_comments, url): + show_comments, url, single_thread=False): """Check the status of patches in patchwork This finds the series in patchwork using the Series-link tag, checks for new @@ -212,9 +73,11 @@ def patchwork_status(branch, count, start, end, dest_branch, force, Raises: ValueError: if the branch has no Series-link value """ + if not branch: + branch = gitutil.get_branch() if count == -1: # Work out how many patches to send if we can - count = (gitutil.count_commits_to_branch(branch) - start) + count = gitutil.count_commits_to_branch(branch) - start series = patchstream.get_metadata(branch, start, count - end) warnings = 0 @@ -231,17 +94,240 @@ def patchwork_status(branch, count, start, end, dest_branch, force, if not links: raise ValueError("Branch has no Series-links value") - # Find the link without a version number (we don't support versions yet) - found = [link for link in links.split() if not ':' in link] - if not found: - raise ValueError('Series-links has no current version (without :)') + _, version = cser_helper.split_name_version(branch) + link = series.get_link_for_version(version, links) + if not link: + raise ValueError('Series-links has no link for v{version}') + tout.debug(f"Link '{link}") # Allow the series to override the URL if 'patchwork_url' in series: url = series.patchwork_url + pwork = Patchwork(url, single_thread=single_thread) # Import this here to avoid failing on other commands if the dependencies # are not present from patman import status - status.check_patchwork_status(series, found[0], branch, dest_branch, force, - show_comments, url) + pwork = Patchwork(url) + status.check_and_show_status(series, link, branch, dest_branch, force, + show_comments, False, pwork) + + +def do_series(args, test_db=None, pwork=None, cser=None): + """Process a series subcommand + + Args: + args (Namespace): Arguments to process + test_db (str or None): Directory containing the test database, None to + use the normal one + pwork (Patchwork): Patchwork object to use, None to create one if + needed + cser (Cseries): Cseries object to use, None to create one + """ + if not cser: + cser = cseries.Cseries(test_db) + needs_patchwork = [ + 'autolink', 'autolink-all', 'open', 'send', 'status', 'gather', + 'gather-all' + ] + try: + cser.open_database() + if args.subcmd in needs_patchwork: + if not pwork: + pwork = Patchwork(args.patchwork_url) + proj = cser.project_get() + if not proj: + raise ValueError( + "Please set project ID with 'patman patchwork set-project'") + _, proj_id, link_name = cser.project_get() + pwork.project_set(proj_id, link_name) + elif pwork and pwork is not True: + raise ValueError( + f"Internal error: command '{args.subcmd}' should not have patchwork") + if args.subcmd == 'add': + cser.add(args.series, args.desc, mark=args.mark, + allow_unmarked=args.allow_unmarked, end=args.upstream, + dry_run=args.dry_run) + elif args.subcmd == 'archive': + cser.archive(args.series) + elif args.subcmd == 'autolink': + cser.link_auto(pwork, args.series, args.version, args.update, + args.autolink_wait) + elif args.subcmd == 'autolink-all': + cser.link_auto_all(pwork, update_commit=args.update, + link_all_versions=args.link_all_versions, + replace_existing=args.replace_existing, + dry_run=args.dry_run, show_summary=True) + elif args.subcmd == 'dec': + cser.decrement(args.series, args.dry_run) + elif args.subcmd == 'gather': + cser.gather(pwork, args.series, args.version, args.show_comments, + args.show_cover_comments, args.gather_tags, + dry_run=args.dry_run) + elif args.subcmd == 'gather-all': + cser.gather_all( + pwork, args.show_comments, args.show_cover_comments, + args.gather_all_versions, args.gather_tags, args.dry_run) + elif args.subcmd == 'get-link': + link = cser.link_get(args.series, args.version) + print(link) + elif args.subcmd == 'inc': + cser.increment(args.series, args.dry_run) + elif args.subcmd == 'ls': + cser.series_list() + elif args.subcmd == 'open': + cser.open(pwork, args.series, args.version) + elif args.subcmd == 'mark': + cser.mark(args.series, args.allow_marked, dry_run=args.dry_run) + elif args.subcmd == 'patches': + cser.list_patches(args.series, args.version, args.commit, + args.patch) + elif args.subcmd == 'progress': + cser.progress(args.series, args.show_all_versions, + args.list_patches) + elif args.subcmd == 'rm': + cser.remove(args.series, dry_run=args.dry_run) + elif args.subcmd == 'rm-version': + cser.version_remove(args.series, args.version, dry_run=args.dry_run) + elif args.subcmd == 'rename': + cser.rename(args.series, args.new_name, dry_run=args.dry_run) + elif args.subcmd == 'scan': + cser.scan(args.series, mark=args.mark, + allow_unmarked=args.allow_unmarked, end=args.upstream, + dry_run=args.dry_run) + elif args.subcmd == 'send': + cser.send(pwork, args.series, args.autolink, args.autolink_wait, + args) + elif args.subcmd == 'set-link': + cser.link_set(args.series, args.version, args.link, args.update) + elif args.subcmd == 'status': + cser.status(pwork, args.series, args.version, args.show_comments, + args.show_cover_comments) + elif args.subcmd == 'summary': + cser.summary(args.series) + elif args.subcmd == 'unarchive': + cser.unarchive(args.series) + elif args.subcmd == 'unmark': + cser.unmark(args.series, args.allow_unmarked, dry_run=args.dry_run) + elif args.subcmd == 'version-change': + cser.version_change(args.series, args.version, args.new_version, + dry_run=args.dry_run) + else: + raise ValueError(f"Unknown series subcommand '{args.subcmd}'") + finally: + cser.close_database() + + +def upstream(args, test_db=None): + """Process an 'upstream' subcommand + + Args: + args (Namespace): Arguments to process + test_db (str or None): Directory containing the test database, None to + use the normal one + """ + cser = cseries.Cseries(test_db) + try: + cser.open_database() + if args.subcmd == 'add': + cser.upstream_add(args.remote_name, args.url) + elif args.subcmd == 'default': + if args.unset: + cser.upstream_set_default(None) + elif args.remote_name: + cser.upstream_set_default(args.remote_name) + else: + result = cser.upstream_get_default() + print(result if result else 'unset') + elif args.subcmd == 'delete': + cser.upstream_delete(args.remote_name) + elif args.subcmd == 'list': + cser.upstream_list() + else: + raise ValueError(f"Unknown upstream subcommand '{args.subcmd}'") + finally: + cser.close_database() + + +def patchwork(args, test_db=None, pwork=None): + """Process a 'patchwork' subcommand + Args: + args (Namespace): Arguments to process + test_db (str or None): Directory containing the test database, None to + use the normal one + pwork (Patchwork): Patchwork object to use + """ + cser = cseries.Cseries(test_db) + try: + cser.open_database() + if args.subcmd == 'set-project': + if not pwork: + pwork = Patchwork(args.patchwork_url) + cser.project_set(pwork, args.project_name) + elif args.subcmd == 'get-project': + info = cser.project_get() + if not info: + raise ValueError("Project has not been set; use 'patman patchwork set-project'") + name, pwid, link_name = info + print(f"Project '{name}' patchwork-ID {pwid} link-name {link_name}") + else: + raise ValueError(f"Unknown patchwork subcommand '{args.subcmd}'") + finally: + cser.close_database() + +def do_patman(args, test_db=None, pwork=None, cser=None): + """Process a patman command + + Args: + args (Namespace): Arguments to process + test_db (str or None): Directory containing the test database, None to + use the normal one + pwork (Patchwork): Patchwork object to use, or None to create one + cser (Cseries): Cseries object to use when executing the command, + or None to create one + """ + if args.full_help: + with resources.path('patman', 'README.rst') as readme: + tools.print_full_help(str(readme)) + return 0 + if args.cmd == 'send': + # Called from git with a patch filename as argument + # Printout a list of additional CC recipients for this patch + if args.cc_cmd: + re_line = re.compile(r'(\S*) (.*)') + with open(args.cc_cmd, 'r', encoding='utf-8') as inf: + for line in inf.readlines(): + match = re_line.match(line) + if match and match.group(1) == args.patchfiles[0]: + for cca in match.group(2).split('\0'): + cca = cca.strip() + if cca: + print(cca) + else: + # If we are not processing tags, no need to warning about bad ones + if not args.process_tags: + args.ignore_bad_tags = True + do_send(args) + return 0 + + ret_code = 0 + try: + # Check status of patches in patchwork + if args.cmd == 'status': + patchwork_status(args.branch, args.count, args.start, args.end, + args.dest_branch, args.force, args.show_comments, + args.patchwork_url) + elif args.cmd == 'series': + do_series(args, test_db, pwork, cser) + elif args.cmd == 'upstream': + upstream(args, test_db) + elif args.cmd == 'patchwork': + patchwork(args, test_db, pwork) + except Exception as exc: + terminal.tprint(f'patman: {type(exc).__name__}: {exc}', + colour=terminal.Color.RED) + if args.debug: + print() + traceback.print_exc() + ret_code = 1 + return ret_code |