| 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(device, apk_package): |
| 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 = device.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(device, 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() != device.GetInstallMetadata(apk_package) |
| 46 | 43 |
| 47 | 44 |
| 48 def RecordInstallMetadata(apk_package, metadata_path): | 45 def RecordInstallMetadata(device, 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(device, apk_package) |
| 51 if not metadata: | 48 if not metadata: |
| 52 if not android_commands.AndroidCommands().IsRootEnabled(): | |
| 53 raise Exception('APK install failed unexpectedly -- root not enabled on ' | |
| 54 'the device (run adb root).') | |
| 55 raise Exception('APK install failed unexpectedly.') | 49 raise Exception('APK install failed unexpectedly.') |
| 56 | 50 |
| 57 with open(metadata_path, 'w') as outfile: | 51 with open(metadata_path, 'w') as outfile: |
| 58 outfile.write(metadata) | 52 outfile.write(metadata) |
| 59 | 53 |
| 60 | 54 |
| 61 def main(argv): | 55 def main(argv): |
| 62 if not build_utils.IsDeviceReady(): | |
| 63 build_utils.PrintBigWarning( | |
| 64 'Zero (or multiple) devices attached. Skipping APK install.') | |
| 65 return | |
| 66 | |
| 67 parser = optparse.OptionParser() | 56 parser = optparse.OptionParser() |
| 68 parser.add_option('--android-sdk-tools', | 57 parser.add_option('--android-sdk-tools', |
| 69 help='Path to Android SDK tools.') | 58 help='Path to Android SDK tools.') |
| 70 parser.add_option('--apk-path', | 59 parser.add_option('--apk-path', |
| 71 help='Path to .apk to install.') | 60 help='Path to .apk to install.') |
| 72 parser.add_option('--install-record', | 61 parser.add_option('--install-record', |
| 73 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.') |
| 74 parser.add_option('--stamp', | 65 parser.add_option('--stamp', |
| 75 help='Path to touch on success.') | 66 help='Path to touch on success.') |
| 76 options, _ = parser.parse_args() | 67 options, _ = parser.parse_args() |
| 77 | 68 |
| 78 # TODO(cjhopman): Should this install to all devices/be configurable? | 69 device = build_device.GetBuildDeviceFromPath( |
| 79 install_cmd = [ | 70 options.build_device_configuration) |
| 80 os.path.join(options.android_sdk_tools, 'adb'), | 71 if not device: |
| 81 'install', '-r', | 72 return |
| 82 options.apk_path] | |
| 83 | 73 |
| 84 serial_number = android_commands.AndroidCommands().Adb().GetSerialNumber() | 74 serial_number = device.GetSerialNumber() |
| 85 apk_package = apk_helper.GetPackageName(options.apk_path) | 75 apk_package = apk_helper.GetPackageName(options.apk_path) |
| 86 | 76 |
| 87 metadata_path = '%s.%s.device.time.stamp' % (options.apk_path, serial_number) | 77 metadata_path = '%s.%s.device.time.stamp' % (options.apk_path, serial_number) |
| 88 | 78 |
| 89 # If the APK on the device does not match the one that was last installed by | 79 # If the APK on the device does not match the one that was last installed by |
| 90 # the build, then the APK has to be installed (regardless of the md5 record). | 80 # the build, then the APK has to be installed (regardless of the md5 record). |
| 91 force_install = HasInstallMetadataChanged(apk_package, metadata_path) | 81 force_install = HasInstallMetadataChanged(device, apk_package, metadata_path) |
| 92 | 82 |
| 93 def Install(): | 83 def Install(): |
| 94 build_utils.CheckCallDie(install_cmd) | 84 device.Install(options.apk_path, reinstall=True) |
| 95 RecordInstallMetadata(apk_package, metadata_path) | 85 RecordInstallMetadata(device, apk_package, metadata_path) |
| 96 build_utils.Touch(options.install_record) | 86 build_utils.Touch(options.install_record) |
| 97 | 87 |
| 98 | 88 |
| 99 record_path = '%s.%s.md5.stamp' % (options.apk_path, serial_number) | 89 record_path = '%s.%s.md5.stamp' % (options.apk_path, serial_number) |
| 100 md5_check.CallAndRecordIfStale( | 90 md5_check.CallAndRecordIfStale( |
| 101 Install, | 91 Install, |
| 102 record_path=record_path, | 92 record_path=record_path, |
| 103 input_paths=[options.apk_path], | 93 input_paths=[options.apk_path], |
| 104 input_strings=install_cmd, | |
| 105 force=force_install) | 94 force=force_install) |
| 106 | 95 |
| 107 if options.stamp: | 96 if options.stamp: |
| 108 build_utils.Touch(options.stamp) | 97 build_utils.Touch(options.stamp) |
| 109 | 98 |
| 110 | 99 |
| 111 if __name__ == '__main__': | 100 if __name__ == '__main__': |
| 112 sys.exit(main(sys.argv)) | 101 sys.exit(main(sys.argv)) |
| OLD | NEW |