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.""" | 7 """Utility script to install APKs from the command line quickly.""" |
8 | 8 |
9 import optparse | 9 import argparse |
| 10 import logging |
10 import os | 11 import os |
11 import sys | 12 import sys |
12 | 13 |
13 from pylib import constants | 14 from pylib import constants |
| 15 from pylib.device import device_blacklist |
14 from pylib.device import device_errors | 16 from pylib.device import device_errors |
15 from pylib.device import device_utils | 17 from pylib.device import device_utils |
| 18 from pylib.utils import run_tests_helper |
16 | 19 |
17 | 20 |
18 def AddInstallAPKOption(option_parser): | 21 def main(): |
19 """Adds apk option used to install the APK to the OptionParser.""" | 22 parser = argparse.ArgumentParser() |
20 option_parser.add_option('--apk', | 23 |
21 help=('DEPRECATED The name of the apk containing the' | 24 apk_group = parser.add_mutually_exclusive_group(required=True) |
22 ' application (with the .apk extension).')) | 25 apk_group.add_argument('--apk', dest='apk_name', |
23 option_parser.add_option('--apk_package', | 26 help='DEPRECATED The name of the apk containing the' |
24 help=('DEPRECATED The package name used by the apk ' | 27 ' application (with the .apk extension).') |
25 'containing the application.')) | 28 apk_group.add_argument('apk_path', nargs='?', |
26 option_parser.add_option('--keep_data', | 29 help='The path to the APK to install.') |
27 action='store_true', | 30 |
28 default=False, | 31 # TODO(jbudorick): Remove once no clients pass --apk_package |
29 help=('Keep the package data when installing ' | 32 parser.add_argument('--apk_package', help='DEPRECATED unused') |
30 'the application.')) | 33 parser.add_argument('--keep_data', |
31 option_parser.add_option('--debug', action='store_const', const='Debug', | 34 action='store_true', |
32 dest='build_type', | 35 default=False, |
33 default=os.environ.get('BUILDTYPE', 'Debug'), | 36 help='Keep the package data when installing ' |
34 help='If set, run test suites under out/Debug. ' | 37 'the application.') |
| 38 parser.add_argument('--debug', action='store_const', const='Debug', |
| 39 dest='build_type', |
| 40 default=os.environ.get('BUILDTYPE', 'Debug'), |
| 41 help='If set, run test suites under out/Debug. ' |
35 'Default is env var BUILDTYPE or Debug') | 42 'Default is env var BUILDTYPE or Debug') |
36 option_parser.add_option('--release', action='store_const', const='Release', | 43 parser.add_argument('--release', action='store_const', const='Release', |
37 dest='build_type', | 44 dest='build_type', |
38 help='If set, run test suites under out/Release. ' | 45 help='If set, run test suites under out/Release. ' |
39 'Default is env var BUILDTYPE or Debug.') | 46 'Default is env var BUILDTYPE or Debug.') |
40 option_parser.add_option('-d', '--device', dest='device', | 47 parser.add_argument('-d', '--device', dest='device', |
41 help='Target device for apk to install on.') | 48 help='Target device for apk to install on.') |
| 49 parser.add_argument('-v', '--verbose', action='count', |
| 50 help='Enable verbose logging.') |
42 | 51 |
| 52 args = parser.parse_args() |
43 | 53 |
44 def ValidateInstallAPKOption(option_parser, options, args): | 54 run_tests_helper.SetLogLevel(args.verbose) |
45 """Validates the apk option and potentially qualifies the path.""" | 55 constants.SetBuildType(args.build_type) |
46 if not options.apk: | |
47 if len(args) > 1: | |
48 options.apk = args[1] | |
49 else: | |
50 option_parser.error('apk target not specified.') | |
51 | 56 |
52 if not options.apk.endswith('.apk'): | 57 if args.apk_path: |
53 options.apk += '.apk' | 58 apk = args.apk_path |
54 | 59 else: |
55 if not os.path.exists(options.apk): | 60 apk = args.apk_name |
56 options.apk = os.path.join(constants.GetOutDirectory(), 'apks', | 61 if not apk.endswith('.apk'): |
57 options.apk) | 62 apk += '.apk' |
58 | 63 if not os.path.exists(apk): |
59 | 64 apk = os.path.join(constants.GetOutDirectory(), 'apks', apk) |
60 def main(argv): | |
61 parser = optparse.OptionParser() | |
62 parser.set_usage("usage: %prog [options] target") | |
63 AddInstallAPKOption(parser) | |
64 options, args = parser.parse_args(argv) | |
65 | |
66 if len(args) > 1 and options.apk: | |
67 parser.error("Appending the apk as argument can't be used with --apk.") | |
68 elif len(args) > 2: | |
69 parser.error("Too many arguments.") | |
70 | |
71 constants.SetBuildType(options.build_type) | |
72 ValidateInstallAPKOption(parser, options, args) | |
73 | 65 |
74 devices = device_utils.DeviceUtils.HealthyDevices() | 66 devices = device_utils.DeviceUtils.HealthyDevices() |
75 | 67 |
76 if options.device: | 68 if args.device: |
77 devices = [d for d in devices if d == options.device] | 69 devices = [d for d in devices if d == args.device] |
78 if not devices: | 70 if not devices: |
79 raise device_errors.DeviceUnreachableError(options.device) | 71 raise device_errors.DeviceUnreachableError(args.device) |
80 elif not devices: | 72 elif not devices: |
81 raise device_errors.NoDevicesError() | 73 raise device_errors.NoDevicesError() |
82 | 74 |
83 device_utils.DeviceUtils.parallel(devices).Install( | 75 def blacklisting_install(device): |
84 options.apk, reinstall=options.keep_data) | 76 try: |
| 77 device.Install(apk, reinstall=args.keep_data) |
| 78 except device_errors.CommandFailedError: |
| 79 logging.exception('Failed to install %s', args.apk) |
| 80 device_blacklist.ExtendBlacklist([str(device)]) |
| 81 logging.warning('Blacklisting %s', str(device)) |
| 82 except device_errors.CommandTimeoutError: |
| 83 logging.exception('Timed out while installing %s', args.apk) |
| 84 device_blacklist.ExtendBlacklist([str(device)]) |
| 85 logging.warning('Blacklisting %s', str(device)) |
| 86 |
| 87 device_utils.DeviceUtils.parallel(devices).pMap(blacklisting_install) |
85 | 88 |
86 | 89 |
87 if __name__ == '__main__': | 90 if __name__ == '__main__': |
88 sys.exit(main(sys.argv)) | 91 sys.exit(main()) |
89 | 92 |
OLD | NEW |