helper.py 1.2 KB

123456789101112131415161718192021222324252627282930313233
  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(os.path.dirname(os.path.realpath(__file__)), 'config/database.yml')
  9. setup_util.replace_text(database_file, " host:.*", " host: " + database_host)
  10. def run(commands, logfile, errfile):
  11. cwd = os.path.expanduser(os.path.basename(os.path.normpath(os.path.dirname(os.path.realpath(__file__)))))
  12. try:
  13. for command in commands:
  14. if command.wait_for_exit:
  15. subprocess.check_call(command.command, shell=True, cwd=cwd, stderr=errfile, stdout=logfile)
  16. else:
  17. subprocess.Popen(command.command, shell=True, cwd=cwd, stderr=errfile, stdout=logfile)
  18. except subprocess.CalledProcessError:
  19. return 1
  20. return 0
  21. def stop(partial_command, logfile, errfile):
  22. p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
  23. out, err = p.communicate()
  24. for line in out.splitlines():
  25. if partial_command in line and 'run-tests' not in line:
  26. pid = int(line.split(None, 2)[1])
  27. os.kill(pid, 15)
  28. return 0