setup_common.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # This file and frameworks/Scala/play2-java/setup_common.py are
  2. # duplicates and should be kept in sync.
  3. import os, setup_util, signal, subprocess
  4. # Create start and stop functions for the Play project with the given dir
  5. # and install them in the given module's globals.
  6. def make_setup_for_dir(module_globals, subtest_name):
  7. def start(args, logfile, errfile):
  8. kill_running_process(logfile)
  9. subtest_dir = get_subtest_dir()
  10. install_dir = os.environ['IROOT']
  11. is_windows = os.name == "nt"
  12. cmd_suffix = '.bat' if is_windows else ''
  13. sbt_cmd = os.path.join(install_dir, 'sbt', 'bin', 'sbt'+cmd_suffix)
  14. app_cmd = os.path.join(subtest_dir, 'target','universal','stage','bin',subtest_name+cmd_suffix)
  15. setup_util.replace_text(
  16. os.path.join(subtest_dir,'conf','application.conf'),
  17. "jdbc:mysql:\/\/.*:3306", "jdbc:mysql://" + args.database_host + ":3306")
  18. logfile.write('Staging app: '+sbt_cmd+' stage\n')
  19. subprocess.call(
  20. [sbt_cmd, 'stage'],
  21. stdin=subprocess.PIPE, cwd=subtest_dir, stderr=errfile, stdout=logfile)
  22. logfile.write('Starting app: '+app_cmd+'\n')
  23. subprocess.Popen(
  24. [app_cmd],
  25. shell=True, stdin=subprocess.PIPE, cwd=subtest_dir, stderr=errfile, stdout=logfile)
  26. return 0
  27. def stop(logfile, errfile):
  28. kill_running_process(logfile)
  29. return 0
  30. # Install the start and stop functions in the calling module
  31. module_globals['start'] = start
  32. module_globals['stop'] = stop
  33. def get_subtest_dir():
  34. test_dir = os.environ['TROOT']
  35. return os.path.join(test_dir, subtest_name)
  36. # Kill the running process and delete the RUNNING_PID file (if any).
  37. def kill_running_process(logfile):
  38. subtest_dir = get_subtest_dir()
  39. pidfile = os.path.join(subtest_dir,"target","universal","stage","RUNNING_PID")
  40. if not os.path.exists(pidfile):
  41. logfile.write('No PID file: {}\n'.format(pidfile))
  42. return
  43. logfile.write('Reading and deleting PID file: {}\n'.format(pidfile))
  44. with open(pidfile) as f:
  45. pid = int(f.read())
  46. os.remove(pidfile)
  47. logfile.write('Sending SIGTERM to process: {}\n'.format(pid))
  48. os.kill(pid, signal.SIGTERM)