From a4dae2dfbe152f7710e8ab24e6abde47dff917fb Mon Sep 17 00:00:00 2001 From: Bryan Date: Wed, 11 Mar 2015 00:38:23 -0400 Subject: [PATCH] make more use of the .env file on diverse systems --- export_env_to_heroku.py | 1 + run_with_env.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 run_with_env.py diff --git a/export_env_to_heroku.py b/export_env_to_heroku.py index 6e652bc..5048e63 100644 --- a/export_env_to_heroku.py +++ b/export_env_to_heroku.py @@ -1,3 +1,4 @@ +# export a foreman env to heroku import subprocess def export_env(filename='.env'): diff --git a/run_with_env.py b/run_with_env.py new file mode 100644 index 0000000..0689ed6 --- /dev/null +++ b/run_with_env.py @@ -0,0 +1,30 @@ +# a duct-tape-and-bubble-gum version of foreman's env support +import os +import subprocess + +def run_in_env(command, filename='.env'): + # configure environment as a copy of the current environment + env = {} + env.update(os.environ) + # plus vars from the file + with open(filename, 'r') as config: + for line in config.readlines(): + # ignore whitespace padding + line.strip() + tmp = line.split('=') + # further ignore whitespace padding that was around the = + tmp = map(str.strip, tmp) + if len(tmp[0]) and tmp[0][0] == '#': + pass + # check for nonempty variable and content + elif len(tmp) == 2 and len(tmp[0]) and len(tmp[1]): + env[tmp[0]] = tmp[1].strip("'") # drop quotes around values + # run command + subprocess.check_call(command, env=env) + # check_call fires an exception on failure. + # if we're here, both calls succeeded. + return 0 + +if __name__ == '__main__': + import sys + sys.exit(run_in_env(sys.argv[1:], '.env')) -- 2.25.1