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