Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2015 The Chromium Authors. All rights reserved. | 2 # Copyright 2015 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 json | 7 import json |
| 8 import logging | 8 import logging |
| 9 import os | 9 import os |
| 10 import re | 10 import re |
| 11 import signal | 11 import signal |
| 12 import socket | 12 import socket |
| 13 import subprocess | 13 import subprocess |
| 14 import sys | 14 import sys |
| 15 import urlparse | 15 import urlparse |
| 16 import time | 16 import time |
| 17 | 17 |
| 18 # TODO(eseidel): This should be BIN_DIR. | 18 # TODO(eseidel): This should be BIN_DIR. |
| 19 LIB_DIR = os.path.realpath(os.path.dirname(os.path.abspath(__file__))) | 19 LIB_DIR = os.path.realpath(os.path.dirname(os.path.abspath(__file__))) |
| 20 SKY_PACKAGE_ROOT = os.path.realpath(os.path.dirname(LIB_DIR)) | 20 SKY_PACKAGE_ROOT = os.path.realpath(os.path.dirname(LIB_DIR)) |
| 21 | 21 |
| 22 SKY_SERVER_PORT = 9888 | 22 SKY_SERVER_PORT = 9888 |
| 23 APK_NAME = 'SkyDemo.apk' | 23 APK_NAME = 'SkyDemo.apk' |
| 24 ANDROID_PACKAGE = "org.domokit.sky.demo" | 24 ANDROID_PACKAGE = "org.domokit.sky.demo" |
| 25 # FIXME: This assumes adb is in $PATH, we could look for ANDROID_HOME, etc? | 25 # FIXME: This assumes adb is in $PATH, we could look for ANDROID_HOME, etc? |
| 26 ADB_PATH = 'adb' | 26 ADB_PATH = 'adb' |
| 27 # FIXME: Do we need to look in $DART_SDK? | |
| 28 DART_PATH = 'dart' | |
|
Cutch
2015/06/23 18:29:47
We probably will need to check in $DART_SDK (somed
| |
| 27 | 29 |
| 28 PID_FILE_PATH = "/tmp/sky_tool.pids" | 30 PID_FILE_PATH = "/tmp/sky_tool.pids" |
| 29 PID_FILE_KEYS = frozenset([ | 31 PID_FILE_KEYS = frozenset([ |
| 30 'remote_sky_server_port', | 32 'remote_sky_server_port', |
| 31 'sky_server_pid', | 33 'sky_server_pid', |
| 32 'sky_server_port', | 34 'sky_server_port', |
| 33 'sky_server_root', | 35 'sky_server_root', |
| 34 'build_dir', | 36 'build_dir', |
| 35 ]) | 37 ]) |
| 36 | 38 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 120 default='main.dart') | 122 default='main.dart') |
| 121 start_parser.set_defaults(func=self.run) | 123 start_parser.set_defaults(func=self.run) |
| 122 | 124 |
| 123 def _is_package_installed(self, package_name): | 125 def _is_package_installed(self, package_name): |
| 124 pm_path_cmd = [ADB_PATH, 'shell', 'pm', 'path', package_name] | 126 pm_path_cmd = [ADB_PATH, 'shell', 'pm', 'path', package_name] |
| 125 return subprocess.check_output(pm_path_cmd).strip() != '' | 127 return subprocess.check_output(pm_path_cmd).strip() != '' |
| 126 | 128 |
| 127 def run(self, args, pids): | 129 def run(self, args, pids): |
| 128 StopSky().run(args, pids) | 130 StopSky().run(args, pids) |
| 129 | 131 |
| 132 project_or_path = os.path.abspath(args.project_or_path) | |
| 133 | |
| 134 if os.path.isdir(project_or_path): | |
| 135 sky_server_root = project_or_path | |
| 136 main_dart = os.path.join(project_or_path, 'main.dart') | |
| 137 missing_msg = "Missing main.dart in project: %s" % sky_server_root | |
| 138 else: | |
| 139 # FIXME: This assumes the path is at the root of the project! | |
| 140 # Instead we should walk up looking for a pubspec.yaml | |
| 141 sky_server_root = os.path.dirname(project_or_path) | |
| 142 main_dart = project_or_path | |
| 143 missing_msg = "%s does not exist." % main_dart | |
| 144 | |
| 145 if not os.path.isfile(main_dart): | |
| 146 print missing_msg | |
| 147 return 2 | |
| 148 | |
| 149 package_root = os.path.join(sky_server_root, 'packages') | |
| 150 if not os.path.isdir(package_root): | |
| 151 print "%s is not a valid packages path." % package_root | |
| 152 return 2 | |
| 153 | |
| 154 subprocess.check_call([ | |
| 155 DART_PATH, | |
| 156 '--package-root=%s' % package_root, | |
| 157 'packages/mojom/generate.dart' | |
| 158 ]) | |
| 159 | |
| 130 if not self._is_package_installed(ANDROID_PACKAGE): | 160 if not self._is_package_installed(ANDROID_PACKAGE): |
| 131 print '%s is not installed, installing.' % APK_NAME | 161 print '%s is not installed, installing.' % APK_NAME |
| 132 args.install = True | 162 args.install = True |
| 133 | 163 |
| 134 if args.install: | 164 if args.install: |
| 135 apk_path = os.path.join(SKY_PACKAGE_ROOT, 'apks', APK_NAME) | 165 apk_path = os.path.join(SKY_PACKAGE_ROOT, 'apks', APK_NAME) |
| 136 if not os.path.exists(apk_path): | 166 if not os.path.exists(apk_path): |
| 137 print "'%s' does not exist?" % apk_path | 167 print "'%s' does not exist?" % apk_path |
| 138 return 2 | 168 return 2 |
| 139 | 169 |
| 140 subprocess.check_call([ADB_PATH, 'install', '-r', apk_path]) | 170 subprocess.check_call([ADB_PATH, 'install', '-r', apk_path]) |
| 141 | 171 |
| 142 project_or_path = os.path.abspath(args.project_or_path) | |
| 143 | |
| 144 if os.path.isdir(project_or_path): | |
| 145 sky_server_root = project_or_path | |
| 146 main_sky = os.path.join(project_or_path, 'main.dart') | |
| 147 missing_msg = "Missing main.dart in project: %s" % sky_server_root | |
| 148 else: | |
| 149 sky_server_root = os.path.dirname(project_or_path) | |
| 150 main_sky = project_or_path | |
| 151 missing_msg = "%s does not exist." % main_sky | |
| 152 | |
| 153 if not os.path.isfile(main_sky): | |
| 154 print missing_msg | |
| 155 return 2 | |
| 156 | |
| 157 sky_server_port = SKY_SERVER_PORT | 172 sky_server_port = SKY_SERVER_PORT |
| 158 pids['sky_server_port'] = sky_server_port | 173 pids['sky_server_port'] = sky_server_port |
| 159 if _port_in_use(sky_server_port): | 174 if _port_in_use(sky_server_port): |
| 160 logging.warn(('Port %s already in use. ' | 175 logging.warn(('Port %s already in use. ' |
| 161 ' Not starting server for %s') % (sky_server_port, sky_server_root)) | 176 ' Not starting server for %s') % (sky_server_port, sky_server_root)) |
| 162 else: | 177 else: |
| 163 sky_server_pid = _start_http_server(sky_server_port, sky_server_root ) | 178 sky_server_pid = _start_http_server(sky_server_port, sky_server_root ) |
| 164 pids['sky_server_pid'] = sky_server_pid | 179 pids['sky_server_pid'] = sky_server_pid |
| 165 pids['sky_server_root'] = sky_server_root | 180 pids['sky_server_root'] = sky_server_root |
| 166 | 181 |
| 167 port_string = 'tcp:%s' % sky_server_port | 182 port_string = 'tcp:%s' % sky_server_port |
| 168 subprocess.check_call([ | 183 subprocess.check_call([ |
| 169 ADB_PATH, 'reverse', port_string, port_string | 184 ADB_PATH, 'reverse', port_string, port_string |
| 170 ]) | 185 ]) |
| 171 pids['remote_sky_server_port'] = sky_server_port | 186 pids['remote_sky_server_port'] = sky_server_port |
| 172 | 187 |
| 173 # The load happens on the remote device, use the remote port. | 188 # The load happens on the remote device, use the remote port. |
| 174 sky_url = _url_for_path(pids['remote_sky_server_port'], sky_server_root, | 189 sky_url = _url_for_path(pids['remote_sky_server_port'], sky_server_root, |
| 175 main_sky) | 190 main_dart) |
| 176 | 191 |
| 177 subprocess.check_call([ADB_PATH, 'shell', | 192 subprocess.check_call([ADB_PATH, 'shell', |
| 178 'am', 'start', | 193 'am', 'start', |
| 179 '-a', 'android.intent.action.VIEW', | 194 '-a', 'android.intent.action.VIEW', |
| 180 '-d', sky_url]) | 195 '-d', sky_url]) |
| 181 | 196 |
| 182 | 197 |
| 183 class StopSky(object): | 198 class StopSky(object): |
| 184 def add_subparser(self, subparsers): | 199 def add_subparser(self, subparsers): |
| 185 stop_parser = subparsers.add_parser('stop', | 200 stop_parser = subparsers.add_parser('stop', |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 | 272 |
| 258 class SkyShellRunner(object): | 273 class SkyShellRunner(object): |
| 259 def _check_for_adb(self): | 274 def _check_for_adb(self): |
| 260 try: | 275 try: |
| 261 subprocess.check_output([ADB_PATH, 'devices']) | 276 subprocess.check_output([ADB_PATH, 'devices']) |
| 262 except OSError: | 277 except OSError: |
| 263 print "'adb' (from the Android SDK) not in $PATH, can't continue." | 278 print "'adb' (from the Android SDK) not in $PATH, can't continue." |
| 264 return False | 279 return False |
| 265 return True | 280 return True |
| 266 | 281 |
| 282 def _check_for_dart(self): | |
| 283 try: | |
| 284 subprocess.check_output([DART_PATH, '--version']) | |
| 285 except OSError: | |
| 286 print "'dart' (from the Dart SDK) not in $PATH, can't continue." | |
| 287 return False | |
| 288 return True | |
| 289 | |
| 267 def main(self): | 290 def main(self): |
| 268 logging.basicConfig(level=logging.WARNING) | 291 logging.basicConfig(level=logging.WARNING) |
| 269 if not self._check_for_adb(): | 292 if not self._check_for_adb() or not self._check_for_dart(): |
| 270 sys.exit(2) | 293 sys.exit(2) |
| 271 | 294 |
| 272 parser = argparse.ArgumentParser(description='Sky Demo Runner') | 295 parser = argparse.ArgumentParser(description='Sky Demo Runner') |
| 273 subparsers = parser.add_subparsers(help='sub-command help') | 296 subparsers = parser.add_subparsers(help='sub-command help') |
| 274 | 297 |
| 275 for command in [StartSky(), StopSky(), StartTracing(), StopTracing()]: | 298 for command in [StartSky(), StopSky(), StartTracing(), StopTracing()]: |
| 276 command.add_subparser(subparsers) | 299 command.add_subparser(subparsers) |
| 277 | 300 |
| 278 args = parser.parse_args() | 301 args = parser.parse_args() |
| 279 pids = Pids.read_from(PID_FILE_PATH, PID_FILE_KEYS) | 302 pids = Pids.read_from(PID_FILE_PATH, PID_FILE_KEYS) |
| 280 exit_code = args.func(args, pids) | 303 exit_code = args.func(args, pids) |
| 281 # We could do this with an at-exit handler instead? | 304 # We could do this with an at-exit handler instead? |
| 282 pids.write_to(PID_FILE_PATH) | 305 pids.write_to(PID_FILE_PATH) |
| 283 sys.exit(exit_code) | 306 sys.exit(exit_code) |
| 284 | 307 |
| 285 | 308 |
| 286 if __name__ == '__main__': | 309 if __name__ == '__main__': |
| 287 SkyShellRunner().main() | 310 SkyShellRunner().main() |
| OLD | NEW |