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