Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(569)

Side by Side Diff: build/android/gyp/apk_install.py

Issue 1141403002: Add split-select logic to apk_install.py & fix crash when --split-apk-path is used (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@uses-sdk-21
Patch Set: Address review nits Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 sys 14 import sys
15 15
16 from util import build_device 16 from util import build_device
17 from util import build_utils 17 from util import build_utils
18 from util import md5_check 18 from util import md5_check
19 19
20 BUILD_ANDROID_DIR = os.path.join(os.path.dirname(__file__), '..') 20 BUILD_ANDROID_DIR = os.path.join(os.path.dirname(__file__), '..')
21 sys.path.append(BUILD_ANDROID_DIR) 21 sys.path.append(BUILD_ANDROID_DIR)
22 22
23 from pylib import constants 23 from pylib import constants
24 from pylib.utils import apk_helper 24 from pylib.utils import apk_helper
25 25
26 def RetrieveDeviceConfig(device):
27 """Probes the given device for its split-select config.
28
29 For example: en-rUS-xhdpi:armeabi-v7a
30 Run "split-select --help" for more info about the format.
31 """
32 # The locale properties here are new in sdk level 21.
33 SHELL_CONFIG_COMMAND = ('echo $(getprop persist.sys.language)-'
jbudorick 2015/05/20 01:43:05 This would seemingly be much more readable as just
jbudorick 2015/05/20 01:44:05 I should note that this is mildly slower than what
agrieve 2015/05/20 14:33:58 Yeah, this was my original reasoning, but seems fa
agrieve 2015/05/20 14:33:58 Done.
34 'r$(getprop persist.sys.country)-'
35 '$(D=$(getprop ro.sf.lcd_density);'
36 ' D=${D/120/ldpi};'
37 ' D=${D/160/mdpi};'
38 ' D=${D/240/hdpi};'
39 ' D=${D/320/xhdpi};'
40 ' D=${D/480/xxhdpi};'
41 ' D=${D/[0-9]*/tvdpi/};'
42 ' echo $D):'
43 '$(getprop ro.product.cpu.abi)')
44 return device.RunShellCommand(SHELL_CONFIG_COMMAND)[0]
45
46
26 def GetNewMetadata(device, apk_package): 47 def GetNewMetadata(device, apk_package):
27 """Gets the metadata on the device for the apk_package apk.""" 48 """Gets the metadata on the device for the apk_package apk."""
28 output = device.RunShellCommand('ls -l /data/app/') 49 output = device.RunShellCommand('ls -l /data/app/')
29 # Matches lines like: 50 # Matches lines like:
30 # -rw-r--r-- system system 7376582 2013-04-19 16:34 \ 51 # -rw-r--r-- system system 7376582 2013-04-19 16:34 \
31 # org.chromium.chrome.shell.apk 52 # org.chromium.chrome.shell.apk
32 # -rw-r--r-- system system 7376582 2013-04-19 16:34 \ 53 # -rw-r--r-- system system 7376582 2013-04-19 16:34 \
33 # org.chromium.chrome.shell-1.apk 54 # org.chromium.chrome.shell-1.apk
34 apk_matcher = lambda s: re.match('.*%s(-[0-9]*)?(.apk)?$' % apk_package, s) 55 apk_matcher = lambda s: re.match('.*%s(-[0-9]*)?(.apk)?$' % apk_package, s)
35 matches = filter(apk_matcher, output) 56 matches = filter(apk_matcher, output)
36 return matches[0] if matches else None 57 return matches[0] if matches else None
37 58
38 def HasInstallMetadataChanged(device, apk_package, metadata_path): 59 def HasInstallMetadataChanged(device, apk_package, metadata_path):
39 """Checks if the metadata on the device for apk_package has changed.""" 60 """Checks if the metadata on the device for apk_package has changed."""
40 if not os.path.exists(metadata_path): 61 if not os.path.exists(metadata_path):
41 return True 62 return True
42 63
43 with open(metadata_path, 'r') as expected_file: 64 with open(metadata_path, 'r') as expected_file:
44 return expected_file.read() != device.GetInstallMetadata(apk_package) 65 return expected_file.read() != device.GetInstallMetadata(apk_package)
cjhopman 2015/05/20 01:37:28 Is the install metadata still valid in a split wor
agrieve 2015/05/20 14:33:58 The install metadata is an "ls -l" of the apk dire
45 66
46 67
47 def RecordInstallMetadata(device, apk_package, metadata_path): 68 def RecordInstallMetadata(device, apk_package, metadata_path):
48 """Records the metadata from the device for apk_package.""" 69 """Records the metadata from the device for apk_package."""
49 metadata = GetNewMetadata(device, apk_package) 70 metadata = GetNewMetadata(device, apk_package)
50 if not metadata: 71 if not metadata:
51 raise Exception('APK install failed unexpectedly.') 72 raise Exception('APK install failed unexpectedly.')
52 73
53 with open(metadata_path, 'w') as outfile: 74 with open(metadata_path, 'w') as outfile:
54 outfile.write(metadata) 75 outfile.write(metadata)
55 76
56 77
57 def main(): 78 def main():
58 parser = optparse.OptionParser() 79 parser = optparse.OptionParser()
59 parser.add_option('--apk-path', 80 parser.add_option('--apk-path',
60 help='Path to .apk to install.') 81 help='Path to .apk to install.')
61 parser.add_option('--split-apk-path', 82 parser.add_option('--split-apk-path',
62 help='Path to .apk splits (can specify multiple times, causes ' 83 help='Path to .apk splits (can specify multiple times, causes '
63 '--install-multiple to be used.', 84 '--install-multiple to be used.',
64 action='append') 85 action='append')
86 parser.add_option('--android-sdk-tools',
87 help='Path to the Android SDK build tools folder. ' +
88 'Required when using --split-apk-path.')
65 parser.add_option('--install-record', 89 parser.add_option('--install-record',
66 help='Path to install record (touched only when APK is installed).') 90 help='Path to install record (touched only when APK is installed).')
67 parser.add_option('--build-device-configuration', 91 parser.add_option('--build-device-configuration',
68 help='Path to build device configuration.') 92 help='Path to build device configuration.')
69 parser.add_option('--stamp', 93 parser.add_option('--stamp',
70 help='Path to touch on success.') 94 help='Path to touch on success.')
71 parser.add_option('--configuration-name', 95 parser.add_option('--configuration-name',
72 help='The build CONFIGURATION_NAME') 96 help='The build CONFIGURATION_NAME')
73 options, _ = parser.parse_args() 97 options, _ = parser.parse_args()
74 98
75 device = build_device.GetBuildDeviceFromPath( 99 device = build_device.GetBuildDeviceFromPath(
76 options.build_device_configuration) 100 options.build_device_configuration)
77 if not device: 101 if not device:
78 return 102 return
79 103
80 constants.SetBuildType(options.configuration_name) 104 constants.SetBuildType(options.configuration_name)
81 105
82 serial_number = device.GetSerialNumber() 106 serial_number = device.GetSerialNumber()
83 apk_package = apk_helper.GetPackageName(options.apk_path) 107 apk_package = apk_helper.GetPackageName(options.apk_path)
84 108
85 metadata_path = '%s.%s.device.time.stamp' % (options.apk_path, serial_number) 109 metadata_path = '%s.%s.device.time.stamp' % (options.apk_path, serial_number)
86 110
87 # If the APK on the device does not match the one that was last installed by 111 # If the APK on the device does not match the one that was last installed by
88 # the build, then the APK has to be installed (regardless of the md5 record). 112 # the build, then the APK has to be installed (regardless of the md5 record).
89 force_install = HasInstallMetadataChanged(device, apk_package, metadata_path) 113 force_install = HasInstallMetadataChanged(device, apk_package, metadata_path)
90 114
115 def SelectSplits(target_config, base_apk, split_apks, android_sdk_tools):
116 cmd = [os.path.join(android_sdk_tools, 'split-select'),
117 '--target', target_config,
118 '--base', base_apk,
119 ]
120 for split in split_apks:
121 cmd.extend(('--split', split))
122
123 # split-select outputs one path per line and a blank line at the end.
124 output = build_utils.CheckOutput(cmd)
125 return [x for x in output.split('\n') if x]
126
91 def Install(): 127 def Install():
92 # TODO: Filter splits using split-select. 128 if options.split_apk_path:
93 active_splits = options.split_apk_path 129 requiredSdkVersion = constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP
94 if active_splits: 130 actualSdkVersion = device.device.build_version_sdk
95 device.adb.InstallMultiple( 131 if actualSdkVersion < requiredSdkVersion:
96 [options.apk_path] + active_splits, 132 raise Exception(('--split-apk-path requires sdk version %s. Device has '
97 reinstall=True) 133 'version %s') % (requiredSdkVersion, actualSdkVersion))
134 device_config = RetrieveDeviceConfig(device)
135 active_splits = SelectSplits(
136 device_config,
137 options.apk_path,
138 options.split_apk_path,
139 options.android_sdk_tools)
140
141 all_apks = [options.apk_path] + active_splits
142 apk_names = ', '.join((os.path.basename(path) for path in all_apks))
143 print 'Installing apks:', apk_names
cjhopman 2015/05/20 01:37:28 maybe drop the print
agrieve 2015/05/20 14:33:58 Done.
144 device.device.adb.InstallMultiple(all_apks, reinstall=True)
98 else: 145 else:
99 device.Install(options.apk_path, reinstall=True) 146 device.Install(options.apk_path, reinstall=True)
100 147
101 RecordInstallMetadata(device, apk_package, metadata_path) 148 RecordInstallMetadata(device, apk_package, metadata_path)
102 build_utils.Touch(options.install_record) 149 build_utils.Touch(options.install_record)
103 150
104 151
105 record_path = '%s.%s.md5.stamp' % (options.apk_path, serial_number) 152 record_path = '%s.%s.md5.stamp' % (options.apk_path, serial_number)
106 md5_check.CallAndRecordIfStale( 153 md5_check.CallAndRecordIfStale(
107 Install, 154 Install,
108 record_path=record_path, 155 record_path=record_path,
109 input_paths=[options.apk_path], 156 input_paths=[options.apk_path],
110 force=force_install) 157 force=force_install)
111 158
112 if options.stamp: 159 if options.stamp:
113 build_utils.Touch(options.stamp) 160 build_utils.Touch(options.stamp)
114 161
115 162
116 if __name__ == '__main__': 163 if __name__ == '__main__':
117 sys.exit(main()) 164 sys.exit(main())
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698