OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import socket | 5 import socket |
6 import subprocess | 6 import subprocess |
7 import logging | 7 import logging |
8 import os.path | 8 import os.path |
| 9 import platform |
9 | 10 |
10 SKYPY_PATH = os.path.dirname(os.path.abspath(__file__)) | 11 SKYPY_PATH = os.path.dirname(os.path.abspath(__file__)) |
11 SKY_TOOLS_PATH = os.path.dirname(SKYPY_PATH) | 12 SKY_TOOLS_PATH = os.path.dirname(SKYPY_PATH) |
| 13 SKYGO_PATH = os.path.join(SKY_TOOLS_PATH, 'skygo') |
12 SKY_ROOT = os.path.dirname(SKY_TOOLS_PATH) | 14 SKY_ROOT = os.path.dirname(SKY_TOOLS_PATH) |
13 SRC_ROOT = os.path.dirname(SKY_ROOT) | 15 SRC_ROOT = os.path.dirname(SKY_ROOT) |
14 | 16 |
15 class SkyServer(object): | 17 class SkyServer(object): |
16 def __init__(self, port, configuration, root, package_root): | 18 def __init__(self, port, configuration, root, package_root): |
17 self.port = port | 19 self.port = port |
18 self.configuration = configuration | 20 self.configuration = configuration |
19 self.root = root | 21 self.root = root |
20 self.package_root = package_root | 22 self.package_root = package_root |
21 self.server = None | 23 self.server = None |
22 | 24 |
23 @staticmethod | 25 @staticmethod |
24 def _port_in_use(port): | 26 def _port_in_use(port): |
25 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | 27 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
26 return sock.connect_ex(('localhost', port)) == 0 | 28 return sock.connect_ex(('localhost', port)) == 0 |
27 | 29 |
28 @staticmethod | 30 @staticmethod |
29 def _download_server_if_necessary(): | 31 def sky_server_path(): |
30 subprocess.call(os.path.join(SKY_TOOLS_PATH, 'download_sky_server')) | 32 |
31 return os.path.join(SRC_ROOT, 'out', 'downloads', 'sky_server') | 33 if platform.system() == 'Linux': |
| 34 platform_dir = 'linux64' |
| 35 elif platform.system() == 'Mac': |
| 36 platform_dir = 'mac' |
| 37 else: |
| 38 assert False, 'No sky_server binary for this platform?' |
| 39 |
| 40 return os.path.join(SKYGO_PATH, platform_dir, 'sky_server') |
32 | 41 |
33 def start(self): | 42 def start(self): |
34 if self._port_in_use(self.port): | 43 if self._port_in_use(self.port): |
35 logging.warn( | 44 logging.warn( |
36 'Port %s already in use, assuming custom sky_server started.' % | 45 'Port %s already in use, assuming custom sky_server started.' % |
37 self.port) | 46 self.port) |
38 return | 47 return |
39 | 48 |
40 server_path = self._download_server_if_necessary() | 49 server_path = self.sky_server_path() |
41 server_command = [ | 50 server_command = [ |
42 server_path, | 51 server_path, |
43 '-t', self.configuration, | 52 '-t', self.configuration, |
44 self.root, | 53 self.root, |
45 str(self.port), | 54 str(self.port), |
46 self.package_root, | 55 self.package_root, |
47 ] | 56 ] |
48 self.server = subprocess.Popen(server_command) | 57 self.server = subprocess.Popen(server_command) |
49 return self.server.pid | 58 return self.server.pid |
50 | 59 |
51 def stop(self): | 60 def stop(self): |
52 if self.server: | 61 if self.server: |
53 self.server.terminate() | 62 self.server.terminate() |
54 | 63 |
55 def __enter__(self): | 64 def __enter__(self): |
56 self.start() | 65 self.start() |
57 | 66 |
58 def __exit__(self, exc_type, exc_value, traceback): | 67 def __exit__(self, exc_type, exc_value, traceback): |
59 self.stop() | 68 self.stop() |
60 | 69 |
61 def path_as_url(self, path): | 70 def path_as_url(self, path): |
62 return self.url_for_path(self.port, self.root, path) | 71 return self.url_for_path(self.port, self.root, path) |
63 | 72 |
64 @staticmethod | 73 @staticmethod |
65 def url_for_path(port, root, path): | 74 def url_for_path(port, root, path): |
66 relative_path = os.path.relpath(path, root) | 75 relative_path = os.path.relpath(path, root) |
67 return 'http://localhost:%s/%s' % (port, relative_path) | 76 return 'http://localhost:%s/%s' % (port, relative_path) |
OLD | NEW |