def test_explicit_prepend(): """join_dicts with explicitly prepended values""" a = {"A": "0"} # Prepend one value b = {"A": {"prepend": "1"}} result = mappings.join_dicts(a, b) assert result == {"A": ["1", "0"]} # Prepend list of values c = {"A": {"prepend": ["2", "3"]}} result = mappings.join_dicts(a, b, c) assert result == {"A": ["2", "3", "1", "0"]} # Multiple prepend operations d = {"A": [{"prepend": "4"}, {"prepend": "5"}]} result = mappings.join_dicts(a, b, c, d) assert result == {"A": ["5", "4", "2", "3", "1", "0"]} # Prepend to non-existant var e = {"B": {"prepend": "6"}} result = mappings.join_dicts(a, b, c, d, e) assert result == {"A": ["5", "4", "2", "3", "1", "0"], "B": ["6"]} # Prepend duplicates are ignored f = {"A": {"prepend": ["0", "5", "6"]}} result = mappings.join_dicts(a, b, c, d, e, f) assert result == {"A": ["6", "5", "4", "2", "3", "1", "0"], "B": ["6"]}
def test_explicit_set(): """join_dicts with explicitly set items""" a = {"A": "0", "B": ["1", "2"]} # Explicit set str, list, non-existant key b = { "A": { "set": "1" }, "B": { "set": ["2", "3"] }, "C": { "set": "4" }, } result = mappings.join_dicts(a, b) assert result == {"A": "1", "B": ["2", "3"], "C": "4"} # Explicit set in list of ops c = { "A": [ { "set": "10" }, { "append": "20" }, ] } result = mappings.join_dicts(a, b, c) assert result == {"A": ["10", "20"], "B": ["2", "3"], "C": "4"}
def test_implicit_prepend_values(): """join_dicts implicitly prepends values""" a = {"var": "z"} b = {"var": ["x", "y"]} c = {"var": ["0", "1"]} result = mappings.join_dicts(a, b) assert result["var"] == ["x", "y", "z"] result = mappings.join_dicts(a, b, c) assert result["var"] == ["0", "1", "x", "y", "z"]
def test_implicit_set_values(): """join_dicts implicitly sets values""" a = {"var": ["x", "y"]} b = {"var": "z"} c = {"var": "a"} result = mappings.join_dicts(a, b) assert result["var"] == b["var"] result = mappings.join_dicts(a, b, c) assert result["var"] == c["var"]
def test_join_case_insensitivity(): """join_dicts is case insensitive""" a = {"Var": "a"} # Original mixed case b = {"VAR": "b"} # UPPER - set c = {"var": ["0", "1"]} # lower - prepend # Ensure Var is properly set and case of key is changed result = mappings.join_dicts(a, b) assert result["VAR"] == "b" # Ensure Var is properly set, prepended to and case of key is changed result = mappings.join_dicts(a, b, c) assert result["var"] == ["0", "1", "b"]
def test_explicit_unset(): """join_dicts with explicitly unset keys""" a = {"A": "0"} b = {"A": {"unset": "1"}} result = mappings.join_dicts(a, b) assert result == {}
def launch_app(app): '''Launch application''' cmd = [app.path] + app.args cwd = app.cwd os_env = env_to_dict(dict(os.environ)) app_env = env_to_dict(app.env) env = dict_to_env(join_dicts(os_env, app_env)) run(cmd, env=env, cwd=cwd)
def test_explicit_remove(): """join_dicts with explicitly removed values""" a = {"A": ["0", "1", "2", "3", "4"]} # Remove one value b = {"A": {"remove": "1"}} result = mappings.join_dicts(a, b) assert result == {"A": ["0", "2", "3", "4"]} # Remove list of values c = {"A": {"remove": ["2", "4"]}} result = mappings.join_dicts(a, b, c) assert result == {"A": ["0", "3"]} # Multiple remove operations d = {"A": [{"remove": "0"}, {"remove": "3"}], "B": {"remove": "6"}} result = mappings.join_dicts(a, b, c, d) assert result == {}
def test_platform_values(): """join_dicts with platform values""" tests = { "implicit_set": { "mac": "mac", "linux": "linux", "win": "win", }, "implicit_prepend": { "mac": ["mac0", "mac1"], "linux": ["linux0", "linux1"], "win": ["win0", "win1"], }, "explicit_set": { "set": { "mac": "mac", "linux": "linux", "win": "win", } }, "explicit_ops": { "mac": [{ "append": "mac0" }, { "prepend": "mac1" }], "linux": [{ "append": "linux0" }, { "prepend": "linux1" }], "win": [{ "append": "win0" }, { "prepend": "win1" }], }, } expected = { "implicit_set": tests["implicit_set"], "implicit_prepend": tests["implicit_prepend"], "explicit_set": tests["explicit_set"]["set"], "explicit_ops": { "mac": ["mac1", "mac0"], "linux": ["linux1", "linux0"], "win": ["win1", "win0"], }, } results = mappings.join_dicts(tests) p = platform assert results["implicit_set"] == expected["implicit_set"][p] assert results["implicit_prepend"] == expected["implicit_prepend"][p] assert results["explicit_set"] == expected["explicit_set"][p] assert results["explicit_ops"] == expected["explicit_ops"][p]
def activate_cpenv_modules(app, modules): '''Add cpenv modules to app env''' import cpenv from cpenv import mappings localizer = cpenv.Localizer(to_repo='home') localized = localizer.localize(modules) activator = cpenv.Activator(localizer) cpenv_env = activator.combine_modules(localized) app_env = mappings.env_to_dict(app.env) combined_env = mappings.join_dicts(app_env, cpenv_env) app.env = mappings.dict_to_env(combined_env)
def test_explicit_complex_operation(): """join_dicts with multiple explicit operations""" a = { "A": ["0", "1", "2"], "B": "100", "C": ["0"], "D": "200", } b = { "A": [ { "remove": ["1", "2"] }, { "append": "B" }, { "prepend": ["A", "C"] }, ], "B": [ { "set": ["A", "B"] }, { "prepend": "C" }, { "remove": "B" }, ], "C": [{ "set": ["A", "B", "C"] }, { "prepend": "Z" }], "D": { "remove": "200" }, } expected = { "A": ["A", "C", "0", "B"], "B": ["C", "A"], "C": ["Z", "A", "B", "C"], } result = mappings.join_dicts(a, b) assert result == expected
def GlobalJobPreLoad(plugin): '''Execute this method in your GlobalJobPreLoad.py file. See GlobalJobPreLoad.py for reference. Or if you are not using a GlobalJobPreLoad script simply copy the file to <repo>/custom/plugins. ''' # Get job cpenv requirements job = plugin.GetJob() requirements = job.GetJobExtraInfoKeyValue('cpenv_requirements') if not requirements: plugin.LogInfo('Job has no cpenv requirements...') return # Read config from autocpenv EventPlugin configure_autocpenv(plugin.LogInfo) # Use cpenv to resolve requirements stored in ExtraInfo resolved = cpenv.resolve(requirements.split()) localizer = cpenv.Localizer(cpenv.get_repo('home')) localized = localizer.localize(resolved, overwrite=False) activator = cpenv.Activator(localizer) env = activator.combine_modules(localized) # Get existing environment plugin.LogInfo('Collecting process and job environment variables...') proc_env = {} for key in env.keys(): proc_env_var = plugin.GetProcessEnvironmentVariable(key) if proc_env_var: proc_env[key] = proc_env_var plugin.LogInfo('Merging environment variables...') new_job_env = mappings.dict_to_env(mappings.join_dicts( proc_env, env, )) new_job_env = mappings.expand_envvars(new_job_env) plugin.LogInfo('Setting process environment variables...') for k, v in new_job_env.items(): plugin.SetProcessEnvironmentVariable(k, v)