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