Bug 1318200 - Obtain target tasks method from parameters; r=dustin
authorGregory Szorc <gps@mozilla.com>
Thu, 17 Nov 2016 16:29:51 -0800
changeset 898726 1fbab277e984c006d8b67ebb0c6a1abffb7b743a
parent 898725 734ec3d6b7d0db9d9401c7ce8373199ffcbd6475
child 898727 a34c214911ad19a23f3e56b067f83c6c1ce85bf1
push id155986
push userdharvey@mozilla.com
push dateMon, 21 Nov 2016 15:45:47 +0000
treeherdertry@31662330c674 [default view] [failures only]
reviewersdustin
bugs1318200
milestone53.0a1
Bug 1318200 - Obtain target tasks method from parameters; r=dustin Previously, all callers outside of tests that passed "target_tasks_method" to TaskGraphGenerator all used the same pattern of looking for a key in the parameters and calling a function in the target_tasks module. Future commits will refactor how target tasks graph work. To make the transition easier, we move the logic for obtaining the target tasks method into TaskGraphGenerator. MozReview-Commit-ID: 3QU09iGhoXh
taskcluster/mach_commands.py
taskcluster/taskgraph/decision.py
taskcluster/taskgraph/generator.py
taskcluster/taskgraph/test/test_generator.py
--- a/taskcluster/mach_commands.py
+++ b/taskcluster/mach_commands.py
@@ -219,22 +219,19 @@ class MachCommands(MachCommandBase):
         import taskgraph.target_tasks
         import taskgraph.generator
 
         try:
             self.setup_logging(quiet=options['quiet'], verbose=options['verbose'])
             parameters = taskgraph.parameters.load_parameters_file(options)
             parameters.check()
 
-            target_tasks_method = parameters.get('target_tasks_method', 'all_tasks')
-            target_tasks_method = taskgraph.target_tasks.get_method(target_tasks_method)
             tgg = taskgraph.generator.TaskGraphGenerator(
                 root_dir=options['root'],
-                parameters=parameters,
-                target_tasks_method=target_tasks_method)
+                parameters=parameters)
 
             tg = getattr(tgg, graph_attr)
 
             show_method = getattr(self, 'show_taskgraph_' + (options['format'] or 'labels'))
             tg = self.get_filtered_taskgraph(tg, options["tasks_regex"])
             show_method(tg)
         except Exception:
             traceback.print_exc()
--- a/taskcluster/taskgraph/decision.py
+++ b/taskcluster/taskgraph/decision.py
@@ -11,17 +11,16 @@ import json
 import logging
 
 import time
 import yaml
 
 from .generator import TaskGraphGenerator
 from .create import create_tasks
 from .parameters import Parameters
-from .target_tasks import get_method
 from .taskgraph import TaskGraph
 
 from taskgraph.util.templates import Templates
 from taskgraph.util.time import (
     json_time_from_now,
     current_json_time,
 )
 
@@ -74,22 +73,19 @@ def taskgraph_decision(options):
        taskgraph` commands do
      * generating a set of artifacts to memorialize the graph
      * calling TaskCluster APIs to create the graph
     """
 
     parameters = get_decision_parameters(options)
 
     # create a TaskGraphGenerator instance
-    target_tasks_method = parameters.get('target_tasks_method', 'all_tasks')
-    target_tasks_method = get_method(target_tasks_method)
     tgg = TaskGraphGenerator(
         root_dir=options['root'],
-        parameters=parameters,
-        target_tasks_method=target_tasks_method)
+        parameters=parameters)
 
     # write out the parameters used to generate this graph
     write_artifact('parameters.yml', dict(**parameters))
 
     # write out the yml file for action tasks
     write_artifact('action.yml', get_action_yml(parameters))
 
     # write out the full graph for reference
--- a/taskcluster/taskgraph/generator.py
+++ b/taskcluster/taskgraph/generator.py
@@ -3,16 +3,17 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 from __future__ import absolute_import, print_function, unicode_literals
 import logging
 import os
 import yaml
 
 from .graph import Graph
+from .target_tasks import get_method
 from .taskgraph import TaskGraph
 from .optimize import optimize_task_graph
 from .util.python_path import find_object
 
 logger = logging.getLogger(__name__)
 
 
 class Kind(object):
@@ -46,30 +47,27 @@ class TaskGraphGenerator(object):
     various phases of generation, is available via properties.  This encourages
     the provision of all generation inputs at instance construction time.
     """
 
     # Task-graph generation is implemented as a Python generator that yields
     # each "phase" of generation.  This allows some mach subcommands to short-
     # circuit generation of the entire graph by never completing the generator.
 
-    def __init__(self, root_dir, parameters,
-                 target_tasks_method):
+    def __init__(self, root_dir, parameters):
         """
         @param root_dir: root directory, with subdirectories for each kind
         @param parameters: parameters for this task-graph generation
         @type parameters: dict
-        @param target_tasks_method: function to determine the target_task_set;
-                see `./target_tasks.py`.
-        @type target_tasks_method: function
         """
-
         self.root_dir = root_dir
         self.parameters = parameters
-        self.target_tasks_method = target_tasks_method
+
+        target_tasks_method = parameters.get('target_tasks_method', 'all_tasks')
+        self.target_tasks_method = get_method(target_tasks_method)
 
         # this can be set up until the time the target task set is generated;
         # it defaults to parameters['target_tasks']
         self._target_tasks = parameters.get('target_tasks')
 
         # start the generator
         self._run = self._run()
         self._run_results = {}
--- a/taskcluster/taskgraph/test/test_generator.py
+++ b/taskcluster/taskgraph/test/test_generator.py
@@ -2,17 +2,17 @@
 # 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/.
 
 from __future__ import absolute_import, print_function, unicode_literals
 
 import unittest
 
 from ..generator import TaskGraphGenerator, Kind
-from .. import graph
+from .. import graph, target_tasks as target_tasks_mod
 from ..task import base
 from mozunit import main
 
 
 class FakeTask(base.Task):
 
     def __init__(self, **kwargs):
         self.i = kwargs.pop('i')
@@ -60,17 +60,25 @@ class WithFakeKind(TaskGraphGenerator):
 class TestGenerator(unittest.TestCase):
 
     def maketgg(self, target_tasks=None, kinds=[('fake', [])]):
         FakeKind.loaded_kinds = []
         self.target_tasks = target_tasks or []
 
         def target_tasks_method(full_task_graph, parameters):
             return self.target_tasks
-        return WithFakeKind('/root', {'kinds': kinds}, target_tasks_method)
+
+        target_tasks_mod._target_task_methods['test_method'] = target_tasks_method
+
+        parameters = {
+            'kinds': kinds,
+            'target_tasks_method': 'test_method',
+        }
+
+        return WithFakeKind('/root', parameters)
 
     def test_kind_ordering(self):
         "When task kinds depend on each other, they are loaded in postorder"
         self.tgg = self.maketgg(kinds=[
             ('fake3', ['fake2', 'fake1']),
             ('fake2', ['fake1']),
             ('fake1', []),
         ])