setup.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import subprocess
  2. import sys
  3. import setup_util
  4. import os
  5. def start(args, logfile, errfile):
  6. setup_util.replace_text("nodejs/hello.js", "mongodb:\/\/.*\/hello_world", "mongodb://" + args.database_host + "/hello_world")
  7. setup_util.replace_text("nodejs/hello.js", "localhost", args.database_host)
  8. try:
  9. npm(logfile, errfile)
  10. subprocess.Popen("node hello.js", shell=True, cwd="nodejs", stderr=errfile, stdout=logfile)
  11. return 0
  12. except subprocess.CalledProcessError:
  13. return 1
  14. def npm(logfile, errfile):
  15. if os.name == 'nt':
  16. subprocess.check_call("copy package.json package.json.dist /y > NUL", shell=True, cwd="nodejs", stderr=errfile, stdout=logfile)
  17. setup_util.replace_text("nodejs/package.json", ".*mysql.*", "")
  18. setup_util.replace_text("nodejs/package.json", ".*mapper.*", "")
  19. try:
  20. subprocess.check_call("npm install", shell=True, cwd="nodejs", stderr=errfile, stdout=logfile)
  21. finally:
  22. if os.name == 'nt':
  23. subprocess.check_call("del package.json", shell=True, cwd="nodejs")
  24. subprocess.check_call("ren package.json.dist package.json", shell=True, cwd="nodejs", stderr=errfile, stdout=logfile)
  25. def stop(logfile, errfile):
  26. if os.name == 'nt':
  27. subprocess.Popen("taskkill /f /im node.exe > NUL", shell=True, stderr=errfile, stdout=logfile)
  28. return 0
  29. p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
  30. out, err = p.communicate()
  31. for line in out.splitlines():
  32. if 'hello.js' in line:
  33. pid = int(line.split(None, 2)[1])
  34. try:
  35. os.kill(pid, 15)
  36. except OSError:
  37. pass
  38. return 0