indexden is now optional
[oweals/karmaworld.git] / run_with_env.py
1 # a duct-tape-and-bubble-gum version of foreman's env support
2 import os
3 import subprocess
4
5 def run_in_env(command, filename='.env'):
6     # configure environment as a copy of the current environment
7     env = {}
8     env.update(os.environ)
9     # plus vars from the file
10     with open(filename, 'r') as config:
11         for line in config.readlines():
12             # ignore whitespace padding
13             line.strip()
14             tmp = line.split('=')
15             # further ignore whitespace padding that was around the =
16             tmp = map(str.strip, tmp)
17             if len(tmp[0]) and tmp[0][0] == '#':
18                 pass
19             # check for nonempty variable and content
20             elif len(tmp) == 2 and len(tmp[0]) and len(tmp[1]):
21                 env[tmp[0]] = tmp[1].strip("'") # drop quotes around values
22     # run command
23     subprocess.check_call(command, env=env)
24     # check_call fires an exception on failure.
25     # if we're here, both calls succeeded.
26     return 0
27
28 if __name__ == '__main__':
29     import sys
30     sys.exit(run_in_env(sys.argv[1:], '.env'))