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