Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # |
| 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 |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Installs an APK. |
| 8 | |
| 9 | """ |
| 10 | |
| 11 | import optparse |
| 12 | import os |
| 13 | import re |
| 14 | import sys |
| 15 | |
| 16 | from util import build_device |
| 17 | from util import build_utils |
| 18 | from util import md5_check |
| 19 | |
| 20 | BUILD_ANDROID_DIR = os.path.abspath( |
| 21 | os.path.join(os.path.dirname(__file__), '..')) |
| 22 | sys.path.append(BUILD_ANDROID_DIR) |
| 23 | |
| 24 | import devil_chromium |
| 25 | from devil.android import apk_helper |
| 26 | from pylib import constants |
| 27 | |
| 28 | |
| 29 | def GetNewMetadata(device, apk_package): |
| 30 | """Gets the metadata on the device for the apk_package apk.""" |
| 31 | output = device.RunShellCommand('ls -l /data/app/') |
| 32 | # Matches lines like: |
| 33 | # -rw-r--r-- system system 7376582 2013-04-19 16:34 \ |
| 34 | # org.chromium.chrome.apk |
| 35 | # -rw-r--r-- system system 7376582 2013-04-19 16:34 \ |
| 36 | # org.chromium.chrome-1.apk |
| 37 | apk_matcher = lambda s: re.match('.*%s(-[0-9]*)?(.apk)?$' % apk_package, s) |
| 38 | matches = filter(apk_matcher, output) |
| 39 | return matches[0] if matches else None |
| 40 | |
| 41 | def HasInstallMetadataChanged(device, apk_package, metadata_path): |
| 42 | """Checks if the metadata on the device for apk_package has changed.""" |
| 43 | if not os.path.exists(metadata_path): |
| 44 | return True |
| 45 | |
| 46 | with open(metadata_path, 'r') as expected_file: |
| 47 | return expected_file.read() != device.GetInstallMetadata(apk_package) |
| 48 | |
| 49 | |
| 50 | def RecordInstallMetadata(device, apk_package, metadata_path): |
| 51 | """Records the metadata from the device for apk_package.""" |
| 52 | metadata = GetNewMetadata(device, apk_package) |
| 53 | if not metadata: |
| 54 | raise Exception('APK install failed unexpectedly.') |
| 55 | |
| 56 | with open(metadata_path, 'w') as outfile: |
| 57 | outfile.write(metadata) |
| 58 | |
| 59 | |
| 60 | def main(): |
| 61 | parser = optparse.OptionParser() |
| 62 | parser.add_option('--apk-path', |
| 63 | help='Path to .apk to install.') |
| 64 | parser.add_option('--split-apk-path', |
| 65 | help='Path to .apk splits (can specify multiple times, causes ' |
| 66 | '--install-multiple to be used.', |
| 67 | action='append') |
| 68 | parser.add_option('--android-sdk-tools', |
| 69 | help='Path to the Android SDK build tools folder. ' + |
| 70 | 'Required when using --split-apk-path.') |
| 71 | parser.add_option('--install-record', |
| 72 | help='Path to install record (touched only when APK is installed).') |
| 73 | parser.add_option('--build-device-configuration', |
| 74 | help='Path to build device configuration.') |
| 75 | parser.add_option('--stamp', |
| 76 | help='Path to touch on success.') |
| 77 | parser.add_option('--configuration-name', |
| 78 | help='The build CONFIGURATION_NAME') |
| 79 | parser.add_option('--output-directory', |
| 80 | help='The output directory.') |
| 81 | options, _ = parser.parse_args() |
| 82 | |
| 83 | constants.SetBuildType(options.configuration_name) |
| 84 | |
| 85 | devil_chromium.Initialize( |
| 86 | output_directory=os.path.abspath(options.output_directory)) |
| 87 | |
| 88 | device = build_device.GetBuildDeviceFromPath( |
| 89 | options.build_device_configuration) |
| 90 | if not device: |
| 91 | return |
| 92 | |
| 93 | serial_number = device.GetSerialNumber() |
| 94 | apk_package = apk_helper.GetPackageName(options.apk_path) |
| 95 | |
| 96 | metadata_path = '%s.%s.device.time.stamp' % (options.apk_path, serial_number) |
| 97 | |
| 98 | # If the APK on the device does not match the one that was last installed by |
| 99 | # the build, then the APK has to be installed (regardless of the md5 record). |
| 100 | force_install = HasInstallMetadataChanged(device, apk_package, metadata_path) |
| 101 | |
| 102 | |
| 103 | def Install(): |
| 104 | if options.split_apk_path: |
| 105 | device.InstallSplitApk(options.apk_path, options.split_apk_path) |
| 106 | else: |
| 107 | device.Install(options.apk_path, reinstall=True) |
| 108 | |
| 109 | RecordInstallMetadata(device, apk_package, metadata_path) |
| 110 | build_utils.Touch(options.install_record) |
| 111 | |
| 112 | |
| 113 | record_path = '%s.%s.md5.stamp' % (options.apk_path, serial_number) |
| 114 | md5_check.CallAndRecordIfStale( |
| 115 | Install, |
| 116 | record_path=record_path, |
| 117 | input_paths=[options.apk_path], |
| 118 | force=force_install) |
| 119 | |
| 120 | if options.stamp: |
| 121 | build_utils.Touch(options.stamp) |
| 122 | |
| 123 | |
| 124 | if __name__ == '__main__': |
| 125 | sys.exit(main()) |