diff options
author | Richard Maw <richard.maw@codethink.co.uk> | 2013-08-13 09:53:32 +0000 |
---|---|---|
committer | Richard Maw <richard.maw@codethink.co.uk> | 2013-08-13 14:21:52 +0000 |
commit | ec6d4e10304293157ba61501b7f053cb1cbc142a (patch) | |
tree | 144869b0f95956aac6756ad3016221580f835028 | |
parent | c38b5ddeb6359f6f848553bcfdde52365c807ab4 (diff) | |
download | morph-ec6d4e10304293157ba61501b7f053cb1cbc142a.tar.gz |
deploy: refactor environment argument parsing
It is now a tested helper function in morphlib.util
-rw-r--r-- | morphlib/plugins/deploy_plugin.py | 8 | ||||
-rw-r--r-- | morphlib/util.py | 32 | ||||
-rw-r--r-- | morphlib/util_tests.py | 18 |
3 files changed, 51 insertions, 7 deletions
diff --git a/morphlib/plugins/deploy_plugin.py b/morphlib/plugins/deploy_plugin.py index ef607ffe..edc4a81a 100644 --- a/morphlib/plugins/deploy_plugin.py +++ b/morphlib/plugins/deploy_plugin.py @@ -303,13 +303,7 @@ class DeployPlugin(cliapp.Plugin): system_tree=system_tree) # Set up environment for running extensions. - env = dict(os.environ) - for spec in env_vars: - name, value = spec.split('=', 1) - if name in env: - raise morphlib.Error( - '%s is already set in the enviroment' % name) - env[name] = value + env = morphlib.util.parse_environment_pairs(os.environ, env_vars) # Extensions get a private tempdir so we can more easily clean # up any files an extension left behind diff --git a/morphlib/util.py b/morphlib/util.py index ead0bafe..b83211e3 100644 --- a/morphlib/util.py +++ b/morphlib/util.py @@ -327,3 +327,35 @@ def find_leaf(dirname, subdir_name): return None dirname = subdirs[0] + +class EnvironmentAlreadySetError(morphlib.Error): + + def __init__(self, conflicts): + self.conflicts = conflicts + morphlib.Error.__init__( + self, 'Keys %r are already set in the environment' % conflicts) + + +def parse_environment_pairs(env, pairs): + '''Add key=value pairs to the environment dict. + + Given a dict and a list of strings of the form key=value, + set dict[key] = value, unless key is already set in the + environment, at which point raise an exception. + + This does not modify the passed in dict. + + Returns the extended dict. + + ''' + + extra_env = dict(p.split('=', 1) for p in pairs) + conflicting = [k for k in extra_env if k in env] + if conflicting: + raise EnvironmentAlreadySetError(conflicting) + + # Return a dict that is the union of the two + # This is not the most performant, since it creates + # 3 unnecessary lists, but I felt this was the most + # easy to read. Using itertools.chain may be more efficicent + return dict(env.items() + extra_env.items()) diff --git a/morphlib/util_tests.py b/morphlib/util_tests.py index eaff0821..ca9fe5ae 100644 --- a/morphlib/util_tests.py +++ b/morphlib/util_tests.py @@ -87,3 +87,21 @@ class FindParentOfTests(unittest.TestCase): def test_find_leaf_returns_none_if_not_found(self): self.assertEqual(morphlib.util.find_leaf(self.a, '.magic'), None) + +class ParseEnvironmentPairsTests(unittest.TestCase): + + def test_parse_environment_pairs_adds_key(self): + ret = morphlib.util.parse_environment_pairs({}, ["foo=bar"]) + self.assertEqual(ret.get("foo"), "bar") + + def test_parse_environment_does_not_alter_passed_dict(self): + d = {} + morphlib.util.parse_environment_pairs(d, ["foo=bar"]) + self.assertTrue("foo" not in d) + + def test_parse_environment_raises_on_duplicates(self): + self.assertRaises( + morphlib.util.EnvironmentAlreadySetError, + morphlib.util.parse_environment_pairs, + {"foo": "bar"}, + ["foo=bar"]) |