| 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 from skypy.paths import Paths | 9 from skypy.paths import Paths |
| 10 import skypy.configuration as configuration |
| 10 import socket; | 11 import socket; |
| 11 import subprocess | 12 import subprocess |
| 12 import sys | 13 import sys |
| 13 import urlparse | 14 import urlparse |
| 14 | 15 |
| 15 | 16 |
| 16 SUPPORTED_MIME_TYPES = [ | 17 SUPPORTED_MIME_TYPES = [ |
| 17 'text/html', | 18 'text/html', |
| 18 'text/sky', | 19 'text/sky', |
| 19 'text/plain', | 20 'text/plain', |
| 20 ] | 21 ] |
| 21 | 22 |
| 22 | 23 |
| 23 class SkyDebugger(object): | 24 class SkyDebugger(object): |
| 24 def __init__(self): | 25 def __init__(self): |
| 25 self._sky_server = None | 26 self._sky_server = None |
| 26 self.paths = Paths(os.path.join('out', 'Debug')) | 27 self.paths = None |
| 27 | 28 |
| 28 @staticmethod | 29 @staticmethod |
| 29 def _port_in_use(port): | 30 def _port_in_use(port): |
| 30 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | 31 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 31 return sock.connect_ex(('localhost', port)) == 0 | 32 return sock.connect_ex(('localhost', port)) == 0 |
| 32 | 33 |
| 33 def _start_http_server_for_file(self, path): | 34 def _start_http_server_for_file(self, path, configuration): |
| 34 HTTP_PORT = 9999 | 35 HTTP_PORT = 9999 |
| 35 | 36 |
| 36 | 37 |
| 37 path = os.path.abspath(path) | 38 path = os.path.abspath(path) |
| 38 if os.path.commonprefix([path, self.paths.src_root]) == self.paths.src_r
oot: | 39 if os.path.commonprefix([path, self.paths.src_root]) == self.paths.src_r
oot: |
| 39 server_root = self.paths.src_root | 40 server_root = self.paths.src_root |
| 40 else: | 41 else: |
| 41 server_root = os.path.dirname(path) | 42 server_root = os.path.dirname(path) |
| 42 logging.warn( | 43 logging.warn( |
| 43 '%s is outside of mojo root, using %s as server root' % | 44 '%s is outside of mojo root, using %s as server root' % |
| 44 (path, server_root)) | 45 (path, server_root)) |
| 45 relative_path = os.path.relpath(path, server_root) | 46 relative_path = os.path.relpath(path, server_root) |
| 46 | 47 |
| 47 if self._port_in_use(HTTP_PORT): | 48 if self._port_in_use(HTTP_PORT): |
| 48 logging.warn( | 49 logging.warn( |
| 49 'Port %s already in use, assuming custom sky_server started.' % | 50 'Port %s already in use, assuming custom sky_server started.' % |
| 50 HTTP_PORT) | 51 HTTP_PORT) |
| 51 else: | 52 else: |
| 52 server_command = [ | 53 server_command = [ |
| 53 os.path.join(self.paths.sky_tools_directory, 'sky_server'), | 54 os.path.join(self.paths.sky_tools_directory, 'sky_server'), |
| 54 '--debug', | 55 '-t', configuration, |
| 55 server_root, | 56 server_root, |
| 56 str(HTTP_PORT), | 57 str(HTTP_PORT), |
| 57 ] | 58 ] |
| 58 self._sky_server = subprocess.Popen(server_command) | 59 self._sky_server = subprocess.Popen(server_command) |
| 59 return 'http://localhost:%s/%s' % (HTTP_PORT, relative_path) | 60 return 'http://localhost:%s/%s' % (HTTP_PORT, relative_path) |
| 60 | 61 |
| 61 def _in_chromoting(self): | 62 def _in_chromoting(self): |
| 62 return os.environ.get('CHROME_REMOTE_DESKTOP_SESSION', False) | 63 return os.environ.get('CHROME_REMOTE_DESKTOP_SESSION', False) |
| 63 | 64 |
| 64 def main(self): | 65 def main(self): |
| 65 logging.basicConfig(level=logging.INFO) | 66 logging.basicConfig(level=logging.INFO) |
| 66 | 67 |
| 67 parser = argparse.ArgumentParser(description='Sky launcher/debugger') | 68 parser = argparse.ArgumentParser(description='Sky launcher/debugger') |
| 68 parser.add_argument('--gdb', action='store_true') | 69 parser.add_argument('--gdb', action='store_true') |
| 69 parser.add_argument('--use-osmesa', action='store_true', | 70 parser.add_argument('--use-osmesa', action='store_true', |
| 70 default=self._in_chromoting()) | 71 default=self._in_chromoting()) |
| 71 parser.add_argument('url', nargs='?', type=str) | 72 parser.add_argument('url', nargs='?', type=str) |
| 73 configuration.add_arguments(parser) |
| 72 args = parser.parse_args() | 74 args = parser.parse_args() |
| 73 | 75 |
| 76 self.paths = Paths(os.path.join('out', args.configuration)) |
| 77 |
| 74 content_handlers = ['%s,%s' % (mime_type, 'mojo:sky_viewer') | 78 content_handlers = ['%s,%s' % (mime_type, 'mojo:sky_viewer') |
| 75 for mime_type in SUPPORTED_MIME_TYPES] | 79 for mime_type in SUPPORTED_MIME_TYPES] |
| 76 shell_command = [ | 80 shell_command = [ |
| 77 self.paths.mojo_shell_path, | 81 self.paths.mojo_shell_path, |
| 78 '--v=1', | 82 '--v=1', |
| 79 '--content-handlers=%s' % ','.join(content_handlers), | 83 '--content-handlers=%s' % ','.join(content_handlers), |
| 80 '--url-mappings=mojo:window_manager=mojo:sky_debugger', | 84 '--url-mappings=mojo:window_manager=mojo:sky_debugger', |
| 81 'mojo:window_manager', | 85 'mojo:window_manager', |
| 82 ] | 86 ] |
| 83 if args.url: | 87 if args.url: |
| 84 url = args.url | 88 url = args.url |
| 85 if not urlparse.urlparse(url).scheme: | 89 if not urlparse.urlparse(url).scheme: |
| 86 url = self._start_http_server_for_file(url) | 90 url = self._start_http_server_for_file(url, args.configuration) |
| 87 | 91 |
| 88 prompt_args = '--args-for=mojo:sky_debugger_prompt %s' % url | 92 prompt_args = '--args-for=mojo:sky_debugger_prompt %s' % url |
| 89 shell_command.append(prompt_args) | 93 shell_command.append(prompt_args) |
| 90 if args.use_osmesa: | 94 if args.use_osmesa: |
| 91 shell_command.append('--args-for=mojo:native_viewport_service --use-
osmesa') | 95 shell_command.append('--args-for=mojo:native_viewport_service --use-
osmesa') |
| 92 if args.gdb: | 96 if args.gdb: |
| 93 shell_command = ['gdb', '--args'] + shell_command | 97 shell_command = ['gdb', '--args'] + shell_command |
| 94 | 98 |
| 95 subprocess.check_call(shell_command) | 99 subprocess.check_call(shell_command) |
| 96 | 100 |
| 97 def shutdown(self): | 101 def shutdown(self): |
| 98 print "Quitting" | 102 print "Quitting" |
| 99 if self._sky_server: | 103 if self._sky_server: |
| 100 self._sky_server.terminate() | 104 self._sky_server.terminate() |
| 101 | 105 |
| 102 | 106 |
| 103 if __name__ == '__main__': | 107 if __name__ == '__main__': |
| 104 skydb = SkyDebugger() | 108 skydb = SkyDebugger() |
| 105 try: | 109 try: |
| 106 skydb.main() | 110 skydb.main() |
| 107 except (KeyboardInterrupt, SystemExit): | 111 except (KeyboardInterrupt, SystemExit): |
| 108 pass | 112 pass |
| 109 finally: | 113 finally: |
| 110 skydb.shutdown() | 114 skydb.shutdown() |
| OLD | NEW |