| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2015 The Swarming Authors. All rights reserved. | 2 # Copyright 2015 The Swarming Authors. All rights reserved. |
| 3 # Use of this source code is governed by the Apache v2.0 license that can be | 3 # Use of this source code is governed by the Apache v2.0 license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Starts a local bot to connect to a local server.""" | 6 """Starts a local bot to connect to a local server.""" |
| 7 | 7 |
| 8 import glob | 8 import glob |
| 9 import os | 9 import os |
| 10 import signal | 10 import signal |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 if self._redirect: | 46 if self._redirect: |
| 47 logs = os.path.join(self._tmpdir, 'logs') | 47 logs = os.path.join(self._tmpdir, 'logs') |
| 48 if not os.path.isdir(logs): | 48 if not os.path.isdir(logs): |
| 49 os.mkdir(logs) | 49 os.mkdir(logs) |
| 50 with open(os.path.join(logs, 'bot_stdout.log'), 'wb') as f: | 50 with open(os.path.join(logs, 'bot_stdout.log'), 'wb') as f: |
| 51 self._proc = subprocess.Popen( | 51 self._proc = subprocess.Popen( |
| 52 cmd, cwd=self._tmpdir, stdout=f, env=env, stderr=f, **kwargs) | 52 cmd, cwd=self._tmpdir, stdout=f, env=env, stderr=f, **kwargs) |
| 53 else: | 53 else: |
| 54 self._proc = subprocess.Popen(cmd, cwd=self._tmpdir, env=env, **kwargs) | 54 self._proc = subprocess.Popen(cmd, cwd=self._tmpdir, env=env, **kwargs) |
| 55 | 55 |
| 56 def stop(self): | 56 def stop(self, leak): |
| 57 """Stops the local Swarming bot. Returns the process exit code.""" | 57 """Stops the local Swarming bot. Returns the process exit code.""" |
| 58 if not self._proc: | 58 if not self._proc: |
| 59 return None | 59 return None |
| 60 if self._proc.poll() is None: | 60 if self._proc.poll() is None: |
| 61 try: | 61 try: |
| 62 self._proc.send_signal(signal.SIGTERM) | 62 self._proc.send_signal(signal.SIGTERM) |
| 63 # TODO(maruel): SIGKILL after N seconds. | 63 # TODO(maruel): SIGKILL after N seconds. |
| 64 self._proc.wait() | 64 self._proc.wait() |
| 65 except OSError: | 65 except OSError: |
| 66 pass | 66 pass |
| 67 exit_code = self._proc.returncode | 67 exit_code = self._proc.returncode |
| 68 if self._tmpdir: | 68 if self._tmpdir: |
| 69 for i in sorted(glob.glob(os.path.join(self._tmpdir, 'logs', '*.log'))): | 69 for i in sorted(glob.glob(os.path.join(self._tmpdir, 'logs', '*.log'))): |
| 70 self._read_log(i) | 70 self._read_log(i) |
| 71 try: | 71 if not leak: |
| 72 shutil.rmtree(self._tmpdir) | 72 try: |
| 73 except OSError: | 73 shutil.rmtree(self._tmpdir) |
| 74 print >> sys.stderr, 'Leaking %s' % self._tmpdir | 74 except OSError: |
| 75 print >> sys.stderr, 'Leaking %s' % self._tmpdir |
| 75 self._tmpdir = None | 76 self._tmpdir = None |
| 76 self._proc = None | 77 self._proc = None |
| 77 return exit_code | 78 return exit_code |
| 78 | 79 |
| 79 def wait(self): | 80 def wait(self): |
| 80 """Waits for the process to normally exit.""" | 81 """Waits for the process to normally exit.""" |
| 81 self._proc.wait() | 82 self._proc.wait() |
| 82 | 83 |
| 83 def kill(self): | 84 def kill(self): |
| 84 """Kills the child forcibly.""" | 85 """Kills the child forcibly.""" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 112 print >> sys.stderr, 'Specify url to Swarming server' | 113 print >> sys.stderr, 'Specify url to Swarming server' |
| 113 return 1 | 114 return 1 |
| 114 bot = LocalBot(sys.argv[1], False) | 115 bot = LocalBot(sys.argv[1], False) |
| 115 try: | 116 try: |
| 116 bot.start() | 117 bot.start() |
| 117 bot.wait() | 118 bot.wait() |
| 118 bot.dump_log() | 119 bot.dump_log() |
| 119 except KeyboardInterrupt: | 120 except KeyboardInterrupt: |
| 120 print >> sys.stderr, '<Ctrl-C> received; stopping bot' | 121 print >> sys.stderr, '<Ctrl-C> received; stopping bot' |
| 121 finally: | 122 finally: |
| 122 exit_code = bot.stop() | 123 exit_code = bot.stop(False) |
| 123 return exit_code | 124 return exit_code |
| 124 | 125 |
| 125 | 126 |
| 126 if __name__ == '__main__': | 127 if __name__ == '__main__': |
| 127 sys.exit(main()) | 128 sys.exit(main()) |
| OLD | NEW |