| 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 os | 8 import os |
| 9 import skypy.paths as paths |
| 10 import socket; |
| 8 import subprocess | 11 import subprocess |
| 9 import sys | 12 import sys |
| 10 import urlparse | 13 import urlparse |
| 11 import logging | |
| 12 import socket; | |
| 13 | 14 |
| 14 | 15 |
| 15 OUT_DIR = 'out' | |
| 16 CONFIG_NAME = 'Debug' | |
| 17 SKY_TOOLS_DIRECTORY = os.path.abspath(os.path.join(__file__, os.pardir)) | |
| 18 MOJO_ROOT = os.path.abspath(os.path.join(SKY_TOOLS_DIRECTORY, os.pardir, | |
| 19 os.pardir)) | |
| 20 MOJO_SHELL_PATH = os.path.join(MOJO_ROOT, OUT_DIR, CONFIG_NAME, 'mojo_shell') | |
| 21 | |
| 22 SUPPORTED_MIME_TYPES = [ | 16 SUPPORTED_MIME_TYPES = [ |
| 23 'text/html', | 17 'text/html', |
| 24 'text/sky', | 18 'text/sky', |
| 25 'text/plain', | 19 'text/plain', |
| 26 ] | 20 ] |
| 27 | 21 |
| 22 |
| 28 class SkyDebugger(object): | 23 class SkyDebugger(object): |
| 29 def __init__(self): | 24 def __init__(self): |
| 30 self._sky_server = None | 25 self._sky_server = None |
| 31 | 26 |
| 32 @staticmethod | 27 @staticmethod |
| 33 def _port_in_use(port): | 28 def _port_in_use(port): |
| 34 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | 29 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 35 return sock.connect_ex(('localhost', port)) == 0 | 30 return sock.connect_ex(('localhost', port)) == 0 |
| 36 | 31 |
| 37 def _start_http_server_for_file(self, path): | 32 def _start_http_server_for_file(self, path): |
| 38 HTTP_PORT = 9999 | 33 HTTP_PORT = 9999 |
| 39 | 34 |
| 40 path = os.path.abspath(path) | 35 path = os.path.abspath(path) |
| 41 if os.path.commonprefix([path, MOJO_ROOT]) == MOJO_ROOT: | 36 if os.path.commonprefix([path, paths.SRC_ROOT]) == paths.SRC_ROOT: |
| 42 server_root = MOJO_ROOT | 37 server_root = paths.SRC_ROOT |
| 43 else: | 38 else: |
| 44 server_root = os.path.dirname(path) | 39 server_root = os.path.dirname(path) |
| 45 logging.warn( | 40 logging.warn( |
| 46 '%s is outside of mojo root, using %s as server root' % | 41 '%s is outside of mojo root, using %s as server root' % |
| 47 (path, server_root)) | 42 (path, server_root)) |
| 48 relative_path = os.path.relpath(path, server_root) | 43 relative_path = os.path.relpath(path, server_root) |
| 49 | 44 |
| 50 if self._port_in_use(HTTP_PORT): | 45 if self._port_in_use(HTTP_PORT): |
| 51 logging.warn( | 46 logging.warn( |
| 52 'Port %s already in use, assuming custom sky_server started.' % | 47 'Port %s already in use, assuming custom sky_server started.' % |
| 53 HTTP_PORT) | 48 HTTP_PORT) |
| 54 else: | 49 else: |
| 55 server_command = [ | 50 server_command = [ |
| 56 os.path.join(SKY_TOOLS_DIRECTORY, 'sky_server'), | 51 os.path.join(paths.SKY_TOOLS_DIRECTORY, 'sky_server'), |
| 57 server_root, | 52 server_root, |
| 58 str(HTTP_PORT), | 53 str(HTTP_PORT), |
| 59 ] | 54 ] |
| 60 self._sky_server = subprocess.Popen(server_command) | 55 self._sky_server = subprocess.Popen(server_command) |
| 61 return 'http://localhost:%s/%s' % (HTTP_PORT, relative_path) | 56 return 'http://localhost:%s/%s' % (HTTP_PORT, relative_path) |
| 62 | 57 |
| 63 def main(self): | 58 def main(self): |
| 64 logging.basicConfig(level=logging.INFO) | 59 logging.basicConfig(level=logging.INFO) |
| 65 | 60 |
| 66 parser = argparse.ArgumentParser(description='Sky launcher/debugger') | 61 parser = argparse.ArgumentParser(description='Sky launcher/debugger') |
| 67 parser.add_argument('--gdb', action='store_true') | 62 parser.add_argument('--gdb', action='store_true') |
| 68 parser.add_argument('--use-osmesa', action='store_true') | 63 parser.add_argument('--use-osmesa', action='store_true') |
| 69 parser.add_argument('url', nargs='?', type=str) | 64 parser.add_argument('url', nargs='?', type=str) |
| 70 args = parser.parse_args() | 65 args = parser.parse_args() |
| 71 | 66 |
| 72 content_handlers = ['%s,%s' % (mime_type, 'mojo://sky_viewer/') | 67 content_handlers = ['%s,%s' % (mime_type, 'mojo://sky_viewer/') |
| 73 for mime_type in SUPPORTED_MIME_TYPES] | 68 for mime_type in SUPPORTED_MIME_TYPES] |
| 74 shell_command = [ | 69 shell_command = [ |
| 75 MOJO_SHELL_PATH, | 70 paths.MOJO_SHELL_PATH, |
| 76 '--v=1', | 71 '--v=1', |
| 77 '--content-handlers=%s' % ','.join(content_handlers), | 72 '--content-handlers=%s' % ','.join(content_handlers), |
| 78 '--url-mappings=mojo:window_manager=mojo:sky_debugger', | 73 '--url-mappings=mojo:window_manager=mojo:sky_debugger', |
| 79 'mojo:window_manager', | 74 'mojo:window_manager', |
| 80 ] | 75 ] |
| 81 if args.url: | 76 if args.url: |
| 82 url = args.url | 77 url = args.url |
| 83 if not urlparse.urlparse(url).scheme: | 78 if not urlparse.urlparse(url).scheme: |
| 84 url = self._start_http_server_for_file(url) | 79 url = self._start_http_server_for_file(url) |
| 85 | 80 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 99 | 94 |
| 100 | 95 |
| 101 if __name__ == '__main__': | 96 if __name__ == '__main__': |
| 102 skydb = SkyDebugger() | 97 skydb = SkyDebugger() |
| 103 try: | 98 try: |
| 104 skydb.main() | 99 skydb.main() |
| 105 except (KeyboardInterrupt, SystemExit): | 100 except (KeyboardInterrupt, SystemExit): |
| 106 pass | 101 pass |
| 107 finally: | 102 finally: |
| 108 skydb.shutdown() | 103 skydb.shutdown() |
| OLD | NEW |