From 2b0f207e9392018d2655994685d61f8f2e8ed391 Mon Sep 17 00:00:00 2001 From: Kazuki Sakamoto Date: Fri, 8 Jan 2021 18:07:52 -0800 Subject: [PATCH 1/4] Serialize layer env --- pyoutline/outline/backend/cue.py | 5 +++++ pyoutline/outline/layer.py | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pyoutline/outline/backend/cue.py b/pyoutline/outline/backend/cue.py index 0a6dfc7bd..635b3e00e 100644 --- a/pyoutline/outline/backend/cue.py +++ b/pyoutline/outline/backend/cue.py @@ -294,6 +294,11 @@ def _serialize(launcher, use_pycuerun): limit = Et.SubElement(limits, "limit") limit.text = limit_name + layer_env = Et.SubElement(spec_layer, "env") + for env_k, env_v in layer.get_envs().items(): + pair = Et.SubElement(layer_env, "key", {"name": env_k}) + pair.text = env_v + services = Et.SubElement(spec_layer, "services") service = Et.SubElement(services, "service") try: diff --git a/pyoutline/outline/layer.py b/pyoutline/outline/layer.py index e7220053a..d3ce07dfd 100644 --- a/pyoutline/outline/layer.py +++ b/pyoutline/outline/layer.py @@ -304,7 +304,11 @@ def set_env(self, key, value): def get_env(self, key, default=None): """Get the value of the env var that will be set before execute.""" - self.__env.get(key, default) + return self.__env.get(key, default) + + def get_envs(self): + """Return all env.""" + return self.__env def get_name(self): """Return the layer name.""" From 11ac814bbf77752f5fe3e68dca16ef3e612e6a87 Mon Sep 17 00:00:00 2001 From: Kazuki Sakamoto Date: Fri, 8 Jan 2021 20:59:19 -0800 Subject: [PATCH 2/4] Add unittest --- pyoutline/tests/json/shell.outline | 1 + pyoutline/tests/json_test.py | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pyoutline/tests/json/shell.outline b/pyoutline/tests/json/shell.outline index 5d6b4e2cb..7e0e859d1 100644 --- a/pyoutline/tests/json/shell.outline +++ b/pyoutline/tests/json/shell.outline @@ -5,6 +5,7 @@ { "name": "shell_layer", "module": "outline.modules.shell.Shell", + "env": {"LAYER_KEY": "LAYER_VALUE"}, "command": ["/bin/ls"] } ] diff --git a/pyoutline/tests/json_test.py b/pyoutline/tests/json_test.py index a9fa7fef4..3c5750727 100644 --- a/pyoutline/tests/json_test.py +++ b/pyoutline/tests/json_test.py @@ -22,7 +22,9 @@ import mock import os import unittest +from xml.etree import ElementTree as Et +import outline from outline import load_json from . import test_utils @@ -40,6 +42,7 @@ def testJson(self): '"layers": [{' '"name": "layer_1", ' '"module": "outline.modules.shell.Shell", ' + '"env": {"LAYER_KEY": "LAYER_VALUE"}, ' '"command": ["/bin/ls"]' '}]' '}') @@ -48,16 +51,25 @@ def testJson(self): self.assertEqual('test_job', ol.get_name()) self.assertEqual('1-10', ol.get_frame_range()) + l = outline.cuerun.OutlineLauncher(ol) + root = Et.fromstring(l.serialize()) + env = root.find('job/layers/layer/env/key[@name="LAYER_KEY"]') + self.assertEqual('LAYER_VALUE', env.text) + @mock.patch('outline.layer.Layer.system') + @mock.patch.dict(os.environ, {}, clear=True) def testJsonFile(self, systemMock): """Load JSON from a file""" with open(os.path.join(JSON_DIR, 'shell.outline')) as fp: ol = load_json(fp.read()) with test_utils.TemporarySessionDirectory(): ol.setup() - ol.get_layer('shell_layer').execute('1000') + layer = ol.get_layer('shell_layer') + self.assertEqual('LAYER_VALUE', layer.get_env('LAYER_KEY')) + layer.execute('1000') systemMock.assert_has_calls([mock.call(['/bin/ls'], frame=1000)]) + self.assertEqual('LAYER_VALUE', os.environ['LAYER_KEY']) if __name__ == '__main__': From d4a9bc58a252e2ccbeb101ea98916a3f31717fe3 Mon Sep 17 00:00:00 2001 From: Kazuki Sakamoto Date: Sat, 16 Jan 2021 12:17:11 -0800 Subject: [PATCH 3/4] Fix Python2 future.types.newstr.newstr decoding issue --- pyoutline/outline/backend/cue.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyoutline/outline/backend/cue.py b/pyoutline/outline/backend/cue.py index 635b3e00e..f3a27e940 100644 --- a/pyoutline/outline/backend/cue.py +++ b/pyoutline/outline/backend/cue.py @@ -296,7 +296,7 @@ def _serialize(launcher, use_pycuerun): layer_env = Et.SubElement(spec_layer, "env") for env_k, env_v in layer.get_envs().items(): - pair = Et.SubElement(layer_env, "key", {"name": env_k}) + pair = Et.SubElement(layer_env, "key", {"name": "{}".format(env_k)}) pair.text = env_v services = Et.SubElement(spec_layer, "services") From c079a944b8da994f2e909e33cde854785f454fb0 Mon Sep 17 00:00:00 2001 From: Kazuki Sakamoto Date: Sat, 16 Jan 2021 12:18:11 -0800 Subject: [PATCH 4/4] Add set_env to unit test --- pyoutline/tests/json_test.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pyoutline/tests/json_test.py b/pyoutline/tests/json_test.py index 3c5750727..6a576431f 100644 --- a/pyoutline/tests/json_test.py +++ b/pyoutline/tests/json_test.py @@ -42,7 +42,7 @@ def testJson(self): '"layers": [{' '"name": "layer_1", ' '"module": "outline.modules.shell.Shell", ' - '"env": {"LAYER_KEY": "LAYER_VALUE"}, ' + '"env": {"LAYER_KEY1": "LAYER_VALUE1"}, ' '"command": ["/bin/ls"]' '}]' '}') @@ -50,11 +50,16 @@ def testJson(self): ol = load_json(s) self.assertEqual('test_job', ol.get_name()) self.assertEqual('1-10', ol.get_frame_range()) + self.assertEqual('LAYER_VALUE1', ol.get_layer('layer_1').get_env('LAYER_KEY1')) + + ol.get_layer('layer_1').set_env('LAYER_KEY2', 'LAYER_VALUE2') l = outline.cuerun.OutlineLauncher(ol) root = Et.fromstring(l.serialize()) - env = root.find('job/layers/layer/env/key[@name="LAYER_KEY"]') - self.assertEqual('LAYER_VALUE', env.text) + env1 = root.find('job/layers/layer/env/key[@name="LAYER_KEY1"]') + self.assertEqual('LAYER_VALUE1', env1.text) + env2 = root.find('job/layers/layer/env/key[@name="LAYER_KEY2"]') + self.assertEqual('LAYER_VALUE2', env2.text) @mock.patch('outline.layer.Layer.system') @mock.patch.dict(os.environ, {}, clear=True)