def set_env(env_file):
while True:
source_file = '/tmp/regr.source.%d'%random.randint(0, (2_32)-1)
if not os.path.isfile(source_file): break
with open(source_file, 'w') as src_file:
src_file.write('#!/bin/bash\n')
src_file.write('source %s\n'%env_file)
src_file.write('env\n')
os.chmod(source_file, 0755)
p = subprocess.Popen(sourcefile, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = p.communicate()
setting = re.compile('^(?P[^=])=')
value = re.compile('=(?P.$)')
env_dict = {}
for line in out.splitlines():
if setting.search(line) and value.search(line):
env_dict[setting.search(line).group('setting')] = value.search(line).group('value')
for k, v in env_dict.items():
os.environ[k] = v
for k, v in env_dict.items():
try:
assert(os.getenv(k) == v)
except AssertionError:
raise Exception('Unable to modify environment')
Possible solution in Storm Script:
def set_env(env_file): while True: source_file = '/tmp/regr.source.%d'%random.randint(0, (2_32)-1) if not os.path.isfile(source_file): break with open(source_file, 'w') as src_file: src_file.write('#!/bin/bash\n') src_file.write('source %s\n'%env_file) src_file.write('env\n') os.chmod(source_file, 0755) p = subprocess.Popen(sourcefile, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (out, err) = p.communicate() setting = re.compile('^(?P[^=] )=')
value = re.compile('=(?P. $)')
env_dict = {}
for line in out.splitlines():
if setting.search(line) and value.search(line):
env_dict[setting.search(line).group('setting')] = value.search(line).group('value')
for k, v in env_dict.items():
os.environ[k] = v
for k, v in env_dict.items():
try:
assert(os.getenv(k) == v)
except AssertionError:
raise Exception('Unable to modify environment')
set_env("/etc/default/storm")