Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 1 | """This is the main tool for handling llvm builds, bisects etc.""" |
| 2 | |
| 3 | import os |
Diana Picus | adb07c4 | 2017-11-22 16:12:57 +0100 | [diff] [blame] | 4 | from sys import argv |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 5 | from sys import exit |
| 6 | |
Diana Picus | 95226d4 | 2017-11-01 13:16:54 +0100 | [diff] [blame] | 7 | from modules.llvm import LLVMSubproject |
| 8 | from modules.llvm import LLVMSourceConfig |
| 9 | from modules.llvm import get_remote_branch |
| 10 | from modules.llvm import push_branch |
| 11 | |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 12 | from linaropy.git.clone import Clone |
| 13 | from linaropy.proj import Proj |
| 14 | |
| 15 | from argparse import Action, ArgumentParser, RawTextHelpFormatter |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 16 | from functools import partial |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 17 | |
| 18 | |
| 19 | def die(message, config_to_dump=None): |
| 20 | """Print an error message and exit.""" |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 21 | print(message) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 22 | |
| 23 | if config_to_dump is not None: |
| 24 | dump_config(config_to_dump) |
| 25 | |
| 26 | exit(1) |
| 27 | |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame] | 28 | |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame] | 29 | def get_worktree_root(env): |
Diana Picus | 81089db | 2017-05-05 22:26:49 +0200 | [diff] [blame] | 30 | """Get the path to the LLVM worktree corresponding to env.""" |
| 31 | return os.path.join(env, "llvm") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 32 | |
| 33 | |
| 34 | def dump_config(config): |
| 35 | """Dump the list of projects that are enabled in the given config.""" |
| 36 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 37 | print("Projects linked:") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 38 | enabled = config.get_enabled_subprojects() |
| 39 | if not enabled: |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 40 | print("none") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 41 | else: |
| 42 | for subproj in sorted(enabled): |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 43 | print(" + {}".format(subproj)) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 44 | |
| 45 | |
| 46 | def projects(args): |
| 47 | """Add/remove subprojects based on the values in args.""" |
| 48 | |
| 49 | proj = Proj() |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame] | 50 | |
| 51 | llvm_worktree_root = get_worktree_root(args.env) |
Diana Picus | 81089db | 2017-05-05 22:26:49 +0200 | [diff] [blame] | 52 | llvm_repos_root = args.repos |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 53 | config = LLVMSourceConfig(proj, llvm_worktree_root) |
| 54 | |
| 55 | if not args.add and not args.remove: |
| 56 | # Nothing to change, just print the current configuration |
| 57 | dump_config(config) |
| 58 | exit(0) |
| 59 | |
| 60 | to_add = {} |
| 61 | if args.add: |
| 62 | for subproj in args.add: |
| 63 | repo = Clone(proj, os.path.join(llvm_repos_root, subproj)) |
| 64 | to_add[subproj] = repo |
| 65 | |
| 66 | try: |
| 67 | config.update(to_add, args.remove) |
| 68 | except (EnvironmentError, ValueError) as exc: |
| 69 | die("Failed to update subprojects because:\n{}".format(str(exc))) |
| 70 | |
| 71 | dump_config(config) |
| 72 | |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 73 | |
Diana Picus | 95226d4 | 2017-11-01 13:16:54 +0100 | [diff] [blame] | 74 | def push_current_branch(args): |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 75 | """Push current branch to origin.""" |
| 76 | |
| 77 | proj = Proj() |
| 78 | |
| 79 | llvm_worktree_root = get_worktree_root(args.env) |
| 80 | config = LLVMSourceConfig(proj, llvm_worktree_root) |
| 81 | |
Diana Picus | 95226d4 | 2017-11-01 13:16:54 +0100 | [diff] [blame] | 82 | llvm_worktree = Clone(proj, llvm_worktree_root) |
| 83 | local_branch = llvm_worktree.getbranch() |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 84 | |
| 85 | try: |
Diana Picus | 95226d4 | 2017-11-01 13:16:54 +0100 | [diff] [blame] | 86 | remote_branch = get_remote_branch(llvm_worktree, local_branch) |
| 87 | config.for_each_enabled(partial(push_branch, proj, local_branch, |
| 88 | remote_branch)) |
| 89 | print("Pushed to {}".format(remote_branch)) |
| 90 | except (EnvironmentError, RuntimeError) as exc: |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 91 | die("Failed to push branch because: " + str(exc) + str(exc.__cause__)) |
| 92 | |
Diana Picus | 95226d4 | 2017-11-01 13:16:54 +0100 | [diff] [blame] | 93 | |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 94 | ########################################################################## |
| 95 | # Command line parsing # |
| 96 | ########################################################################## |
| 97 | |
| 98 | # If we decide we want shorthands for the subprojects, we can append to this |
| 99 | # list |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 100 | valid_subprojects = list(LLVMSubproject.get_all_subprojects().keys()) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 101 | |
| 102 | options = ArgumentParser(formatter_class=RawTextHelpFormatter) |
Diana Picus | 81089db | 2017-05-05 22:26:49 +0200 | [diff] [blame] | 103 | options.add_argument( |
| 104 | '--env', |
| 105 | required=True, |
| 106 | help="Path to the environment to update.") |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame] | 107 | |
Diana Picus | adb07c4 | 2017-11-22 16:12:57 +0100 | [diff] [blame] | 108 | subcommands = options.add_subparsers(dest="subcommand") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 109 | |
| 110 | # Subcommand for adding / removing subprojects |
Diana Picus | 36317e8 | 2017-10-31 15:35:24 +0100 | [diff] [blame] | 111 | projs = subcommands.add_parser( |
| 112 | "projects", help="Add/remove LLVM subprojects.\n" |
| 113 | "Adding a subproject will create a worktree for it " |
| 114 | "somewhere in the LLVM source tree, on the same git " |
| 115 | "branch as LLVM itself.\n" |
| 116 | "Removing a subproject will remove the worktree, but " |
| 117 | "not the underlying git branch.") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 118 | projs.set_defaults(run_command=projects) |
| 119 | |
| 120 | # TODO: Overwriting previous values is not necessarily what users expect (so for |
| 121 | # instance --add S1 S2 --remove S3 --add S4 would lead to adding only S4). We |
| 122 | # can do better by using action='append', which would create a list (of lists? |
| 123 | # or of lists and scalars?) that we can flatten to obtain all the values passed |
| 124 | # by the user. |
| 125 | projs.add_argument( |
| 126 | '-a', '--add', |
| 127 | nargs='+', |
| 128 | choices=valid_subprojects, |
| 129 | metavar='subproject', |
Diana Picus | 36317e8 | 2017-10-31 15:35:24 +0100 | [diff] [blame] | 130 | help="Enable given subprojects. Valid values are:\n\t{}\n".format( |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 131 | "\n\t".join(valid_subprojects))) |
| 132 | projs.add_argument( |
| 133 | '-r', '--remove', |
| 134 | nargs='+', |
| 135 | choices=valid_subprojects, |
| 136 | metavar='subproject', |
Diana Picus | 36317e8 | 2017-10-31 15:35:24 +0100 | [diff] [blame] | 137 | help="Disable given subprojects.") |
Diana Picus | adb07c4 | 2017-11-22 16:12:57 +0100 | [diff] [blame] | 138 | projs.add_argument( |
| 139 | '--repos', |
| 140 | help="Path to the directory containing the repositories for all LLVM " |
| 141 | "subprojects.") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 142 | |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 143 | # Subcommand for pushing the current branch to origin |
| 144 | push = subcommands.add_parser( |
| 145 | "push", |
Diana Picus | 36317e8 | 2017-10-31 15:35:24 +0100 | [diff] [blame] | 146 | help="Push current branch to origin linaro-local/<user>/<branch>, " |
| 147 | "for all enabled subprojects.") |
Diana Picus | 95226d4 | 2017-11-01 13:16:54 +0100 | [diff] [blame] | 148 | push.set_defaults(run_command=push_current_branch) |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 149 | |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 150 | args = options.parse_args() |
Diana Picus | adb07c4 | 2017-11-22 16:12:57 +0100 | [diff] [blame] | 151 | if args.subcommand == "projects" and args.add and not args.repos: |
| 152 | projs.error( |
| 153 | "When adding a subproject you must also pass the --repos argument") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 154 | args.run_command(args) |