Bug 1413928 - [tryselect] Change templates to return an entire context dict instead of a single value
This changes templates so they return an entire context dict instead of
only returning context based on their name. For example, now the 'path'
template can set context for 'env'.
A side effect of this is that there is no longer a 1-to-1 mapping of templates
in tryselect and taskgraph.
MozReview-Commit-ID: IHRXXi5mB4G
--- a/tools/tryselect/cli.py
+++ b/tools/tryselect/cli.py
@@ -99,14 +99,14 @@ class BaseTryParser(ArgumentParser):
args.message = '{}\n\n{}'.format(args.message, '{msg}')
def parse_known_args(self, *args, **kwargs):
args, remainder = ArgumentParser.parse_known_args(self, *args, **kwargs)
self.validate(args)
if self.templates:
args.templates = {}
- for name, cls in self.templates.iteritems():
+ for cls in self.templates.itervalues():
context = cls.context(**vars(args))
if context is not None:
- args.templates[name] = context
+ args.templates.update(context)
return args, remainder
--- a/tools/tryselect/templates.py
+++ b/tools/tryselect/templates.py
@@ -36,41 +36,47 @@ class Artifact(Template):
group = parser.add_mutually_exclusive_group()
group.add_argument('--artifact', action='store_true',
help='Force artifact builds where possible.')
group.add_argument('--no-artifact', action='store_true',
help='Disable artifact builds even if being used locally.')
def context(self, artifact, no_artifact, **kwargs):
if artifact:
- return {'enabled': '1'}
+ return {
+ 'artifact': {'enabled': '1'}
+ }
if no_artifact:
return
build = MozbuildObject.from_environment(cwd=here)
try:
if build.substs.get("MOZ_ARTIFACT_BUILDS"):
print("Artifact builds enabled, pass --no-artifact to disable")
- return {'enabled': '1'}
+ return {
+ 'artifact': {'enabled': '1'}
+ }
except BuildEnvironmentNotFoundException:
pass
class Environment(Template):
def add_arguments(self, parser):
parser.add_argument('--env', action='append', default=None,
help='Set an environment variable, of the form FOO=BAR. '
'Can be passed in multiple times.')
def context(self, env, **kwargs):
if not env:
return
- return dict(e.split('=', 1) for e in env)
+ return {
+ 'env': dict(e.split('=', 1) for e in env),
+ }
class RangeAction(Action):
def __init__(self, min, max, *args, **kwargs):
self.min = min
self.max = max
kwargs['metavar'] = '[{}-{}]'.format(self.min, self.max)
super(RangeAction, self).__init__(*args, **kwargs)
@@ -89,16 +95,18 @@ class Rebuild(Template):
def add_arguments(self, parser):
parser.add_argument('--rebuild', action=RangeAction, min=2, max=20, default=None, type=int,
help='Rebuild all selected tasks the specified number of times.')
def context(self, rebuild, **kwargs):
if not rebuild:
return
- return rebuild
+ return {
+ 'rebuild': rebuild,
+ }
all_templates = {
'artifact': Artifact,
'env': Environment,
'rebuild': Rebuild,
}
--- a/tools/tryselect/test/test_templates.py
+++ b/tools/tryselect/test/test_templates.py
@@ -12,25 +12,25 @@ import pytest
from tryselect.templates import all_templates
# templates have a list of tests of the form (input, expected)
TEMPLATE_TESTS = {
'artifact': [
(['--no-artifact'], None),
- (['--artifact'], {'enabled': '1'}),
+ (['--artifact'], {'artifact': {'enabled': '1'}}),
],
'env': [
([], None),
- (['--env', 'foo=bar', '--env', 'num=10'], {'foo': 'bar', 'num': '10'}),
+ (['--env', 'foo=bar', '--env', 'num=10'], {'env': {'foo': 'bar', 'num': '10'}}),
],
'rebuild': [
([], None),
- (['--rebuild', '10'], 10),
+ (['--rebuild', '10'], {'rebuild': 10}),
(['--rebuild', '1'], SystemExit),
(['--rebuild', '21'], SystemExit),
],
}
def test_templates(template, args, expected):
parser = ArgumentParser()