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 | 9 |
| 10 SKYPY_PATH = os.path.dirname(__file__) |
| 11 SKY_TOOLS_PATH = os.path.dirname(SKYPY_PATH) |
| 12 SKY_ROOT = os.path.dirname(SKY_TOOLS_PATH) |
| 13 SRC_ROOT = os.path.dirname(SKY_ROOT) |
| 14 |
10 class SkyServer(object): | 15 class SkyServer(object): |
11 def __init__(self, paths, port, configuration, root): | 16 def __init__(self, port, configuration, root): |
12 self.paths = paths | |
13 self.port = port | 17 self.port = port |
14 self.configuration = configuration | 18 self.configuration = configuration |
15 self.root = root | 19 self.root = root |
16 self.server = None | 20 self.server = None |
17 | 21 |
18 @staticmethod | 22 @staticmethod |
19 def _port_in_use(port): | 23 def _port_in_use(port): |
20 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | 24 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
21 return sock.connect_ex(('localhost', port)) == 0 | 25 return sock.connect_ex(('localhost', port)) == 0 |
22 | 26 |
23 @staticmethod | 27 @staticmethod |
24 def _download_server_if_necessary(paths): | 28 def _download_server_if_necessary(): |
25 subprocess.call(os.path.join(paths.sky_tools_directory, | 29 subprocess.call(os.path.join(SKY_TOOLS_PATH, 'download_sky_server')) |
26 'download_sky_server')) | 30 return os.path.join(SRC_ROOT, 'out', 'downloads', 'sky_server') |
27 return os.path.join(paths.src_root, 'out', 'downloads', 'sky_server') | |
28 | 31 |
29 def start(self): | 32 def start(self): |
30 if self._port_in_use(self.port): | 33 if self._port_in_use(self.port): |
31 logging.warn( | 34 logging.warn( |
32 'Port %s already in use, assuming custom sky_server started.' % | 35 'Port %s already in use, assuming custom sky_server started.' % |
33 self.port) | 36 self.port) |
34 return | 37 return |
35 | 38 |
36 server_path = self._download_server_if_necessary(self.paths) | 39 server_path = self._download_server_if_necessary() |
37 server_command = [ | 40 server_command = [ |
38 server_path, | 41 server_path, |
39 '-t', self.configuration, | 42 '-t', self.configuration, |
40 self.root, | 43 self.root, |
41 str(self.port), | 44 str(self.port), |
42 ] | 45 ] |
43 self.server = subprocess.Popen(server_command) | 46 self.server = subprocess.Popen(server_command) |
44 return self.server.pid | 47 return self.server.pid |
45 | 48 |
46 def stop(self): | 49 def stop(self): |
47 if self.server: | 50 if self.server: |
48 self.server.terminate() | 51 self.server.terminate() |
49 | 52 |
50 def __enter__(self): | 53 def __enter__(self): |
51 self.start() | 54 self.start() |
52 | 55 |
53 def __exit__(self, exc_type, exc_value, traceback): | 56 def __exit__(self, exc_type, exc_value, traceback): |
54 self.stop() | 57 self.stop() |
55 | 58 |
56 def path_as_url(self, path): | 59 def path_as_url(self, path): |
57 return self.url_for_path(self.port, self.root, path) | 60 return self.url_for_path(self.port, self.root, path) |
58 | 61 |
59 @staticmethod | 62 @staticmethod |
60 def url_for_path(port, root, path): | 63 def url_for_path(port, root, path): |
61 relative_path = os.path.relpath(path, root) | 64 relative_path = os.path.relpath(path, root) |
62 return 'http://localhost:%s/%s' % (port, relative_path) | 65 return 'http://localhost:%s/%s' % (port, relative_path) |
OLD | NEW |