| 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 |
| 11 import shutil | |
| 12 import socket | 11 import socket |
| 13 import sys | 12 import sys |
| 14 import tempfile | 13 import tempfile |
| 15 import urllib | 14 import urllib |
| 16 | 15 |
| 17 | 16 |
| 18 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) | 17 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 19 CLIENT_DIR = os.path.join(THIS_DIR, '..', '..', '..', 'client') | 18 CLIENT_DIR = os.path.join(THIS_DIR, '..', '..', '..', 'client') |
| 20 sys.path.insert(0, CLIENT_DIR) | 19 sys.path.insert(0, CLIENT_DIR) |
| 20 from third_party.depot_tools import fix_encoding |
| 21 from utils import file_path |
| 21 from utils import subprocess42 | 22 from utils import subprocess42 |
| 22 sys.path.pop(0) | 23 sys.path.pop(0) |
| 23 | 24 |
| 24 | 25 |
| 25 class LocalBot(object): | 26 class LocalBot(object): |
| 26 """A local running Swarming bot. | 27 """A local running Swarming bot. |
| 27 | 28 |
| 28 It creates its own temporary directory to download the zip and run tasks | 29 It creates its own temporary directory to download the zip and run tasks |
| 29 locally. | 30 locally. |
| 30 """ | 31 """ |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 # TODO(maruel): SIGKILL after N seconds. | 72 # TODO(maruel): SIGKILL after N seconds. |
| 72 self._proc.wait() | 73 self._proc.wait() |
| 73 except OSError: | 74 except OSError: |
| 74 pass | 75 pass |
| 75 exit_code = self._proc.returncode | 76 exit_code = self._proc.returncode |
| 76 if self._tmpdir: | 77 if self._tmpdir: |
| 77 for i in sorted(glob.glob(os.path.join(self._tmpdir, 'logs', '*.log'))): | 78 for i in sorted(glob.glob(os.path.join(self._tmpdir, 'logs', '*.log'))): |
| 78 self._read_log(i) | 79 self._read_log(i) |
| 79 if not leak: | 80 if not leak: |
| 80 try: | 81 try: |
| 81 shutil.rmtree(self._tmpdir) | 82 file_path.rmtree(self._tmpdir) |
| 82 except OSError: | 83 except OSError: |
| 83 print >> sys.stderr, 'Leaking %s' % self._tmpdir | 84 print >> sys.stderr, 'Leaking %s' % self._tmpdir |
| 84 self._tmpdir = None | 85 self._tmpdir = None |
| 85 self._proc = None | 86 self._proc = None |
| 86 return exit_code | 87 return exit_code |
| 87 | 88 |
| 88 def poll(self): | 89 def poll(self): |
| 89 """Polls the process to know if it exited.""" | 90 """Polls the process to know if it exited.""" |
| 90 self._proc.poll() | 91 self._proc.poll() |
| 91 | 92 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 114 | 115 |
| 115 def _read_log(self, path): | 116 def _read_log(self, path): |
| 116 try: | 117 try: |
| 117 with open(path, 'rb') as f: | 118 with open(path, 'rb') as f: |
| 118 self._logs[os.path.basename(path)] = f.read() | 119 self._logs[os.path.basename(path)] = f.read() |
| 119 except (IOError, OSError): | 120 except (IOError, OSError): |
| 120 pass | 121 pass |
| 121 | 122 |
| 122 | 123 |
| 123 def main(): | 124 def main(): |
| 125 fix_encoding.fix_encoding() |
| 124 if len(sys.argv) != 2: | 126 if len(sys.argv) != 2: |
| 125 print >> sys.stderr, 'Specify url to Swarming server' | 127 print >> sys.stderr, 'Specify url to Swarming server' |
| 126 return 1 | 128 return 1 |
| 127 bot = LocalBot(sys.argv[1], False) | 129 bot = LocalBot(sys.argv[1], False) |
| 128 try: | 130 try: |
| 129 bot.start() | 131 bot.start() |
| 130 bot.wait() | 132 bot.wait() |
| 131 bot.dump_log() | 133 bot.dump_log() |
| 132 except KeyboardInterrupt: | 134 except KeyboardInterrupt: |
| 133 print >> sys.stderr, '<Ctrl-C> received; stopping bot' | 135 print >> sys.stderr, '<Ctrl-C> received; stopping bot' |
| 134 finally: | 136 finally: |
| 135 exit_code = bot.stop(False) | 137 exit_code = bot.stop(False) |
| 136 return exit_code | 138 return exit_code |
| 137 | 139 |
| 138 | 140 |
| 139 if __name__ == '__main__': | 141 if __name__ == '__main__': |
| 140 sys.exit(main()) | 142 sys.exit(main()) |
| OLD | NEW |