1# This Source Code Form is subject to the terms of the Mozilla Public License,
2# v. 2.0. If a copy of the MPL was not distributed with this file, You can
3# obtain one at http://mozilla.org/MPL/2.0/.
4
5from __future__ import absolute_import, print_function, unicode_literals
6
7import copy
8import logging
9
10from . import transform
11from ..util.yaml import load_yaml
12
13logger = logging.getLogger(__name__)
14
15
16class PostBuildTask(transform.TransformTask):
17    """
18    A task implementing a post-build job.  These depend on jobs and perform
19    various followup tasks after a build has completed.
20
21    The `only-for-build-platforms` kind configuration, if specified, will limit
22    the build platforms for which a post-build task will be created.
23
24    The `job-template' kind configuration points to a yaml file which will
25    be used to create the input to the transforms.  It will have added to it
26    keys `build-label`, the label for the build task, and `build-platform`, its
27    platform.
28    """
29
30    @classmethod
31    def get_inputs(cls, kind, path, config, params, loaded_tasks):
32        if config.get('kind-dependencies', []) != ["build"]:
33            raise Exception("PostBuildTask kinds must depend on builds")
34
35        only_platforms = config.get('only-for-build-platforms')
36        prototype = load_yaml(path, config.get('job-template'))
37
38        for task in loaded_tasks:
39            if task.kind != 'build':
40                continue
41
42            build_platform = task.attributes.get('build_platform')
43            build_type = task.attributes.get('build_type')
44            if not build_platform or not build_type:
45                continue
46            platform = "{}/{}".format(build_platform, build_type)
47            if only_platforms and platform not in only_platforms:
48                continue
49
50            post_task = copy.deepcopy(prototype)
51            post_task['build-label'] = task.label
52            post_task['build-platform'] = platform
53            yield post_task
54