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."""
13 from pylib
import constants
14 from pylib
.device
import device_utils
17 def AddInstallAPKOption(option_parser
):
18 """Adds apk option used to install the APK to the OptionParser."""
19 option_parser
.add_option('--apk',
20 help=('DEPRECATED The name of the apk containing the'
21 ' application (with the .apk extension).'))
22 option_parser
.add_option('--apk_package',
23 help=('DEPRECATED The package name used by the apk '
24 'containing the application.'))
25 option_parser
.add_option('--keep_data',
28 help=('Keep the package data when installing '
30 option_parser
.add_option('--debug', action
='store_const', const
='Debug',
32 default
=os
.environ
.get('BUILDTYPE', 'Debug'),
33 help='If set, run test suites under out/Debug. '
34 'Default is env var BUILDTYPE or Debug')
35 option_parser
.add_option('--release', action
='store_const', const
='Release',
37 help='If set, run test suites under out/Release. '
38 'Default is env var BUILDTYPE or Debug.')
39 option_parser
.add_option('-d', '--device', dest
='device',
40 help='Target device for apk to install on.')
43 def ValidateInstallAPKOption(option_parser
, options
, args
):
44 """Validates the apk option and potentially qualifies the path."""
49 option_parser
.error('apk target not specified.')
51 if not options
.apk
.endswith('.apk'):
54 if not os
.path
.exists(options
.apk
):
55 options
.apk
= os
.path
.join(constants
.GetOutDirectory(), 'apks',
60 parser
= optparse
.OptionParser()
61 parser
.set_usage("usage: %prog [options] target")
62 AddInstallAPKOption(parser
)
63 options
, args
= parser
.parse_args(argv
)
65 if len(args
) > 1 and options
.apk
:
66 parser
.error("Appending the apk as argument can't be used with --apk.")
68 parser
.error("Too many arguments.")
70 constants
.SetBuildType(options
.build_type
)
71 ValidateInstallAPKOption(parser
, options
, args
)
73 devices
= device_utils
.DeviceUtils
.HealthyDevices()
76 device_serials
= [d
.adb
.GetDeviceSerial() for d
in devices
]
77 if options
.device
not in device_serials
:
78 raise Exception('Error: %s not in attached devices %s' % (options
.device
,
79 ','.join(device_serials
)))
80 devices
= [options
.device
]
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
))