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