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."""
|
|
|
|
|
2012-10-03 01:12:25 +04:00
|
|
|
import multiprocessing
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from pylib import android_commands
|
|
|
|
from pylib import constants
|
2013-03-20 22:06:53 +04:00
|
|
|
from pylib.utils import apk_helper
|
2013-01-08 02:21:47 +04:00
|
|
|
from pylib.utils import test_options_parser
|
2012-10-03 01:12:25 +04:00
|
|
|
|
|
|
|
|
2013-07-09 13:03:26 +04:00
|
|
|
def AddInstallAPKOption(option_parser):
|
|
|
|
"""Adds apk option used to install the APK to the OptionParser."""
|
|
|
|
test_options_parser.AddBuildTypeOption(option_parser)
|
|
|
|
option_parser.add_option('--apk',
|
|
|
|
help=('The name of the apk containing the '
|
|
|
|
' application (with the .apk extension).'))
|
|
|
|
option_parser.add_option('--apk_package',
|
|
|
|
help=('The package name used by the apk containing '
|
|
|
|
'the application.'))
|
|
|
|
option_parser.add_option('--keep_data',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help=('Keep the package data when installing '
|
|
|
|
'the application.'))
|
|
|
|
|
|
|
|
|
|
|
|
def ValidateInstallAPKOption(option_parser, options):
|
|
|
|
"""Validates the apk option and potentially qualifies the path."""
|
|
|
|
if not options.apk:
|
|
|
|
option_parser.error('--apk is mandatory.')
|
|
|
|
if not os.path.exists(options.apk):
|
|
|
|
options.apk = os.path.join(constants.DIR_SOURCE_ROOT,
|
|
|
|
'out', options.build_type,
|
|
|
|
'apks', options.apk)
|
|
|
|
|
|
|
|
|
2012-11-01 22:55:26 +04:00
|
|
|
def _InstallApk(args):
|
2013-01-29 06:43:39 +04:00
|
|
|
apk_path, apk_package, keep_data, device = args
|
2012-10-03 01:12:25 +04:00
|
|
|
result = android_commands.AndroidCommands(device=device).ManagedInstall(
|
2013-01-29 06:43:39 +04:00
|
|
|
apk_path, keep_data, apk_package)
|
2012-10-03 01:12:25 +04:00
|
|
|
print '----- Installed on %s -----' % device
|
|
|
|
print result
|
|
|
|
|
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
parser = optparse.OptionParser()
|
2013-07-09 13:03:26 +04:00
|
|
|
AddInstallAPKOption(parser)
|
2012-10-03 01:12:25 +04:00
|
|
|
options, args = parser.parse_args(argv)
|
2013-07-09 13:03:26 +04:00
|
|
|
ValidateInstallAPKOption(parser, options)
|
2012-10-03 01:12:25 +04:00
|
|
|
if len(args) > 1:
|
|
|
|
raise Exception('Error: Unknown argument:', args[1:])
|
|
|
|
|
|
|
|
devices = android_commands.GetAttachedDevices()
|
|
|
|
if not devices:
|
|
|
|
raise Exception('Error: no connected devices')
|
|
|
|
|
2012-11-01 22:55:26 +04:00
|
|
|
if not options.apk_package:
|
2013-03-20 22:06:53 +04:00
|
|
|
options.apk_package = apk_helper.GetPackageName(options.apk)
|
2012-11-01 22:55:26 +04:00
|
|
|
|
2012-10-03 01:12:25 +04:00
|
|
|
pool = multiprocessing.Pool(len(devices))
|
2012-11-01 22:55:26 +04:00
|
|
|
# Send a tuple (apk_path, apk_package, device) per device.
|
|
|
|
pool.map(_InstallApk, zip([options.apk] * len(devices),
|
|
|
|
[options.apk_package] * len(devices),
|
2013-01-29 06:43:39 +04:00
|
|
|
[options.keep_data] * len(devices),
|
2012-11-01 22:55:26 +04:00
|
|
|
devices))
|
2012-10-03 01:12:25 +04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main(sys.argv))
|