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 | 6 from skypy.paths import Paths |
7 from skypy.skyserver import SkyServer | 7 from skypy.skyserver import SkyServer |
8 import argparse | 8 import argparse |
| 9 import json |
9 import logging | 10 import logging |
10 import os | 11 import os |
| 12 import requests |
| 13 import signal |
11 import skypy.configuration as configuration | 14 import skypy.configuration as configuration |
12 import subprocess | 15 import subprocess |
13 import urlparse | 16 import urlparse |
14 | 17 |
15 | 18 |
16 SUPPORTED_MIME_TYPES = [ | 19 SUPPORTED_MIME_TYPES = [ |
17 'text/html', | 20 'text/html', |
18 'text/sky', | 21 'text/sky', |
19 'text/plain', | 22 'text/plain', |
20 ] | 23 ] |
21 | 24 |
22 | |
23 HTTP_PORT = 9999 | 25 HTTP_PORT = 9999 |
| 26 PID_FILE_PATH = "/tmp/skydb.pids" |
24 | 27 |
25 | 28 |
26 class SkyDebugger(object): | 29 class SkyDebugger(object): |
27 def __init__(self): | 30 def __init__(self): |
28 self.paths = None | 31 self.paths = None |
| 32 self.pids = {} |
| 33 # FIXME: This is not android aware nor aware of the port |
| 34 # skyserver is listening on. |
| 35 self.base_url = 'http://localhost:7777' |
29 | 36 |
30 def _server_root_and_url_from_path_arg(self, url_or_path): | 37 def _server_root_and_url_from_path_arg(self, url_or_path): |
31 # This is already a valid url we don't need a local server. | 38 # This is already a valid url we don't need a local server. |
32 if urlparse.urlparse(url_or_path).scheme: | 39 if urlparse.urlparse(url_or_path).scheme: |
33 return None, url_or_path | 40 return None, url_or_path |
34 | 41 |
35 path = os.path.abspath(url_or_path) | 42 path = os.path.abspath(url_or_path) |
36 if os.path.commonprefix([path, self.paths.src_root]) == self.paths.src_r
oot: | 43 if os.path.commonprefix([path, self.paths.src_root]) == self.paths.src_r
oot: |
37 server_root = self.paths.src_root | 44 server_root = self.paths.src_root |
38 else: | 45 else: |
39 server_root = os.path.dirname(path) | 46 server_root = os.path.dirname(path) |
40 logging.warn( | 47 logging.warn( |
41 '%s is outside of mojo root, using %s as server root' % | 48 '%s is outside of mojo root, using %s as server root' % |
42 (path, server_root)) | 49 (path, server_root)) |
43 local_url = SkyServer.url_for_path(HTTP_PORT, server_root, path) | 50 local_url = SkyServer.url_for_path(HTTP_PORT, server_root, path) |
44 return server_root, local_url | 51 return server_root, local_url |
45 | 52 |
46 def _in_chromoting(self): | 53 def _in_chromoting(self): |
47 return os.environ.get('CHROME_REMOTE_DESKTOP_SESSION', False) | 54 return os.environ.get('CHROME_REMOTE_DESKTOP_SESSION', False) |
48 | 55 |
49 def main(self): | 56 def _build_mojo_shell_command(self, args): |
50 logging.basicConfig(level=logging.INFO) | 57 sky_server = None |
51 | |
52 parser = argparse.ArgumentParser(description='Sky launcher/debugger') | |
53 parser.add_argument('--gdb', action='store_true') | |
54 parser.add_argument('--use-osmesa', action='store_true', | |
55 default=self._in_chromoting()) | |
56 parser.add_argument('url_or_path', nargs='?', type=str) | |
57 parser.add_argument('--show-command', action='store_true', | |
58 help='Display the shell command and exit') | |
59 configuration.add_arguments(parser) | |
60 args = parser.parse_args() | |
61 | |
62 self.paths = Paths(os.path.join('out', args.configuration)) | 58 self.paths = Paths(os.path.join('out', args.configuration)) |
63 | 59 |
64 content_handlers = ['%s,%s' % (mime_type, 'mojo:sky_viewer') | 60 content_handlers = ['%s,%s' % (mime_type, 'mojo:sky_viewer') |
65 for mime_type in SUPPORTED_MIME_TYPES] | 61 for mime_type in SUPPORTED_MIME_TYPES] |
66 shell_command = [ | 62 shell_command = [ |
67 self.paths.mojo_shell_path, | 63 self.paths.mojo_shell_path, |
68 '--v=1', | 64 '--v=1', |
69 '--content-handlers=%s' % ','.join(content_handlers), | 65 '--content-handlers=%s' % ','.join(content_handlers), |
70 '--url-mappings=mojo:window_manager=mojo:sky_debugger', | 66 '--url-mappings=mojo:window_manager=mojo:sky_debugger', |
71 'mojo:window_manager', | 67 'mojo:window_manager', |
72 ] | 68 ] |
73 if args.use_osmesa: | 69 if args.use_osmesa: |
74 shell_command.append('--args-for=mojo:native_viewport_service --use-
osmesa') | 70 shell_command.append('--args-for=mojo:native_viewport_service --use-
osmesa') |
75 | 71 |
76 server_root = None | |
77 | |
78 if args.url_or_path: | 72 if args.url_or_path: |
79 # Check if we need a local server for the url/path arg: | 73 # Check if we need a local server for the url/path arg: |
80 server_root, url = \ | 74 server_root, url = \ |
81 self._server_root_and_url_from_path_arg(args.url_or_path) | 75 self._server_root_and_url_from_path_arg(args.url_or_path) |
| 76 sky_server = SkyServer(self.paths, HTTP_PORT, args.configuration, |
| 77 server_root) |
| 78 |
82 prompt_args = '--args-for=mojo:sky_debugger_prompt %s' % url | 79 prompt_args = '--args-for=mojo:sky_debugger_prompt %s' % url |
83 shell_command.append(prompt_args) | 80 shell_command.append(prompt_args) |
84 | 81 |
85 if args.gdb: | 82 if args.gdb: |
86 shell_command = ['gdb', '--args'] + shell_command | 83 shell_command = ['gdb', '--args'] + shell_command |
87 | 84 |
88 if server_root: | 85 return shell_command, sky_server |
89 with SkyServer(self.paths, HTTP_PORT, args.configuration, | 86 |
90 server_root): | 87 def start_command(self, args): |
91 subprocess.check_call(shell_command) | 88 shell_command, sky_server = self._build_mojo_shell_command(args) |
92 else: | |
93 subprocess.check_call(shell_command) | |
94 if args.show_command: | 89 if args.show_command: |
95 print " ".join(shell_command) | 90 print " ".join(shell_command) |
| 91 return |
| 92 |
| 93 self.stop_command([]) # Quit any existing process. |
| 94 |
| 95 if sky_server: |
| 96 self.pids['sky_server_pid'] = sky_server.start() |
| 97 # self.pids['sky_server_port'] = sky_server.port |
| 98 # self.pids['sky_server_root'] = sky_server.root |
| 99 self.pids['mojo_shell_pid'] = subprocess.Popen(shell_command).pid |
| 100 |
| 101 def _kill_if_exists(self, key, name): |
| 102 pid = self.pids.pop(key, None) |
| 103 if not pid: |
| 104 logging.info('No pid for %s, nothing to do.' % name) |
| 105 return |
| 106 logging.info('Killing %s (%s).' % (name, pid)) |
| 107 try: |
| 108 os.kill(pid, signal.SIGTERM) |
| 109 except OSError: |
| 110 logging.info('%s (%s) already gone.' % (name, pid)) |
| 111 |
| 112 def stop_command(self, args): |
| 113 # FIXME: Send /quit to sky prompt instead of killing. |
| 114 # self._send_command_to_sky('/quit') |
| 115 self._kill_if_exists('mojo_shell_pid', 'mojo_shell') |
| 116 self._kill_if_exists('sky_server_pid', 'sky_server') |
| 117 |
| 118 def load_command(self, args): |
| 119 # Should resolve paths to relative urls like start does. |
| 120 # self.pids['sky_server_root'] and port should help. |
| 121 self._send_command_to_sky('/load', args.url_or_path) |
| 122 |
| 123 def _send_command_to_sky(self, command_path, payload=None): |
| 124 url = self.base_url + command_path |
| 125 if payload: |
| 126 response = requests.post(url, payload) |
96 else: | 127 else: |
97 subprocess.check_call(shell_command) | 128 response = requests.get(url) |
| 129 print response.text |
98 | 130 |
99 def shutdown(self): | 131 # FIXME: These could be made into a context object with __enter__/__exit__. |
100 print "Quitting" | 132 def _load_pid_file(self, path): |
101 if self._sky_server: | 133 try: |
102 self._sky_server.terminate() | 134 with open(path, 'r') as pid_file: |
| 135 return json.load(pid_file) |
| 136 except: |
| 137 if os.path.exists(path): |
| 138 logging.warn('Failed to read pid file: %s' % path) |
| 139 return {} |
| 140 |
| 141 def _write_pid_file(self, path, pids): |
| 142 try: |
| 143 with open(path, 'w') as pid_file: |
| 144 json.dump(pids, pid_file) |
| 145 except: |
| 146 logging.warn('Failed to write pid file: %s' % path) |
| 147 |
| 148 def _add_basic_command(self, subparsers, name, url_path, help_text): |
| 149 parser = subparsers.add_parser(name, help=help_text) |
| 150 command = lambda args: self._send_command_to_sky(url_path) |
| 151 parser.set_defaults(func=command) |
| 152 |
| 153 def main(self): |
| 154 logging.basicConfig(level=logging.INFO) |
| 155 |
| 156 self.pids = self._load_pid_file(PID_FILE_PATH) |
| 157 |
| 158 parser = argparse.ArgumentParser(description='Sky launcher/debugger') |
| 159 subparsers = parser.add_subparsers(help='sub-command help') |
| 160 |
| 161 start_parser = subparsers.add_parser('start', |
| 162 help='launch a new mojo_shell with sky') |
| 163 configuration.add_arguments(start_parser) |
| 164 start_parser.add_argument('--gdb', action='store_true') |
| 165 start_parser.add_argument('--use-osmesa', action='store_true', |
| 166 default=self._in_chromoting()) |
| 167 start_parser.add_argument('url_or_path', nargs='?', type=str) |
| 168 start_parser.add_argument('--show-command', action='store_true', |
| 169 help='Display the shell command and exit') |
| 170 start_parser.set_defaults(func=self.start_command) |
| 171 |
| 172 stop_parser = subparsers.add_parser('stop', |
| 173 help=('stop sky (as listed in %s)' % PID_FILE_PATH)) |
| 174 stop_parser.set_defaults(func=self.stop_command) |
| 175 |
| 176 self._add_basic_command(subparsers, 'trace', '/trace', |
| 177 'toggle tracing') |
| 178 self._add_basic_command(subparsers, 'reload', '/reload', |
| 179 'reload the current page') |
| 180 self._add_basic_command(subparsers, 'inspect', '/inspect', |
| 181 'stop the running sky instance') |
| 182 |
| 183 load_parser = subparsers.add_parser('load', |
| 184 help='load a new page in the currently running sky') |
| 185 load_parser.add_argument('url_or_path', type=str) |
| 186 load_parser.set_defaults(func=self.load_command) |
| 187 |
| 188 args = parser.parse_args() |
| 189 args.func(args) |
| 190 |
| 191 self._write_pid_file(PID_FILE_PATH, self.pids) |
103 | 192 |
104 | 193 |
105 if __name__ == '__main__': | 194 if __name__ == '__main__': |
106 SkyDebugger().main() | 195 SkyDebugger().main() |
OLD | NEW |