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 |
| 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.apk |
| 33 # -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) |
| 35 matches = filter(apk_matcher, output) |
| 36 return matches[0] if matches else None |
| 37 |
| 38 |
| 39 def HasInstallMetadataChanged(apk_package, metadata_path): |
| 40 """Checks if the metadata on the device for apk_package has changed.""" |
| 41 if not os.path.exists(metadata_path): |
| 42 return True |
| 43 |
| 44 with open(metadata_path, 'r') as expected_file: |
| 45 return expected_file.read() != GetMetadata(apk_package) |
| 46 |
| 47 |
| 48 def RecordInstallMetadata(apk_package, metadata_path): |
| 49 """Records the metadata from the device for apk_package.""" |
| 50 metadata = GetMetadata(apk_package) |
| 51 if not metadata: |
| 52 raise 'APK install failed unexpectedly.' |
| 53 |
| 54 with open(metadata_path, 'w') as outfile: |
| 55 outfile.write(metadata) |
| 56 |
23 | 57 |
24 def main(argv): | 58 def main(argv): |
25 parser = optparse.OptionParser() | 59 parser = optparse.OptionParser() |
26 parser.add_option('--android-sdk-tools', | 60 parser.add_option('--android-sdk-tools', |
27 help='Path to Android SDK tools.') | 61 help='Path to Android SDK tools.') |
28 parser.add_option('--apk-path', | 62 parser.add_option('--apk-path', |
29 help='Path to .apk to install.') | 63 help='Path to .apk to install.') |
30 parser.add_option('--install-record', | 64 parser.add_option('--install-record', |
31 help='Path to install record (touched only when APK is installed).') | 65 help='Path to install record (touched only when APK is installed).') |
32 parser.add_option('--stamp', | 66 parser.add_option('--stamp', |
33 help='Path to touch on success.') | 67 help='Path to touch on success.') |
34 options, _ = parser.parse_args() | 68 options, _ = parser.parse_args() |
35 | 69 |
36 # TODO(cjhopman): Should this install to all devices/be configurable? | 70 # TODO(cjhopman): Should this install to all devices/be configurable? |
37 install_cmd = [ | 71 install_cmd = [ |
38 os.path.join(options.android_sdk_tools, 'adb'), | 72 os.path.join(options.android_sdk_tools, 'adb'), |
39 'install', '-r', | 73 'install', '-r', |
40 options.apk_path] | 74 options.apk_path] |
41 | 75 |
| 76 serial_number = android_commands.AndroidCommands().Adb().GetSerialNumber() |
| 77 apk_package = apk_helper.GetPackageName(options.apk_path) |
| 78 |
| 79 metadata_path = '%s.%s.device.time.stamp' % (options.apk_path, serial_number) |
| 80 |
| 81 # If the APK on the device does not match the one that was last installed by |
| 82 # the build, then the APK has to be installed (regardless of the md5 record). |
| 83 force_install = HasInstallMetadataChanged(apk_package, metadata_path) |
| 84 |
42 def Install(): | 85 def Install(): |
43 build_utils.CheckCallDie(install_cmd) | 86 build_utils.CheckCallDie(install_cmd) |
44 build_utils.Touch(options.install_record) | 87 RecordInstallMetadata(apk_package, metadata_path) |
| 88 build_utils.Touch(options.install_record) |
45 | 89 |
46 serial_number = android_commands.AndroidCommands().Adb().GetSerialNumber() | 90 |
47 record_path = '%s.%s.md5.stamp' % (options.apk_path, serial_number) | 91 record_path = '%s.%s.md5.stamp' % (options.apk_path, serial_number) |
48 md5_check.CallAndRecordIfStale( | 92 md5_check.CallAndRecordIfStale( |
49 Install, | 93 Install, |
50 record_path=record_path, | 94 record_path=record_path, |
51 input_paths=[options.apk_path], | 95 input_paths=[options.apk_path], |
52 input_strings=install_cmd) | 96 input_strings=install_cmd, |
| 97 force=force_install) |
53 | 98 |
54 if options.stamp: | 99 if options.stamp: |
55 build_utils.Touch(options.stamp) | 100 build_utils.Touch(options.stamp) |
56 | 101 |
57 | 102 |
58 if __name__ == '__main__': | 103 if __name__ == '__main__': |
59 sys.exit(main(sys.argv)) | 104 sys.exit(main(sys.argv)) |
OLD | NEW |