2017-08-15 18:39:46 +03:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Templates provide a way of modifying the task definition of selected
|
|
|
|
tasks. They live under taskcluster/taskgraph/templates.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
|
|
|
|
|
|
|
import os
|
2017-08-17 18:13:50 +03:00
|
|
|
from abc import ABCMeta, abstractmethod
|
2017-11-07 18:27:44 +03:00
|
|
|
from argparse import Action
|
2017-08-15 18:39:46 +03:00
|
|
|
|
|
|
|
from mozbuild.base import BuildEnvironmentNotFoundException, MozbuildObject
|
|
|
|
|
|
|
|
here = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
|
|
|
|
|
|
|
|
class Template(object):
|
|
|
|
__metaclass__ = ABCMeta
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def context(self, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Artifact(Template):
|
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
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:
|
2018-01-16 00:05:17 +03:00
|
|
|
return {
|
|
|
|
'artifact': {'enabled': '1'}
|
|
|
|
}
|
2017-08-15 18:39:46 +03:00
|
|
|
|
|
|
|
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")
|
2018-01-16 00:05:17 +03:00
|
|
|
return {
|
|
|
|
'artifact': {'enabled': '1'}
|
|
|
|
}
|
2017-08-15 18:39:46 +03:00
|
|
|
except BuildEnvironmentNotFoundException:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-08-25 22:24:22 +03:00
|
|
|
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
|
2018-01-16 00:05:17 +03:00
|
|
|
return {
|
|
|
|
'env': dict(e.split('=', 1) for e in env),
|
|
|
|
}
|
2017-08-25 22:24:22 +03:00
|
|
|
|
|
|
|
|
2017-11-07 18:27:44 +03:00
|
|
|
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)
|
|
|
|
|
|
|
|
def __call__(self, parser, namespace, values, option_string=None):
|
|
|
|
name = option_string or self.dest
|
|
|
|
if values < self.min:
|
|
|
|
parser.error('{} can not be less than {}'.format(name, self.min))
|
|
|
|
if values > self.max:
|
|
|
|
parser.error('{} can not be more than {}'.format(name, self.max))
|
|
|
|
setattr(namespace, self.dest, values)
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-01-16 00:05:17 +03:00
|
|
|
return {
|
|
|
|
'rebuild': rebuild,
|
|
|
|
}
|
2017-11-07 18:27:44 +03:00
|
|
|
|
|
|
|
|
2017-08-15 18:39:46 +03:00
|
|
|
all_templates = {
|
|
|
|
'artifact': Artifact,
|
2017-08-25 22:24:22 +03:00
|
|
|
'env': Environment,
|
2017-11-07 18:27:44 +03:00
|
|
|
'rebuild': Rebuild,
|
2017-08-15 18:39:46 +03:00
|
|
|
}
|