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 stat |
9 import platform | 10 import platform |
10 | 11 |
11 SKYPY_PATH = os.path.dirname(os.path.abspath(__file__)) | 12 SKYPY_PATH = os.path.dirname(os.path.abspath(__file__)) |
12 SKY_TOOLS_PATH = os.path.dirname(SKYPY_PATH) | 13 SKY_TOOLS_PATH = os.path.dirname(SKYPY_PATH) |
13 SKYGO_PATH = os.path.join(SKY_TOOLS_PATH, 'skygo') | 14 SKYGO_PATH = os.path.join(SKY_TOOLS_PATH, 'skygo') |
14 SKY_ROOT = os.path.dirname(SKY_TOOLS_PATH) | 15 SKY_ROOT = os.path.dirname(SKY_TOOLS_PATH) |
15 SRC_ROOT = os.path.dirname(SKY_ROOT) | 16 SRC_ROOT = os.path.dirname(SKY_ROOT) |
16 | 17 |
17 class SkyServer(object): | 18 class SkyServer(object): |
18 def __init__(self, port, configuration, root, package_root): | 19 def __init__(self, port, configuration, root, package_root): |
19 self.port = port | 20 self.port = port |
20 self.configuration = configuration | 21 self.configuration = configuration |
21 self.root = root | 22 self.root = root |
22 self.package_root = package_root | 23 self.package_root = package_root |
23 self.server = None | 24 self.server = None |
24 | 25 |
25 @staticmethod | 26 @staticmethod |
26 def _port_in_use(port): | 27 def _port_in_use(port): |
27 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | 28 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
28 return sock.connect_ex(('localhost', port)) == 0 | 29 return sock.connect_ex(('localhost', port)) == 0 |
29 | 30 |
30 @staticmethod | 31 @staticmethod |
31 def sky_server_path(): | 32 def sky_server_path(): |
32 | 33 |
33 if platform.system() == 'Linux': | 34 if platform.system() == 'Linux': |
34 platform_dir = 'linux64' | 35 platform_dir = 'linux64' |
35 elif platform.system() == 'Mac': | 36 elif platform.system() == 'Darwin': |
36 platform_dir = 'mac' | 37 platform_dir = 'mac' |
37 else: | 38 else: |
38 assert False, 'No sky_server binary for this platform?' | 39 assert False, 'No sky_server binary for this platform: ' + platform.
system() |
39 | 40 |
40 return os.path.join(SKYGO_PATH, platform_dir, 'sky_server') | 41 return os.path.join(SKYGO_PATH, platform_dir, 'sky_server') |
41 | 42 |
42 def start(self): | 43 def start(self): |
43 if self._port_in_use(self.port): | 44 if self._port_in_use(self.port): |
44 logging.warn( | 45 logging.warn( |
45 'Port %s already in use, assuming custom sky_server started.' % | 46 'Port %s already in use, assuming custom sky_server started.' % |
46 self.port) | 47 self.port) |
47 return | 48 return |
48 | 49 |
49 server_path = self.sky_server_path() | 50 server_path = self.sky_server_path() |
| 51 st = os.stat(self.sky_server_path()) |
| 52 if not (stat.S_IXUSR & st[stat.ST_MODE]): |
| 53 logging.warn('Changing the permissions of %s to be executable.', sel
f.sky_server_path()) |
| 54 os.chmod(self.sky_server_path(), st[stat.ST_MODE] | stat.S_IEXEC) |
50 server_command = [ | 55 server_command = [ |
51 server_path, | 56 server_path, |
52 '-t', self.configuration, | 57 '-t', self.configuration, |
53 self.root, | 58 self.root, |
54 str(self.port), | 59 str(self.port), |
55 self.package_root, | 60 self.package_root, |
56 ] | 61 ] |
57 self.server = subprocess.Popen(server_command) | 62 self.server = subprocess.Popen(server_command) |
58 return self.server.pid | 63 return self.server.pid |
59 | 64 |
60 def stop(self): | 65 def stop(self): |
61 if self.server: | 66 if self.server: |
62 self.server.terminate() | 67 self.server.terminate() |
63 | 68 |
64 def __enter__(self): | 69 def __enter__(self): |
65 self.start() | 70 self.start() |
66 | 71 |
67 def __exit__(self, exc_type, exc_value, traceback): | 72 def __exit__(self, exc_type, exc_value, traceback): |
68 self.stop() | 73 self.stop() |
69 | 74 |
70 def path_as_url(self, path): | 75 def path_as_url(self, path): |
71 return self.url_for_path(self.port, self.root, path) | 76 return self.url_for_path(self.port, self.root, path) |
72 | 77 |
73 @staticmethod | 78 @staticmethod |
74 def url_for_path(port, root, path): | 79 def url_for_path(port, root, path): |
75 relative_path = os.path.relpath(path, root) | 80 relative_path = os.path.relpath(path, root) |
76 return 'http://localhost:%s/%s' % (port, relative_path) | 81 return 'http://localhost:%s/%s' % (port, relative_path) |
OLD | NEW |