setup.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import subprocess
  2. import sys
  3. import setup_util
  4. import os
  5. def start(args):
  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()
  10. subprocess.Popen("node hello.js", shell=True, cwd="nodejs")
  11. return 0
  12. except subprocess.CalledProcessError:
  13. return 1
  14. def npm():
  15. if os.name == 'nt':
  16. subprocess.check_call("copy package.json package.json.dist /y > NUL", shell=True, cwd="nodejs")
  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")
  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")
  25. def stop():
  26. if os.name == 'nt':
  27. subprocess.Popen("taskkill /f /im node.exe > NUL", shell=True)
  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, 9)
  36. except OSError:
  37. pass
  38. return 0