Skip to content

Instantly share code, notes, and snippets.

@zimmerst
Created December 7, 2016 16:37
Show Gist options
  • Save zimmerst/ba6d76f6c9abad7da07bca574779c956 to your computer and use it in GitHub Desktop.
Save zimmerst/ba6d76f6c9abad7da07bca574779c956 to your computer and use it in GitHub Desktop.
#####
# @brief: example to show how one would emulate equivalent to source script.sh, creates env dump and updates internal dictionary
# @todo: python executable itself needs to be reloaded
#####
from subprocess import Popen, PIPE
from os.path import isfile
from os import environ
from ast import literal_eval
def set_environ(source_cmd):
'''emulates environment'''
my_script="#!/bin/bash\n{source_cmd}\npython -c \"from os import environ; fout=open('/tmp/my_script.env','w'); fout.write(str(dict(environ))); fout.close()\"".format(source_cmd=source_cmd)
fout = open("/tmp/my_script.sh","w")
fout.write(my_script)
fout.close()
po = Popen("bash /tmp/my_script.sh".split(), stdout=PIPE)
rc = po.wait()
if rc:
raise Exception("could not execute script")
if not isfile("/tmp/my_script.env"):
raise Exception("environment not dumped correctly, could not find file")
my_env = literal_eval(open("/tmp/my_script.env","r").read())
return my_env
# update fields
environ.update(set_environ("source /opt/exp_software/dampe/setup/setup.sh"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment