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