Bug 1718787 [wpt PR 29544] - Add web platform tests for cookie size requirements...
[gecko.git] / tools / tryselect / cli.py
blobe51b656ddde6c2262ee81597e7f22ff573ccd980
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
14 COMMON_ARGUMENT_GROUPS = {
15 "push": [
17 ["-m", "--message"],
19 "const": "editor",
20 "default": "{msg}",
21 "nargs": "?",
22 "help": "Use the specified commit message, or create it in your "
23 "$EDITOR if blank. Defaults to computed message.",
27 ["--no-push"],
29 "dest": "push",
30 "action": "store_false",
31 "help": "Do not push to try as a result of running this command (if "
32 "specified this command will only print calculated try "
33 "syntax and selection info).",
37 ["--closed-tree"],
39 "action": "store_true",
40 "default": False,
41 "help": "Push despite a closed try tree",
45 "preset": [
47 ["--save"],
49 "default": None,
50 "help": "Save selection for future use with --preset.",
54 ["--preset"],
56 "default": None,
57 "help": "Load a saved selection.",
61 ["--list-presets"],
63 "action": "store_const",
64 "dest": "preset_action",
65 "const": "list",
66 "default": None,
67 "help": "List available preset selections.",
71 ["--edit-presets"],
73 "action": "store_const",
74 "dest": "preset_action",
75 "const": "edit",
76 "default": None,
77 "help": "Edit the preset file.",
81 "task": [
83 ["--full"],
85 "action": "store_true",
86 "default": False,
87 "help": "Use the full set of tasks as input to fzf (instead of "
88 "target tasks).",
92 ["-p", "--parameters"],
94 "default": None,
95 "help": "Use the given parameters.yml to generate tasks, "
96 "defaults to a default set of parameters",
103 class BaseTryParser(ArgumentParser):
104 name = "try"
105 common_groups = ["push", "preset"]
106 arguments = []
107 task_configs = []
109 def __init__(self, *args, **kwargs):
110 ArgumentParser.__init__(self, *args, **kwargs)
112 group = self.add_argument_group("{} arguments".format(self.name))
113 for cli, kwargs in self.arguments:
114 group.add_argument(*cli, **kwargs)
116 for name in self.common_groups:
117 group = self.add_argument_group("{} arguments".format(name))
118 arguments = COMMON_ARGUMENT_GROUPS[name]
120 # Preset arguments are all mutually exclusive.
121 if name == "preset":
122 group = group.add_mutually_exclusive_group()
124 for cli, kwargs in arguments:
125 group.add_argument(*cli, **kwargs)
127 group = self.add_argument_group("task configuration arguments")
128 self.task_configs = {c: all_task_configs[c]() for c in self.task_configs}
129 for cfg in self.task_configs.values():
130 cfg.add_arguments(group)
132 def validate(self, args):
133 if hasattr(args, "message"):
134 if args.message == "editor":
135 if "EDITOR" not in os.environ:
136 self.error(
137 "must set the $EDITOR environment variable to use blank --message"
140 with tempfile.NamedTemporaryFile(mode="r") as fh:
141 subprocess.call([os.environ["EDITOR"], fh.name])
142 args.message = fh.read().strip()
144 if "{msg}" not in args.message:
145 args.message = "{}\n\n{}".format(args.message, "{msg}")
147 def parse_known_args(self, *args, **kwargs):
148 args, remainder = ArgumentParser.parse_known_args(self, *args, **kwargs)
149 self.validate(args)
150 return args, remainder