setup.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import subprocess
  2. import sys
  3. import os
  4. import setup_util
  5. from threading import Thread
  6. from Queue import Queue, Empty
  7. # TODO - no need to use a daemon, kill this off in stop!
  8. # NOTE - it is safe to use logging module in a multi-threaded
  9. # system, but not safe to log to the same file across multiple
  10. # processes. Our system has two main processes (main and __run_test),
  11. # and lots of minor ones from 'subprocess'. As long as we only use
  12. # one logger inside TestRunner and NonBlockingFoo, sd are good
  13. # Add credit for http://eyalarubas.com/python-subproc-nonblock.html
  14. class NonBlockingStreamReader:
  15. def __init__(self, stream):
  16. self._s = stream
  17. self._q = Queue()
  18. def _populateQueue(stream, queue):
  19. for line in iter(stream.readline, b''):
  20. queue.put(line)
  21. self._t = Thread(target = _populateQueue,
  22. args = (self._s, self._q))
  23. self._t.daemon = True
  24. self._t.start() #start collecting lines from the stream
  25. # TODO - This is only returning one line, if it is available.
  26. def readline(self, timeout = None):
  27. try:
  28. return self._q.get(block = timeout is not None,
  29. timeout = timeout)
  30. except Empty:
  31. return None
  32. def read(self):
  33. lines = []
  34. while True:
  35. line = self.readline(0.1)
  36. if not line:
  37. return lines
  38. lines.append(line)
  39. import time
  40. class TestRunner:
  41. def __init__(self, directory, stdout):
  42. self.dir = directory
  43. self.stdout = stdout
  44. def sh(self, command, **kwargs):
  45. kwargs.setdefault('cwd', self.dir)
  46. self.stdout.write("Running %s (cwd=%s)" % (command, kwargs.get('cwd')))
  47. try:
  48. output = subprocess.check_output(command, shell=True, **kwargs)
  49. if output and output.strip():
  50. self.stdout.write("Output:")
  51. self.stdout.write(output.rstrip('\n'))
  52. else:
  53. self.stdout.write("No Output")
  54. except subprocess.CalledProcessError:
  55. self.stdout.write("Process Returned non-zero exit code")
  56. def sh_async(self, command, initial_logs=True, **kwargs):
  57. kwargs.setdefault('cwd', self.dir)
  58. self.stdout.write("Running %s (cwd=%s)" % (command, kwargs.get('cwd')))
  59. # Open in line-buffered mode, as NonBlockingStreamReader uses readline anyways
  60. process = subprocess.Popen(command, bufsize=1, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE, **kwargs)
  61. nbsr = NonBlockingStreamReader(process.stdout)
  62. nbsr_err = NonBlockingStreamReader(process.stderr)
  63. if initial_logs:
  64. time.sleep(8)
  65. # TODO put this read into a tight loop to prevent deadlock due to
  66. # filling up OS buffers
  67. out = nbsr.read()
  68. err = nbsr_err.read()
  69. if len(out) != 0:
  70. self.stdout.write("Initial Output:")
  71. for line in out:
  72. self.stdout.write(line.rstrip('\n'))
  73. else:
  74. self.stdout.write("No output")
  75. if len(err) != 0:
  76. self.stdout.write("Initial Error Logs")
  77. for line in err:
  78. self.stdout.write(line.rstrip('\n'))
  79. def start(args, logfile, errfile):
  80. t = TestRunner("go", logfile)
  81. setup_util.replace_text("go/src/hello/hello.go", "tcp\(.*:3306\)", "tcp(" + args.database_host + ":3306)")
  82. if os.name == 'nt':
  83. #subprocess.call("rmdir /s /q pkg\\windows_amd64", shell=True, cwd="go")
  84. #subprocess.call("rmdir /s /q src\\github.com", shell=True, cwd="go")
  85. #subprocess.call("del /s /q /f bin\\hello.exe", shell=True, cwd="go")
  86. subprocess.call("set GOPATH=C:\\FrameworkBenchmarks\\go&& go get ./...", shell=True, cwd="go", stderr=errfile, stdout=logfile)
  87. subprocess.Popen("setup.bat", shell=True, cwd="go", stderr=errfile, stdout=logfile)
  88. return 0
  89. t.sh("which go")
  90. t.sh("rm -rf src/github.com")
  91. t.sh("ls src/github.com/go-sql-driver/mysql")
  92. t.sh("go get ./...")
  93. t.sh("ls src/github.com/go-sql-driver/mysql")
  94. t.sh_async("go run -x -v src/hello/hello.go")
  95. # t.sh("ps aux")
  96. return 0
  97. def stop(logfile, errfile):
  98. if os.name == 'nt':
  99. subprocess.call("taskkill /f /im go.exe > NUL", shell=True, stderr=errfile, stdout=logfile)
  100. subprocess.call("taskkill /f /im hello.exe > NUL", shell=True, stderr=errfile, stdout=logfile)
  101. return 0
  102. p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
  103. out, err = p.communicate()
  104. for line in out.splitlines():
  105. if 'hello' in line:
  106. pid = int(line.split(None, 2)[1])
  107. os.kill(pid, 15)
  108. return 0