-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexport_env_to_heroku.py
36 lines (33 loc) · 1.37 KB
/
export_env_to_heroku.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# export a foreman env to heroku
import subprocess
def export_env(filename='.env'):
data=['heroku', 'config:set']
unset=['heroku', 'config:unset']
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)
# Strip any quotes that might show up around the string
def striphyphen(somestring):
return somestring.strip("'").strip('"')
tmp = map(striphyphen, tmp)
if len(tmp[0]) and tmp[0][0] == '#':
# the heroku CLI cannot return if a variable is not yet set
# or if it has been set to the empty string.
# delete commented-out variables to be safe.
unset.append(tmp[0][1:])
# check for nonempty variable and content
elif len(tmp) == 2 and len(tmp[0]) and len(tmp[1]):
data.append('{0}={1}'.format(*tmp))
# run heroku configuration
subprocess.check_call(data)
subprocess.check_call(unset)
# check_call fires an exception on failure.
# if we're here, both calls succeeded.
return 0
if __name__ == '__main__':
import sys
sys.exit(export_env('.env'))