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 re |
| 14 import subprocess | 14 import subprocess |
| 15 import sys | 15 import sys |
| 16 | 16 |
| 17 from util import build_device | |
| 17 from util import build_utils | 18 from util import build_utils |
| 18 from util import md5_check | 19 from util import md5_check |
| 19 | 20 |
| 20 BUILD_ANDROID_DIR = os.path.join(os.path.dirname(__file__), '..') | 21 BUILD_ANDROID_DIR = os.path.join(os.path.dirname(__file__), '..') |
| 21 sys.path.append(BUILD_ANDROID_DIR) | 22 sys.path.append(BUILD_ANDROID_DIR) |
| 22 | 23 |
| 23 from pylib import android_commands | |
| 24 from pylib.utils import apk_helper | 24 from pylib.utils import apk_helper |
| 25 | 25 |
| 26 | 26 def GetNewMetadata(bd, apk_package): |
|
shashi
2013/06/20 03:16:19
A more descriptive name than bd, maybe build_devic
cjhopman
2013/06/25 16:43:11
Done.
| |
| 27 def GetMetadata(apk_package): | |
| 28 """Gets the metadata on the device for the apk_package apk.""" | 27 """Gets the metadata on the device for the apk_package apk.""" |
| 29 adb = android_commands.AndroidCommands() | 28 output = bd.RunShellCommand('ls -l /data/app/') |
| 30 output = adb.RunShellCommand('ls -l /data/app/') | |
| 31 # Matches lines like: | 29 # Matches lines like: |
| 32 # -rw-r--r-- system system 7376582 2013-04-19 16:34 org.chromium.chrome.t estshell.apk | 30 # -rw-r--r-- system system 7376582 2013-04-19 16:34 org.chromium.chrome.t estshell.apk |
| 33 # -rw-r--r-- system system 7376582 2013-04-19 16:34 org.chromium.chrome.t estshell-1.apk | 31 # -rw-r--r-- system system 7376582 2013-04-19 16:34 org.chromium.chrome.t estshell-1.apk |
| 34 apk_matcher = lambda s: re.match('.*%s(-[0-9]*)?.apk$' % apk_package, s) | 32 apk_matcher = lambda s: re.match('.*%s(-[0-9]*)?.apk$' % apk_package, s) |
| 35 matches = filter(apk_matcher, output) | 33 matches = filter(apk_matcher, output) |
| 36 return matches[0] if matches else None | 34 return matches[0] if matches else None |
| 37 | 35 |
| 38 | 36 def HasInstallMetadataChanged(bd, apk_package, metadata_path): |
| 39 def HasInstallMetadataChanged(apk_package, metadata_path): | |
| 40 """Checks if the metadata on the device for apk_package has changed.""" | 37 """Checks if the metadata on the device for apk_package has changed.""" |
| 41 if not os.path.exists(metadata_path): | 38 if not os.path.exists(metadata_path): |
| 42 return True | 39 return True |
| 43 | 40 |
| 44 with open(metadata_path, 'r') as expected_file: | 41 with open(metadata_path, 'r') as expected_file: |
| 45 return expected_file.read() != GetMetadata(apk_package) | 42 return expected_file.read() != bd.GetInstallMetadata(apk_package) |
| 46 | 43 |
| 47 | 44 |
| 48 def RecordInstallMetadata(apk_package, metadata_path): | 45 def RecordInstallMetadata(bd, apk_package, metadata_path): |
| 49 """Records the metadata from the device for apk_package.""" | 46 """Records the metadata from the device for apk_package.""" |
| 50 metadata = GetMetadata(apk_package) | 47 metadata = GetNewMetadata(bd, apk_package) |
| 51 if not metadata: | 48 if not metadata: |
| 52 raise 'APK install failed unexpectedly.' | 49 raise 'APK install failed unexpectedly.' |
| 53 | 50 |
| 54 with open(metadata_path, 'w') as outfile: | 51 with open(metadata_path, 'w') as outfile: |
| 55 outfile.write(metadata) | 52 outfile.write(metadata) |
| 56 | 53 |
| 57 | 54 |
| 58 def main(argv): | 55 def main(argv): |
| 59 if not build_utils.IsDeviceReady(): | |
| 60 build_utils.PrintBigWarning( | |
| 61 'Zero (or multiple) devices attached. Skipping APK install.') | |
| 62 return | |
| 63 | |
| 64 parser = optparse.OptionParser() | 56 parser = optparse.OptionParser() |
| 65 parser.add_option('--android-sdk-tools', | 57 parser.add_option('--android-sdk-tools', |
| 66 help='Path to Android SDK tools.') | 58 help='Path to Android SDK tools.') |
| 67 parser.add_option('--apk-path', | 59 parser.add_option('--apk-path', |
| 68 help='Path to .apk to install.') | 60 help='Path to .apk to install.') |
| 69 parser.add_option('--install-record', | 61 parser.add_option('--install-record', |
| 70 help='Path to install record (touched only when APK is installed).') | 62 help='Path to install record (touched only when APK is installed).') |
| 63 parser.add_option('--build-device-configuration', | |
| 64 help='Path to build device configuration.') | |
| 71 parser.add_option('--stamp', | 65 parser.add_option('--stamp', |
| 72 help='Path to touch on success.') | 66 help='Path to touch on success.') |
| 73 options, _ = parser.parse_args() | 67 options, _ = parser.parse_args() |
| 74 | 68 |
| 75 # TODO(cjhopman): Should this install to all devices/be configurable? | 69 bd = build_device.GetBuildDeviceFromPath(options.build_device_configuration) |
| 76 install_cmd = [ | 70 if not bd: |
| 77 os.path.join(options.android_sdk_tools, 'adb'), | 71 return |
| 78 'install', '-r', | |
| 79 options.apk_path] | |
| 80 | 72 |
| 81 serial_number = android_commands.AndroidCommands().Adb().GetSerialNumber() | 73 serial_number = bd.GetSerialNumber() |
| 82 apk_package = apk_helper.GetPackageName(options.apk_path) | 74 apk_package = apk_helper.GetPackageName(options.apk_path) |
| 83 | 75 |
| 84 metadata_path = '%s.%s.device.time.stamp' % (options.apk_path, serial_number) | 76 metadata_path = '%s.%s.device.time.stamp' % (options.apk_path, serial_number) |
| 85 | 77 |
| 86 # If the APK on the device does not match the one that was last installed by | 78 # If the APK on the device does not match the one that was last installed by |
| 87 # the build, then the APK has to be installed (regardless of the md5 record). | 79 # the build, then the APK has to be installed (regardless of the md5 record). |
| 88 force_install = HasInstallMetadataChanged(apk_package, metadata_path) | 80 force_install = HasInstallMetadataChanged(bd, apk_package, metadata_path) |
| 89 | 81 |
| 90 def Install(): | 82 def Install(): |
| 91 build_utils.CheckCallDie(install_cmd) | 83 bd.Install(options.apk_path, reinstall=True) |
| 92 RecordInstallMetadata(apk_package, metadata_path) | 84 RecordInstallMetadata(bd, apk_package, metadata_path) |
| 93 build_utils.Touch(options.install_record) | 85 build_utils.Touch(options.install_record) |
| 94 | 86 |
| 95 | 87 |
| 96 record_path = '%s.%s.md5.stamp' % (options.apk_path, serial_number) | 88 record_path = '%s.%s.md5.stamp' % (options.apk_path, serial_number) |
| 97 md5_check.CallAndRecordIfStale( | 89 md5_check.CallAndRecordIfStale( |
| 98 Install, | 90 Install, |
| 99 record_path=record_path, | 91 record_path=record_path, |
| 100 input_paths=[options.apk_path], | 92 input_paths=[options.apk_path], |
| 101 input_strings=install_cmd, | |
| 102 force=force_install) | 93 force=force_install) |
| 103 | 94 |
| 104 if options.stamp: | 95 if options.stamp: |
| 105 build_utils.Touch(options.stamp) | 96 build_utils.Touch(options.stamp) |
| 106 | 97 |
| 107 | 98 |
| 108 if __name__ == '__main__': | 99 if __name__ == '__main__': |
| 109 sys.exit(main(sys.argv)) | 100 sys.exit(main(sys.argv)) |
| OLD | NEW |