OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2012 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 """Utility script to install APKs from the command line quickly.""" |
| 8 |
7 import multiprocessing | 9 import multiprocessing |
8 import optparse | 10 import optparse |
9 import os | 11 import os |
10 import sys | 12 import sys |
11 | 13 |
12 from pylib import android_commands | 14 from pylib import android_commands |
13 from pylib import constants | 15 from pylib import constants |
14 from pylib.utils import apk_helper | 16 from pylib.utils import apk_helper |
15 from pylib.utils import test_options_parser | 17 from pylib.utils import test_options_parser |
16 | 18 |
17 | 19 |
| 20 def AddInstallAPKOption(option_parser): |
| 21 """Adds apk option used to install the APK to the OptionParser.""" |
| 22 test_options_parser.AddBuildTypeOption(option_parser) |
| 23 option_parser.add_option('--apk', |
| 24 help=('The name of the apk containing the ' |
| 25 ' application (with the .apk extension).')) |
| 26 option_parser.add_option('--apk_package', |
| 27 help=('The package name used by the apk containing ' |
| 28 'the application.')) |
| 29 option_parser.add_option('--keep_data', |
| 30 action='store_true', |
| 31 default=False, |
| 32 help=('Keep the package data when installing ' |
| 33 'the application.')) |
| 34 |
| 35 |
| 36 def ValidateInstallAPKOption(option_parser, options): |
| 37 """Validates the apk option and potentially qualifies the path.""" |
| 38 if not options.apk: |
| 39 option_parser.error('--apk is mandatory.') |
| 40 if not os.path.exists(options.apk): |
| 41 options.apk = os.path.join(constants.DIR_SOURCE_ROOT, |
| 42 'out', options.build_type, |
| 43 'apks', options.apk) |
| 44 |
| 45 |
18 def _InstallApk(args): | 46 def _InstallApk(args): |
19 apk_path, apk_package, keep_data, device = args | 47 apk_path, apk_package, keep_data, device = args |
20 result = android_commands.AndroidCommands(device=device).ManagedInstall( | 48 result = android_commands.AndroidCommands(device=device).ManagedInstall( |
21 apk_path, keep_data, apk_package) | 49 apk_path, keep_data, apk_package) |
22 print '----- Installed on %s -----' % device | 50 print '----- Installed on %s -----' % device |
23 print result | 51 print result |
24 | 52 |
25 | 53 |
26 def main(argv): | 54 def main(argv): |
27 parser = optparse.OptionParser() | 55 parser = optparse.OptionParser() |
28 test_options_parser.AddInstallAPKOption(parser) | 56 AddInstallAPKOption(parser) |
29 options, args = parser.parse_args(argv) | 57 options, args = parser.parse_args(argv) |
30 test_options_parser.ValidateInstallAPKOption(parser, options) | 58 ValidateInstallAPKOption(parser, options) |
31 if len(args) > 1: | 59 if len(args) > 1: |
32 raise Exception('Error: Unknown argument:', args[1:]) | 60 raise Exception('Error: Unknown argument:', args[1:]) |
33 | 61 |
34 devices = android_commands.GetAttachedDevices() | 62 devices = android_commands.GetAttachedDevices() |
35 if not devices: | 63 if not devices: |
36 raise Exception('Error: no connected devices') | 64 raise Exception('Error: no connected devices') |
37 | 65 |
38 if not options.apk_package: | 66 if not options.apk_package: |
39 options.apk_package = apk_helper.GetPackageName(options.apk) | 67 options.apk_package = apk_helper.GetPackageName(options.apk) |
40 | 68 |
41 pool = multiprocessing.Pool(len(devices)) | 69 pool = multiprocessing.Pool(len(devices)) |
42 # Send a tuple (apk_path, apk_package, device) per device. | 70 # Send a tuple (apk_path, apk_package, device) per device. |
43 pool.map(_InstallApk, zip([options.apk] * len(devices), | 71 pool.map(_InstallApk, zip([options.apk] * len(devices), |
44 [options.apk_package] * len(devices), | 72 [options.apk_package] * len(devices), |
45 [options.keep_data] * len(devices), | 73 [options.keep_data] * len(devices), |
46 devices)) | 74 devices)) |
47 | 75 |
48 | 76 |
49 if __name__ == '__main__': | 77 if __name__ == '__main__': |
50 sys.exit(main(sys.argv)) | 78 sys.exit(main(sys.argv)) |
OLD | NEW |