2012-10-03 01:12:25 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
2013-07-09 13:03:26 +04:00
|
|
|
"""Utility script to install APKs from the command line quickly."""
|
|
|
|
|
2015-06-18 18:22:12 +03:00
|
|
|
import argparse
|
2015-07-07 22:33:23 +03:00
|
|
|
import glob
|
2015-06-18 18:22:12 +03:00
|
|
|
import logging
|
2012-10-03 01:12:25 +04:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2015-12-04 17:27:43 +03:00
|
|
|
import devil_chromium
|
2016-01-13 01:52:25 +03:00
|
|
|
from devil import devil_env
|
2015-09-03 21:00:57 +03:00
|
|
|
from devil.android import apk_helper
|
|
|
|
from devil.android import device_blacklist
|
|
|
|
from devil.android import device_errors
|
|
|
|
from devil.android import device_utils
|
|
|
|
from devil.utils import run_tests_helper
|
2012-10-03 01:12:25 +04:00
|
|
|
from pylib import constants
|
2015-06-18 18:22:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
|
|
|
apk_group = parser.add_mutually_exclusive_group(required=True)
|
|
|
|
apk_group.add_argument('--apk', dest='apk_name',
|
|
|
|
help='DEPRECATED The name of the apk containing the'
|
|
|
|
' application (with the .apk extension).')
|
|
|
|
apk_group.add_argument('apk_path', nargs='?',
|
|
|
|
help='The path to the APK to install.')
|
|
|
|
|
|
|
|
# TODO(jbudorick): Remove once no clients pass --apk_package
|
|
|
|
parser.add_argument('--apk_package', help='DEPRECATED unused')
|
2015-07-07 22:33:23 +03:00
|
|
|
parser.add_argument('--split',
|
|
|
|
action='append',
|
|
|
|
dest='splits',
|
|
|
|
help='A glob matching the apk splits. '
|
|
|
|
'Can be specified multiple times.')
|
2015-06-18 18:22:12 +03:00
|
|
|
parser.add_argument('--keep_data',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='Keep the package data when installing '
|
|
|
|
'the application.')
|
|
|
|
parser.add_argument('--debug', action='store_const', const='Debug',
|
|
|
|
dest='build_type',
|
|
|
|
default=os.environ.get('BUILDTYPE', 'Debug'),
|
|
|
|
help='If set, run test suites under out/Debug. '
|
2014-05-21 06:27:41 +04:00
|
|
|
'Default is env var BUILDTYPE or Debug')
|
2015-06-18 18:22:12 +03:00
|
|
|
parser.add_argument('--release', action='store_const', const='Release',
|
|
|
|
dest='build_type',
|
|
|
|
help='If set, run test suites under out/Release. '
|
2014-05-21 06:27:41 +04:00
|
|
|
'Default is env var BUILDTYPE or Debug.')
|
2015-12-08 03:46:23 +03:00
|
|
|
parser.add_argument('-d', '--device', dest='devices', action='append',
|
2016-04-18 23:02:48 +03:00
|
|
|
default=[],
|
2015-12-08 03:46:23 +03:00
|
|
|
help='Target device for apk to install on. Enter multiple'
|
|
|
|
' times for multiple devices.')
|
2016-01-13 01:52:25 +03:00
|
|
|
parser.add_argument('--adb-path',
|
|
|
|
help='Absolute path to the adb binary to use.')
|
2015-08-27 06:00:17 +03:00
|
|
|
parser.add_argument('--blacklist-file', help='Device blacklist JSON file.')
|
2015-06-18 18:22:12 +03:00
|
|
|
parser.add_argument('-v', '--verbose', action='count',
|
|
|
|
help='Enable verbose logging.')
|
2015-12-09 05:13:15 +03:00
|
|
|
parser.add_argument('--downgrade', action='store_true',
|
|
|
|
help='If set, allows downgrading of apk.')
|
2016-01-26 00:54:58 +03:00
|
|
|
parser.add_argument('--timeout', type=int,
|
|
|
|
default=device_utils.DeviceUtils.INSTALL_DEFAULT_TIMEOUT,
|
|
|
|
help='Seconds to wait for APK installation. '
|
|
|
|
'(default: %(default)s)')
|
2014-06-09 00:11:16 +04:00
|
|
|
|
2015-06-18 18:22:12 +03:00
|
|
|
args = parser.parse_args()
|
2014-06-09 00:11:16 +04:00
|
|
|
|
2015-06-18 18:22:12 +03:00
|
|
|
run_tests_helper.SetLogLevel(args.verbose)
|
|
|
|
constants.SetBuildType(args.build_type)
|
2013-07-09 13:03:26 +04:00
|
|
|
|
2016-01-13 01:52:25 +03:00
|
|
|
devil_custom_deps = None
|
|
|
|
if args.adb_path:
|
|
|
|
devil_custom_deps = {
|
|
|
|
'adb': {
|
|
|
|
devil_env.GetPlatform(): [args.adb_path],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
devil_chromium.Initialize(
|
|
|
|
output_directory=constants.GetOutDirectory(),
|
|
|
|
custom_deps=devil_custom_deps)
|
2015-12-04 17:27:43 +03:00
|
|
|
|
2015-06-18 18:22:12 +03:00
|
|
|
apk = args.apk_path or args.apk_name
|
|
|
|
if not apk.endswith('.apk'):
|
|
|
|
apk += '.apk'
|
|
|
|
if not os.path.exists(apk):
|
|
|
|
apk = os.path.join(constants.GetOutDirectory(), 'apks', apk)
|
|
|
|
if not os.path.exists(apk):
|
|
|
|
parser.error('%s not found.' % apk)
|
2012-10-03 01:12:25 +04:00
|
|
|
|
2015-07-07 22:33:23 +03:00
|
|
|
if args.splits:
|
|
|
|
splits = []
|
|
|
|
base_apk_package = apk_helper.ApkHelper(apk).GetPackageName()
|
|
|
|
for split_glob in args.splits:
|
|
|
|
apks = [f for f in glob.glob(split_glob) if f.endswith('.apk')]
|
|
|
|
if not apks:
|
2015-09-08 19:44:59 +03:00
|
|
|
logging.warning('No apks matched for %s.', split_glob)
|
2015-07-07 22:33:23 +03:00
|
|
|
for f in apks:
|
|
|
|
helper = apk_helper.ApkHelper(f)
|
|
|
|
if (helper.GetPackageName() == base_apk_package
|
|
|
|
and helper.GetSplitName()):
|
|
|
|
splits.append(f)
|
|
|
|
|
2015-09-11 20:23:19 +03:00
|
|
|
blacklist = (device_blacklist.Blacklist(args.blacklist_file)
|
|
|
|
if args.blacklist_file
|
|
|
|
else None)
|
2016-04-18 23:02:48 +03:00
|
|
|
devices = device_utils.DeviceUtils.HealthyDevices(blacklist=blacklist,
|
|
|
|
device_arg=args.devices)
|
2012-10-03 01:12:25 +04:00
|
|
|
|
2015-06-18 18:22:12 +03:00
|
|
|
def blacklisting_install(device):
|
|
|
|
try:
|
2015-07-07 22:33:23 +03:00
|
|
|
if args.splits:
|
2015-12-09 05:13:15 +03:00
|
|
|
device.InstallSplitApk(apk, splits, reinstall=args.keep_data,
|
|
|
|
allow_downgrade=args.downgrade)
|
2015-07-07 22:33:23 +03:00
|
|
|
else:
|
2015-12-09 05:13:15 +03:00
|
|
|
device.Install(apk, reinstall=args.keep_data,
|
2016-01-26 00:54:58 +03:00
|
|
|
allow_downgrade=args.downgrade,
|
|
|
|
timeout=args.timeout)
|
2015-06-18 18:22:12 +03:00
|
|
|
except device_errors.CommandFailedError:
|
2015-07-01 19:56:17 +03:00
|
|
|
logging.exception('Failed to install %s', args.apk_name)
|
2015-08-27 06:00:17 +03:00
|
|
|
if blacklist:
|
2015-10-29 04:45:15 +03:00
|
|
|
blacklist.Extend([str(device)], reason='install_failure')
|
2015-08-27 06:00:17 +03:00
|
|
|
logging.warning('Blacklisting %s', str(device))
|
2015-06-18 18:22:12 +03:00
|
|
|
except device_errors.CommandTimeoutError:
|
2015-07-01 19:56:17 +03:00
|
|
|
logging.exception('Timed out while installing %s', args.apk_name)
|
2015-08-27 06:00:17 +03:00
|
|
|
if blacklist:
|
2015-10-29 04:45:15 +03:00
|
|
|
blacklist.Extend([str(device)], reason='install_timeout')
|
2015-08-27 06:00:17 +03:00
|
|
|
logging.warning('Blacklisting %s', str(device))
|
2015-06-18 18:22:12 +03:00
|
|
|
|
|
|
|
device_utils.DeviceUtils.parallel(devices).pMap(blacklisting_install)
|
2012-10-03 01:12:25 +04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-06-18 18:22:12 +03:00
|
|
|
sys.exit(main())
|
2014-05-22 15:13:40 +04:00
|
|
|
|