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