Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320)
[chromium-blink-merge.git] / build / android / adb_install_apk.py
blob5d0fd171494090e70247d0552a4cf3d4f2bb6c33
1 #!/usr/bin/env python
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.
7 """Utility script to install APKs from the command line quickly."""
9 import optparse
10 import os
11 import sys
13 from pylib import android_commands
14 from pylib import constants
15 from pylib.device import device_utils
18 def AddInstallAPKOption(option_parser):
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.')
44 def ValidateInstallAPKOption(option_parser, options, args):
45 """Validates the apk option and potentially qualifies the path."""
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.')
52 if not options.apk.endswith('.apk'):
53 options.apk += '.apk'
55 if not os.path.exists(options.apk):
56 options.apk = os.path.join(constants.GetOutDirectory(), 'apks',
57 options.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)
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.")
71 constants.SetBuildType(options.build_type)
72 ValidateInstallAPKOption(parser, options, args)
74 devices = android_commands.GetAttachedDevices()
76 if options.device:
77 if options.device not in devices:
78 raise Exception('Error: %s not in attached devices %s' % (options.device,
79 ','.join(devices)))
80 devices = [options.device]
82 if not devices:
83 raise Exception('Error: no connected devices')
85 device_utils.DeviceUtils.parallel(devices).Install(
86 options.apk, reinstall=options.keep_data)
89 if __name__ == '__main__':
90 sys.exit(main(sys.argv))