export_env_to_heroku.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # export a foreman env to heroku
  2. import subprocess
  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
  9. line.strip()
  10. tmp = line.split('=')
  11. # further ignore whitespace padding that was around the =
  12. tmp = map(str.strip, tmp)
  13. # Strip any quotes that might show up around the string
  14. def striphyphen(somestring):
  15. return somestring.strip("'").strip('"')
  16. tmp = map(striphyphen, tmp)
  17. if len(tmp[0]) and tmp[0][0] == '#':
  18. # the heroku CLI cannot return if a variable is not yet set
  19. # or if it has been set to the empty string.
  20. # delete commented-out variables to be safe.
  21. unset.append(tmp[0][1:])
  22. # check for nonempty variable and content
  23. elif len(tmp) == 2 and len(tmp[0]) and len(tmp[1]):
  24. data.append('{0}={1}'.format(*tmp))
  25. # run heroku configuration
  26. subprocess.check_call(data)
  27. subprocess.check_call(unset)
  28. # check_call fires an exception on failure.
  29. # if we're here, both calls succeeded.
  30. return 0
  31. if __name__ == '__main__':
  32. import sys
  33. sys.exit(export_env('.env'))