| 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 stat |
| 10 import platform | 10 import platform |
| 11 | 11 |
| 12 SKYPY_PATH = os.path.dirname(os.path.abspath(__file__)) | 12 SKYPY_PATH = os.path.dirname(os.path.abspath(__file__)) |
| 13 SKY_TOOLS_PATH = os.path.dirname(SKYPY_PATH) | 13 SKY_TOOLS_PATH = os.path.dirname(SKYPY_PATH) |
| 14 SKYGO_PATH = os.path.join(SKY_TOOLS_PATH, 'skygo') | 14 SKYGO_PATH = os.path.join(SKY_TOOLS_PATH, 'skygo') |
| 15 SKY_ROOT = os.path.dirname(SKY_TOOLS_PATH) | 15 SKY_ROOT = os.path.dirname(SKY_TOOLS_PATH) |
| 16 SRC_ROOT = os.path.dirname(SKY_ROOT) | 16 SRC_ROOT = os.path.dirname(SKY_ROOT) |
| 17 | 17 |
| 18 class SkyServer(object): | 18 class SkyServer(object): |
| 19 def __init__(self, port, configuration, root, package_root): | 19 def __init__(self, port, root, package_root): |
| 20 self.port = port | 20 self.port = port |
| 21 self.configuration = configuration | |
| 22 self.root = root | 21 self.root = root |
| 23 self.package_root = package_root | 22 self.package_root = package_root |
| 24 self.server = None | 23 self.server = None |
| 25 | 24 |
| 26 @staticmethod | 25 @staticmethod |
| 27 def _port_in_use(port): | 26 def _port_in_use(port): |
| 28 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | 27 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 29 return sock.connect_ex(('localhost', port)) == 0 | 28 return sock.connect_ex(('localhost', port)) == 0 |
| 30 | 29 |
| 31 @staticmethod | 30 @staticmethod |
| (...skipping 15 matching lines...) Expand all Loading... |
| 47 self.port) | 46 self.port) |
| 48 return | 47 return |
| 49 | 48 |
| 50 server_path = self.sky_server_path() | 49 server_path = self.sky_server_path() |
| 51 st = os.stat(self.sky_server_path()) | 50 st = os.stat(self.sky_server_path()) |
| 52 if not (stat.S_IXUSR & st[stat.ST_MODE]): | 51 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()) | 52 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) | 53 os.chmod(self.sky_server_path(), st[stat.ST_MODE] | stat.S_IEXEC) |
| 55 server_command = [ | 54 server_command = [ |
| 56 server_path, | 55 server_path, |
| 57 '-t', self.configuration, | 56 '-p', str(self.port), |
| 58 self.root, | 57 self.root, |
| 59 str(self.port), | |
| 60 self.package_root, | 58 self.package_root, |
| 61 ] | 59 ] |
| 62 self.server = subprocess.Popen(server_command) | 60 self.server = subprocess.Popen(server_command) |
| 63 return self.server.pid | 61 return self.server.pid |
| 64 | 62 |
| 65 def stop(self): | 63 def stop(self): |
| 66 if self.server: | 64 if self.server: |
| 67 self.server.terminate() | 65 self.server.terminate() |
| 68 | 66 |
| 69 def __enter__(self): | 67 def __enter__(self): |
| 70 self.start() | 68 self.start() |
| 71 | 69 |
| 72 def __exit__(self, exc_type, exc_value, traceback): | 70 def __exit__(self, exc_type, exc_value, traceback): |
| 73 self.stop() | 71 self.stop() |
| 74 | 72 |
| 75 def path_as_url(self, path): | 73 def path_as_url(self, path): |
| 76 return self.url_for_path(self.port, self.root, path) | 74 return self.url_for_path(self.port, self.root, path) |
| 77 | 75 |
| 78 @staticmethod | 76 @staticmethod |
| 79 def url_for_path(port, root, path): | 77 def url_for_path(port, root, path): |
| 80 relative_path = os.path.relpath(path, root) | 78 relative_path = os.path.relpath(path, root) |
| 81 return 'http://localhost:%s/%s' % (port, relative_path) | 79 return 'http://localhost:%s/%s' % (port, relative_path) |
| OLD | NEW |