make more use of the .env file on diverse systems
[oweals/karmaworld.git] / export_env_to_heroku.py
1 # export a foreman env to heroku
2 import subprocess
3
4 def export_env(filename='.env'):
5     data=['heroku', 'config:set']
6     unset=['heroku', 'config:unset']
7     with open(filename, 'r') as config:
8         for line in config.readlines():
9             # ignore whitespace padding
10             line.strip()
11             tmp = line.split('=')
12             # further ignore whitespace padding that was around the =
13             tmp = map(str.strip, tmp)
14             if len(tmp[0]) and tmp[0][0] == '#':
15                 # the heroku CLI cannot return if a variable is not yet set
16                 # or if it has been set to the empty string.
17                 # delete commented-out variables to be safe.
18                 unset.append(tmp[0][1:])
19             # check for nonempty variable and content
20             elif len(tmp) == 2 and len(tmp[0]) and len(tmp[1]):
21                     
22                 data.append('{0}={1}'.format(*tmp))
23     # run heroku configuration
24     subprocess.check_call(data)
25     subprocess.check_call(unset)
26     # check_call fires an exception on failure.
27     # if we're here, both calls succeeded.
28     return 0
29
30 if __name__ == '__main__':
31     import sys
32     sys.exit(export_env('.env'))