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