Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright 2013 The Chromium Authors. All rights reserved. | 3 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """Installs an APK. | 7 """Installs an APK. |
| 8 | 8 |
| 9 """ | 9 """ |
| 10 | 10 |
| 11 import optparse | 11 import optparse |
| 12 import os | 12 import os |
| 13 import re | |
| 13 import subprocess | 14 import subprocess |
| 14 import sys | 15 import sys |
| 15 | 16 |
| 16 from util import build_utils | 17 from util import build_utils |
| 17 from util import md5_check | 18 from util import md5_check |
| 18 | 19 |
| 19 BUILD_ANDROID_DIR = os.path.join(os.path.dirname(__file__), '..') | 20 BUILD_ANDROID_DIR = os.path.join(os.path.dirname(__file__), '..') |
| 20 sys.path.append(BUILD_ANDROID_DIR) | 21 sys.path.append(BUILD_ANDROID_DIR) |
| 21 | 22 |
| 22 from pylib import android_commands | 23 from pylib import android_commands |
| 24 from pylib.utils import apk_helper | |
|
Yaron
2013/04/22 22:02:40
This isn't in the gyp deps but I assume you ignore
cjhopman
2013/04/24 00:16:27
Yeah, it's just infeasible to specify all the impo
| |
| 25 | |
| 26 | |
| 27 def GetMetadata(apk_package): | |
| 28 """Gets the metadata on the device for the apk_package apk.""" | |
| 29 adb = android_commands.AndroidCommands() | |
| 30 output = adb.RunShellCommand('ls -l /data/app/') | |
| 31 # Matches lines like: | |
| 32 # -rw-r--r-- system system 7376582 2013-04-19 16:34 org.chromium.chrome.t estshell-1.apk | |
|
Yaron
2013/04/22 22:02:40
I see a bunch of -1 and -2 apks on my local device
cjhopman
2013/04/24 00:16:27
My understanding is that there can only be one. On
| |
| 33 apk_matcher = lambda s: re.match('.*%s(-[0-9]*)?.apk.*' % apk_package, s) | |
|
Yaron
2013/04/22 22:02:40
Maybe end with "$"? You don't want to pick up misc
cjhopman
2013/04/24 00:16:27
Done.
| |
| 34 matches = filter(apk_matcher, output) | |
| 35 return matches[0] if matches else None | |
| 36 | |
| 37 | |
| 38 def HasInstallMetadataChanged(apk_package, metadata_path): | |
| 39 """Checks if the metadata on the device for apk_package has changed.""" | |
| 40 if not os.path.exists(metadata_path): | |
| 41 return True | |
| 42 | |
| 43 with open(metadata_path, 'r') as expected_file: | |
| 44 return expected_file.read() != GetMetadata(apk_package) | |
| 45 | |
| 46 | |
| 47 def RecordInstallMetadata(apk_package, metadata_path): | |
| 48 """Records the metadata from the device for apk_package.""" | |
| 49 metadata = GetMetadata(apk_package) | |
| 50 if not metadata: | |
| 51 raise 'APK install failed unexpectedly.' | |
| 52 | |
| 53 with open(metadata_path, 'w') as outfile: | |
| 54 outfile.write(metadata) | |
| 55 | |
| 23 | 56 |
| 24 def main(argv): | 57 def main(argv): |
| 25 parser = optparse.OptionParser() | 58 parser = optparse.OptionParser() |
| 26 parser.add_option('--android-sdk-tools', | 59 parser.add_option('--android-sdk-tools', |
| 27 help='Path to Android SDK tools.') | 60 help='Path to Android SDK tools.') |
| 28 parser.add_option('--apk-path', | 61 parser.add_option('--apk-path', |
| 29 help='Path to .apk to install.') | 62 help='Path to .apk to install.') |
| 30 parser.add_option('--install-record', | 63 parser.add_option('--install-record', |
| 31 help='Path to install record (touched only when APK is installed).') | 64 help='Path to install record (touched only when APK is installed).') |
| 32 parser.add_option('--stamp', | 65 parser.add_option('--stamp', |
| 33 help='Path to touch on success.') | 66 help='Path to touch on success.') |
| 34 options, _ = parser.parse_args() | 67 options, _ = parser.parse_args() |
| 35 | 68 |
| 36 # TODO(cjhopman): Should this install to all devices/be configurable? | 69 # TODO(cjhopman): Should this install to all devices/be configurable? |
| 37 install_cmd = [ | 70 install_cmd = [ |
| 38 os.path.join(options.android_sdk_tools, 'adb'), | 71 os.path.join(options.android_sdk_tools, 'adb'), |
| 39 'install', '-r', | 72 'install', '-r', |
| 40 options.apk_path] | 73 options.apk_path] |
| 41 | 74 |
| 75 serial_number = android_commands.AndroidCommands().Adb().GetSerialNumber() | |
| 76 apk_package = apk_helper.GetPackageName(options.apk_path) | |
| 77 | |
| 78 metadata_path = '%s.%s.device.time.stamp' % (options.apk_path, serial_number) | |
| 79 | |
| 80 # If the APK on the device does not match the one that was last installed by | |
| 81 # the build, then the APK has to be installed (regardless of the md5 record). | |
| 82 force_install = HasInstallMetadataChanged(apk_package, metadata_path) | |
| 83 | |
| 42 def Install(): | 84 def Install(): |
| 43 build_utils.CheckCallDie(install_cmd) | 85 build_utils.CheckCallDie(install_cmd) |
| 44 build_utils.Touch(options.install_record) | 86 RecordInstallMetadata(apk_package, metadata_path) |
| 87 build_utils.Touch(options.install_record) | |
| 45 | 88 |
| 46 serial_number = android_commands.AndroidCommands().Adb().GetSerialNumber() | 89 |
| 47 record_path = '%s.%s.md5.stamp' % (options.apk_path, serial_number) | 90 record_path = '%s.%s.md5.stamp' % (options.apk_path, serial_number) |
| 48 md5_check.CallAndRecordIfStale( | 91 md5_check.CallAndRecordIfStale( |
| 49 Install, | 92 Install, |
| 50 record_path=record_path, | 93 record_path=record_path, |
| 51 input_paths=[options.apk_path], | 94 input_paths=[options.apk_path], |
| 52 input_strings=install_cmd) | 95 input_strings=install_cmd, |
| 96 force=force_install) | |
| 53 | 97 |
| 54 if options.stamp: | 98 if options.stamp: |
| 55 build_utils.Touch(options.stamp) | 99 build_utils.Touch(options.stamp) |
| 56 | 100 |
| 57 | 101 |
| 58 if __name__ == '__main__': | 102 if __name__ == '__main__': |
| 59 sys.exit(main(sys.argv)) | 103 sys.exit(main(sys.argv)) |
| OLD | NEW |