helper.py 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. import os
  2. import subprocess
  3. from collections import namedtuple
  4. import setup_util
  5. Command = namedtuple('Command', ['command', 'wait_for_exit'])
  6. def set_database_host(args):
  7. database_host = args.database_host or 'localhost'
  8. database_file = os.path.join(args.troot, 'config', 'database.yml')
  9. setup_util.replace_text(database_file, " host:.*", " host: " + database_host)
  10. def run(commands, logfile, errfile, cwd):
  11. try:
  12. for command in commands:
  13. if command.wait_for_exit:
  14. subprocess.check_call(command.command, shell=True, cwd=cwd, stderr=errfile, stdout=logfile)
  15. else:
  16. subprocess.Popen(command.command, shell=True, cwd=cwd, stderr=errfile, stdout=logfile)
  17. except subprocess.CalledProcessError:
  18. return 1
  19. return 0
  20. def stop(partial_command, logfile, errfile):
  21. p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
  22. out, err = p.communicate()
  23. for line in out.splitlines():
  24. if partial_command in line and 'run-tests' not in line:
  25. pid = int(line.split(None, 2)[1])
  26. os.kill(pid, 15)
  27. return 0