run_with_env.py 1.0 KB

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