Last active
September 11, 2015 13:17
-
-
Save danbradham/0155f0e746fcf93384fe to your computer and use it in GitHub Desktop.
Environment Wrangling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
from copy import deepcopy | |
platform = sys.platform.rstrip('1234567890').lower() | |
if platform == 'darwin': | |
platform = 'osx' | |
elif platform == 'linux': | |
platform = 'unix' | |
env_a = { | |
'PATH': ['New/Path/A', 'New/Path/B'], | |
'PYTHONPATH': ['Another/Path/A', 'B', '$STRVARB'], | |
'STRVAR': 'STRVAR', | |
'NUMVAR': 890176, | |
'PLATVAR': { | |
'win': 'windowses', | |
'osx': 'osxes', | |
'unix': 'unixes', | |
} | |
} | |
env_b = { | |
'PATH': ['New/Path/A', 'New/Path/B', 'New/Path/C'], | |
'PYTHONPATH': ['Another/Path/A', 'B', 'C'], | |
'STRVARB': '$STRVAR', | |
'NUMVAR': 89017, | |
'PLATVARB': { | |
'win': 'windowsesb', | |
'osx': 'osxesb', | |
'unix': 'unixesb', | |
} | |
} | |
def join_envs(*envs): | |
out_env = {} | |
for env in envs: | |
for k, v in env.iteritems(): | |
if isinstance(v, dict): | |
out_env[k] = v[platform] | |
elif isinstance(v, (int, float, long)): | |
out_env[k] = str(v) | |
elif isinstance(v, basestring): | |
out_env[k] = v | |
elif isinstance(v, unicode): | |
out_env[k] = str(v) | |
elif isinstance(v, (list, tuple, set)): | |
if not k in out_env: | |
out_env[k] = list(v) | |
elif isinstance(out_env[k], list): | |
for item in v: | |
if not item in out_env[k]: | |
out_env[k].insert(0, item) | |
elif isinstance(out_env[k], basestring): | |
v.append(out_env[k]) | |
out_env[k] = v | |
else: | |
raise TypeError('{} not a valid value type'.format(type(v))) | |
return out_env | |
def env_to_dict(env, pathsep=os.pathsep): | |
out_dict = {} | |
for k, v in env.iteritems(): | |
if pathsep in v: | |
out_dict[k] = v.split(pathsep) | |
else: | |
out_dict[k] = v | |
return out_dict | |
def dict_to_env(env, pathsep=os.pathsep): | |
out_env = {} | |
for k, v in env.iteritems(): | |
if isinstance(v, list): | |
out_env[k] = pathsep.join(v) | |
elif isinstance(v, basestring): | |
out_env[k] = v | |
else: | |
raise TypeError('{} not a valid env var type'.format(type(v))) | |
return out_env | |
def expand_env(env, using=None): | |
using = using or os.environ.data | |
default_env = deepcopy(os.environ.data) | |
os.environ.data = using | |
out_env = {} | |
# Expand | |
for k, v in env.iteritems(): | |
out_env[k] = os.path.expandvars(v) | |
# Again for nested variable references | |
for k, v in out_env.items(): | |
out_env[k] = os.path.expandvars(v) | |
os.environ.data = default_env | |
return out_env | |
env_c = join_envs(env_to_dict(os.environ.data), env_a, env_b) | |
env_d = dict_to_env(env_c) | |
import pprint | |
pprint.pprint(env_c) | |
pprint.pprint(env_d) | |
os.environ.data = expand_env(env_d, env_d) | |
pprint.pprint(os.environ.data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment