| 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 from skypy.skyserver import SkyServer | 6 from skypy.skyserver import SkyServer |
| 7 import argparse | 7 import argparse |
| 8 import json | 8 import json |
| 9 import logging | 9 import logging |
| 10 import os | 10 import os |
| 11 import re | 11 import re |
| 12 import signal | 12 import signal |
| 13 import subprocess | 13 import subprocess |
| 14 import sys | 14 import sys |
| 15 import time | 15 import time |
| 16 import urlparse | 16 import urlparse |
| 17 import hashlib |
| 18 import tempfile |
| 17 | 19 |
| 18 SKY_TOOLS_DIR = os.path.dirname(os.path.abspath(__file__)) | 20 SKY_TOOLS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 19 SKY_ROOT = os.path.dirname(SKY_TOOLS_DIR) | 21 SKY_ROOT = os.path.dirname(SKY_TOOLS_DIR) |
| 20 SRC_ROOT = os.path.dirname(SKY_ROOT) | 22 SRC_ROOT = os.path.dirname(SKY_ROOT) |
| 21 | 23 |
| 22 SKY_SERVER_PORT = 9888 | 24 SKY_SERVER_PORT = 9888 |
| 23 OBSERVATORY_PORT = 8181 | 25 OBSERVATORY_PORT = 8181 |
| 24 DEFAULT_URL = "sky://domokit.github.io/sky_home" | 26 DEFAULT_URL = "sky://domokit.github.io/sky_home" |
| 25 APK_NAME = 'SkyDemo.apk' | 27 APK_NAME = 'SkyDemo.apk' |
| 26 ADB_PATH = os.path.join(SRC_ROOT, | 28 ADB_PATH = os.path.join(SRC_ROOT, |
| 27 'third_party/android_tools/sdk/platform-tools/adb') | 29 'third_party/android_tools/sdk/platform-tools/adb') |
| 28 ANDROID_PACKAGE = "org.domokit.sky.demo" | 30 ANDROID_PACKAGE = "org.domokit.sky.demo" |
| 31 SHA1_PATH = '/sdcard/%s/%s.sha1' % (ANDROID_PACKAGE, APK_NAME) |
| 29 | 32 |
| 30 PID_FILE_PATH = "/tmp/skydemo.pids" | 33 PID_FILE_PATH = "/tmp/skydemo.pids" |
| 31 PID_FILE_KEYS = frozenset([ | 34 PID_FILE_KEYS = frozenset([ |
| 32 'remote_sky_server_port', | 35 'remote_sky_server_port', |
| 33 'sky_server_pid', | 36 'sky_server_pid', |
| 34 'sky_server_port', | 37 'sky_server_port', |
| 35 'sky_server_root', | 38 'sky_server_root', |
| 36 'build_dir', | 39 'build_dir', |
| 37 ]) | 40 ]) |
| 38 | 41 |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 | 189 |
| 187 packages_root = dev_packages_root(args.build_dir) | 190 packages_root = dev_packages_root(args.build_dir) |
| 188 sky_server = self._sky_server_for_args(args, packages_root) | 191 sky_server = self._sky_server_for_args(args, packages_root) |
| 189 pids['sky_server_pid'] = sky_server.start() | 192 pids['sky_server_pid'] = sky_server.start() |
| 190 pids['sky_server_port'] = sky_server.port | 193 pids['sky_server_port'] = sky_server.port |
| 191 pids['sky_server_root'] = sky_server.root | 194 pids['sky_server_root'] = sky_server.root |
| 192 | 195 |
| 193 pids['build_dir'] = os.path.abspath(args.build_dir) | 196 pids['build_dir'] = os.path.abspath(args.build_dir) |
| 194 | 197 |
| 195 if args.install: | 198 if args.install: |
| 196 # -r to replace an existing apk, -d to allow version downgrade. | 199 # We might need to install a new APK, so check SHA1 |
| 197 subprocess.check_call([ADB_PATH, 'install', '-r', '-d', apk_path]) | 200 source_sha1 = hashlib.sha1(open(apk_path, 'rb').read()).hexdigest() |
| 201 dest_sha1 = subprocess.check_output([ADB_PATH, 'shell', 'cat', SHA1_
PATH]) |
| 202 use_existing_apk = False |
| 203 if source_sha1 == dest_sha1: |
| 204 # Make sure that the APK didn't get uninstalled somehow |
| 205 use_existing_apk = subprocess.check_output([ |
| 206 ADB_PATH, 'shell', 'pm', 'list', 'packages', ANDROID_PACKAGE |
| 207 ]) |
| 198 else: | 208 else: |
| 209 # User is telling us not to bother installing an APK |
| 210 use_existing_apk = True |
| 211 |
| 212 if use_existing_apk: |
| 213 # APK is already on the device, we only need to stop it |
| 199 subprocess.check_call([ | 214 subprocess.check_call([ |
| 200 ADB_PATH, 'shell', 'am', 'force-stop', ANDROID_PACKAGE | 215 ADB_PATH, 'shell', 'am', 'force-stop', ANDROID_PACKAGE |
| 201 ]) | 216 ]) |
| 217 else: |
| 218 # Slow path, need to upload a new APK to the device |
| 219 # -r to replace an existing apk, -d to allow version downgrade. |
| 220 subprocess.check_call([ADB_PATH, 'install', '-r', '-d', apk_path]) |
| 221 # record the SHA1 of the APK we just pushed |
| 222 with tempfile.NamedTemporaryFile() as fp: |
| 223 fp.write(source_sha1) |
| 224 fp.seek(0) |
| 225 subprocess.check_call([ADB_PATH, 'push', fp.name, SHA1_PATH]) |
| 202 | 226 |
| 203 # Set up port forwarding for observatory | 227 # Set up port forwarding for observatory |
| 204 port_string = 'tcp:%s' % OBSERVATORY_PORT | 228 port_string = 'tcp:%s' % OBSERVATORY_PORT |
| 205 subprocess.check_call([ | 229 subprocess.check_call([ |
| 206 ADB_PATH, 'forward', port_string, port_string | 230 ADB_PATH, 'forward', port_string, port_string |
| 207 ]) | 231 ]) |
| 208 | 232 |
| 209 port_string = 'tcp:%s' % sky_server.port | 233 port_string = 'tcp:%s' % sky_server.port |
| 210 subprocess.check_call([ | 234 subprocess.check_call([ |
| 211 ADB_PATH, 'reverse', port_string, port_string | 235 ADB_PATH, 'reverse', port_string, port_string |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 args = parser.parse_args() | 383 args = parser.parse_args() |
| 360 pids = Pids.read_from(PID_FILE_PATH, PID_FILE_KEYS) | 384 pids = Pids.read_from(PID_FILE_PATH, PID_FILE_KEYS) |
| 361 exit_code = args.func(args, pids) | 385 exit_code = args.func(args, pids) |
| 362 # We could do this with an at-exit handler instead? | 386 # We could do this with an at-exit handler instead? |
| 363 pids.write_to(PID_FILE_PATH) | 387 pids.write_to(PID_FILE_PATH) |
| 364 sys.exit(exit_code) | 388 sys.exit(exit_code) |
| 365 | 389 |
| 366 | 390 |
| 367 if __name__ == '__main__': | 391 if __name__ == '__main__': |
| 368 SkyShellRunner().main() | 392 SkyShellRunner().main() |
| OLD | NEW |