Bug 1839315: part 4) Link from `SheetLoadData::mWasAlternate` to spec. r=emilio DONTBUILD
[gecko.git] / tools / tryselect / cli.py
blob11d1867e47cca1abc6d0d85e0a7d5941d1599263
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 import os
7 import subprocess
8 import tempfile
9 from argparse import ArgumentParser
11 from .task_config import all_task_configs
13 COMMON_ARGUMENT_GROUPS = {
14 "push": [
16 ["-m", "--message"],
18 "const": "editor",
19 "default": "{msg}",
20 "nargs": "?",
21 "help": "Use the specified commit message, or create it in your "
22 "$EDITOR if blank. Defaults to computed message.",
26 ["--closed-tree"],
28 "action": "store_true",
29 "default": False,
30 "help": "Push despite a closed try tree",
34 ["--push-to-lando"],
36 "action": "store_true",
37 "default": False,
38 "help": "Submit changes for Lando to push to try.",
42 "preset": [
44 ["--save"],
46 "default": None,
47 "help": "Save selection for future use with --preset.",
51 ["--preset"],
53 "default": None,
54 "help": "Load a saved selection.",
58 ["--list-presets"],
60 "action": "store_const",
61 "dest": "preset_action",
62 "const": "list",
63 "default": None,
64 "help": "List available preset selections.",
68 ["--edit-presets"],
70 "action": "store_const",
71 "dest": "preset_action",
72 "const": "edit",
73 "default": None,
74 "help": "Edit the preset file.",
78 "task": [
80 ["--full"],
82 "action": "store_true",
83 "default": False,
84 "help": "Use the full set of tasks as input to fzf (instead of "
85 "target tasks).",
89 ["-p", "--parameters"],
91 "default": None,
92 "help": "Use the given parameters.yml to generate tasks, "
93 "defaults to a default set of parameters",
99 NO_PUSH_ARGUMENT_GROUP = [
101 ["--stage-changes"],
103 "dest": "stage_changes",
104 "action": "store_true",
105 "help": "Locally stage changes created by this command but do not "
106 "push to try.",
110 ["--no-push"],
112 "dest": "dry_run",
113 "action": "store_true",
114 "help": "Do not push to try as a result of running this command (if "
115 "specified this command will only print calculated try "
116 "syntax and selection info and not change files).",
122 class BaseTryParser(ArgumentParser):
123 name = "try"
124 common_groups = ["push", "preset"]
125 arguments = []
126 task_configs = []
128 def __init__(self, *args, **kwargs):
129 ArgumentParser.__init__(self, *args, **kwargs)
131 group = self.add_argument_group("{} arguments".format(self.name))
132 for cli, kwargs in self.arguments:
133 group.add_argument(*cli, **kwargs)
135 for name in self.common_groups:
136 group = self.add_argument_group("{} arguments".format(name))
137 arguments = COMMON_ARGUMENT_GROUPS[name]
139 # Preset arguments are all mutually exclusive.
140 if name == "preset":
141 group = group.add_mutually_exclusive_group()
143 for cli, kwargs in arguments:
144 group.add_argument(*cli, **kwargs)
146 if name == "push":
147 group_no_push = group.add_mutually_exclusive_group()
148 arguments = NO_PUSH_ARGUMENT_GROUP
149 for cli, kwargs in arguments:
150 group_no_push.add_argument(*cli, **kwargs)
152 group = self.add_argument_group("task configuration arguments")
153 self.task_configs = {c: all_task_configs[c]() for c in self.task_configs}
154 for cfg in self.task_configs.values():
155 cfg.add_arguments(group)
157 def validate(self, args):
158 if hasattr(args, "message"):
159 if args.message == "editor":
160 if "EDITOR" not in os.environ:
161 self.error(
162 "must set the $EDITOR environment variable to use blank --message"
165 with tempfile.NamedTemporaryFile(mode="r") as fh:
166 subprocess.call([os.environ["EDITOR"], fh.name])
167 args.message = fh.read().strip()
169 if "{msg}" not in args.message:
170 args.message = "{}\n\n{}".format(args.message, "{msg}")
172 def parse_known_args(self, *args, **kwargs):
173 args, remainder = ArgumentParser.parse_known_args(self, *args, **kwargs)
174 self.validate(args)
175 return args, remainder