| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import argparse | 6 import argparse |
| 7 import logging | 7 import logging |
| 8 import os | 8 import os |
| 9 import skypy.paths as paths | 9 from skypy.paths import Paths |
| 10 import socket; | 10 import socket; |
| 11 import subprocess | 11 import subprocess |
| 12 import sys | 12 import sys |
| 13 import urlparse | 13 import urlparse |
| 14 | 14 |
| 15 | 15 |
| 16 SUPPORTED_MIME_TYPES = [ | 16 SUPPORTED_MIME_TYPES = [ |
| 17 'text/html', | 17 'text/html', |
| 18 'text/sky', | 18 'text/sky', |
| 19 'text/plain', | 19 'text/plain', |
| 20 ] | 20 ] |
| 21 | 21 |
| 22 | 22 |
| 23 class SkyDebugger(object): | 23 class SkyDebugger(object): |
| 24 def __init__(self): | 24 def __init__(self): |
| 25 self._sky_server = None | 25 self._sky_server = None |
| 26 self.paths = Paths(os.path.join('out', 'Debug')) |
| 26 | 27 |
| 27 @staticmethod | 28 @staticmethod |
| 28 def _port_in_use(port): | 29 def _port_in_use(port): |
| 29 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | 30 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 30 return sock.connect_ex(('localhost', port)) == 0 | 31 return sock.connect_ex(('localhost', port)) == 0 |
| 31 | 32 |
| 32 def _start_http_server_for_file(self, path): | 33 def _start_http_server_for_file(self, path): |
| 33 HTTP_PORT = 9999 | 34 HTTP_PORT = 9999 |
| 34 | 35 |
| 36 |
| 35 path = os.path.abspath(path) | 37 path = os.path.abspath(path) |
| 36 if os.path.commonprefix([path, paths.SRC_ROOT]) == paths.SRC_ROOT: | 38 if os.path.commonprefix([path, self.paths.src_root]) == self.paths.src_r
oot: |
| 37 server_root = paths.SRC_ROOT | 39 server_root = self.paths.src_root |
| 38 else: | 40 else: |
| 39 server_root = os.path.dirname(path) | 41 server_root = os.path.dirname(path) |
| 40 logging.warn( | 42 logging.warn( |
| 41 '%s is outside of mojo root, using %s as server root' % | 43 '%s is outside of mojo root, using %s as server root' % |
| 42 (path, server_root)) | 44 (path, server_root)) |
| 43 relative_path = os.path.relpath(path, server_root) | 45 relative_path = os.path.relpath(path, server_root) |
| 44 | 46 |
| 45 if self._port_in_use(HTTP_PORT): | 47 if self._port_in_use(HTTP_PORT): |
| 46 logging.warn( | 48 logging.warn( |
| 47 'Port %s already in use, assuming custom sky_server started.' % | 49 'Port %s already in use, assuming custom sky_server started.' % |
| 48 HTTP_PORT) | 50 HTTP_PORT) |
| 49 else: | 51 else: |
| 50 server_command = [ | 52 server_command = [ |
| 51 os.path.join(paths.SKY_TOOLS_DIRECTORY, 'sky_server'), | 53 os.path.join(self.paths.sky_tools_directory, 'sky_server'), |
| 54 '--debug', |
| 52 server_root, | 55 server_root, |
| 53 str(HTTP_PORT), | 56 str(HTTP_PORT), |
| 54 ] | 57 ] |
| 55 self._sky_server = subprocess.Popen(server_command) | 58 self._sky_server = subprocess.Popen(server_command) |
| 56 return 'http://localhost:%s/%s' % (HTTP_PORT, relative_path) | 59 return 'http://localhost:%s/%s' % (HTTP_PORT, relative_path) |
| 57 | 60 |
| 58 def main(self): | 61 def main(self): |
| 59 logging.basicConfig(level=logging.INFO) | 62 logging.basicConfig(level=logging.INFO) |
| 60 | 63 |
| 61 parser = argparse.ArgumentParser(description='Sky launcher/debugger') | 64 parser = argparse.ArgumentParser(description='Sky launcher/debugger') |
| 62 parser.add_argument('--gdb', action='store_true') | 65 parser.add_argument('--gdb', action='store_true') |
| 63 parser.add_argument('--use-osmesa', action='store_true') | 66 parser.add_argument('--use-osmesa', action='store_true') |
| 64 parser.add_argument('url', nargs='?', type=str) | 67 parser.add_argument('url', nargs='?', type=str) |
| 65 args = parser.parse_args() | 68 args = parser.parse_args() |
| 66 | 69 |
| 67 content_handlers = ['%s,%s' % (mime_type, 'mojo://sky_viewer/') | 70 content_handlers = ['%s,%s' % (mime_type, 'mojo://sky_viewer/') |
| 68 for mime_type in SUPPORTED_MIME_TYPES] | 71 for mime_type in SUPPORTED_MIME_TYPES] |
| 69 shell_command = [ | 72 shell_command = [ |
| 70 paths.MOJO_SHELL_PATH, | 73 self.paths.mojo_shell_path, |
| 71 '--v=1', | 74 '--v=1', |
| 72 '--content-handlers=%s' % ','.join(content_handlers), | 75 '--content-handlers=%s' % ','.join(content_handlers), |
| 73 '--url-mappings=mojo:window_manager=mojo:sky_debugger', | 76 '--url-mappings=mojo:window_manager=mojo:sky_debugger', |
| 74 'mojo:window_manager', | 77 'mojo:window_manager', |
| 75 ] | 78 ] |
| 76 if args.url: | 79 if args.url: |
| 77 url = args.url | 80 url = args.url |
| 78 if not urlparse.urlparse(url).scheme: | 81 if not urlparse.urlparse(url).scheme: |
| 79 url = self._start_http_server_for_file(url) | 82 url = self._start_http_server_for_file(url) |
| 80 | 83 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 94 | 97 |
| 95 | 98 |
| 96 if __name__ == '__main__': | 99 if __name__ == '__main__': |
| 97 skydb = SkyDebugger() | 100 skydb = SkyDebugger() |
| 98 try: | 101 try: |
| 99 skydb.main() | 102 skydb.main() |
| 100 except (KeyboardInterrupt, SystemExit): | 103 except (KeyboardInterrupt, SystemExit): |
| 101 pass | 104 pass |
| 102 finally: | 105 finally: |
| 103 skydb.shutdown() | 106 skydb.shutdown() |
| OLD | NEW |