Bug 1286075: factor load_yaml into a util module; r=gps
authorDustin J. Mitchell <dustin@mozilla.com>
Tue, 06 Sep 2016 18:01:27 +0000
changeset 354983 ddce0af84cbd65950a71e9851e086f3e0fc0050b
parent 354982 f6ab21ff2b5b78eeea3d603e48e333d3e576ef5e
child 354984 b56e8d1370480cba2eed56541d2b0ddbd775d6fd
push id6570
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:26:13 +0000
treeherdermozilla-beta@f455459b2ae5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgps
bugs1286075
milestone51.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1286075: factor load_yaml into a util module; r=gps MozReview-Commit-ID: DPWZWslcphy
taskcluster/taskgraph/task/test.py
taskcluster/taskgraph/task/transform.py
taskcluster/taskgraph/test/test_util_yaml.py
taskcluster/taskgraph/util/templates.py
taskcluster/taskgraph/util/yaml.py
--- a/taskcluster/taskgraph/task/test.py
+++ b/taskcluster/taskgraph/task/test.py
@@ -1,20 +1,19 @@
 # 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/.
 
 from __future__ import absolute_import, print_function, unicode_literals
 
 import copy
 import logging
-import os
-import yaml
 
 from . import transform
+from ..util.yaml import load_yaml
 
 logger = logging.getLogger(__name__)
 
 
 class TestTask(transform.TransformTask):
     """
     A task implementing a Gecko test.
     """
@@ -109,15 +108,8 @@ class TestTask(transform.TransformTask):
             if test_set not in test_sets_cfg:
                 raise Exception(
                     "Test set '{}' for test platform {} is not defined".format(
                         test_set, test_platform))
             test_names = test_sets_cfg[test_set]
             rv[test_platform] = cfg.copy()
             rv[test_platform]['test-names'] = test_names
         return rv
-
-
-def load_yaml(path, name):
-    """Convenience method to load a YAML file in the kind directory"""
-    filename = os.path.join(path, name)
-    with open(filename, "rb") as f:
-        return yaml.load(f)
--- a/taskcluster/taskgraph/task/transform.py
+++ b/taskcluster/taskgraph/task/transform.py
@@ -1,20 +1,19 @@
 # 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/.
 
 from __future__ import absolute_import, print_function, unicode_literals
 
 import logging
-import os
-import yaml
 
 from . import base
 from ..util.python_path import find_object
+from ..util.yaml import load_yaml
 from ..transforms.base import TransformSequence, TransformConfig
 
 logger = logging.getLogger(__name__)
 
 
 class TransformTask(base.Task):
     """
     Tasks of this class are generated by applying transformations to a sequence
@@ -67,15 +66,8 @@ class TransformTask(base.Task):
         super(TransformTask, self).__init__(kind, task['label'],
                                             task['attributes'], task['task'])
 
     def get_dependencies(self, taskgraph):
         return [(label, name) for name, label in self.dependencies.items()]
 
     def optimize(self):
         return False, None
-
-
-def load_yaml(path, name):
-    """Convenience method to load a YAML file in the kind directory"""
-    filename = os.path.join(path, name)
-    with open(filename, "rb") as f:
-        return yaml.load(f)
new file mode 100644
--- /dev/null
+++ b/taskcluster/taskgraph/test/test_util_yaml.py
@@ -0,0 +1,23 @@
+# 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/.
+
+from __future__ import absolute_import, print_function, unicode_literals
+
+import unittest
+
+from ..util import yaml
+from mozunit import MockedOpen
+
+FOO_YML = """\
+prop:
+    - val1
+"""
+
+
+class TestYaml(unittest.TestCase):
+
+    def test_load(self):
+        with MockedOpen({'/dir1/dir2/foo.yml': FOO_YML}):
+            self.assertEqual(yaml.load_yaml("/dir1/dir2", "foo.yml"),
+                             {'prop': ['val1']})
--- a/taskcluster/taskgraph/util/templates.py
+++ b/taskcluster/taskgraph/util/templates.py
@@ -1,8 +1,14 @@
+# 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/.
+
+from __future__ import absolute_import, print_function, unicode_literals
+
 import os
 
 import pystache
 import yaml
 import copy
 
 # Key used in template inheritance...
 INHERITS_KEY = '$inherits'
new file mode 100644
--- /dev/null
+++ b/taskcluster/taskgraph/util/yaml.py
@@ -0,0 +1,16 @@
+# 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/.
+
+from __future__ import absolute_import, print_function, unicode_literals
+
+import os
+import yaml
+
+
+def load_yaml(path, name):
+    """Convenience function to load a YAML file in the given path.  This is
+    useful for loading kind configuration files from the kind path."""
+    filename = os.path.join(path, name)
+    with open(filename, "rb") as f:
+        return yaml.load(f)