Bug 1286075: factor load_yaml into a util module; r=gps draft
authorDustin J. Mitchell <dustin@mozilla.com>
Tue, 06 Sep 2016 18:01:27 +0000
changeset 412738 003971b8caf20565f03a800ae5712fe775366e92
parent 412737 61c44d02841dc497f4f9ade2a912c02e86ead62f
child 412739 484a9afc12893a2f2f4a63f9a7e4a53b536c64e4
push id29252
push userdmitchell@mozilla.com
push dateMon, 12 Sep 2016 19:16:39 +0000
reviewersgps
bugs1286075
milestone51.0a1
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)