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."""
14 from pylib
import constants
15 from pylib
.device
import device_blacklist
16 from pylib
.device
import device_errors
17 from pylib
.device
import device_utils
18 from pylib
.utils
import run_tests_helper
22 parser
= argparse
.ArgumentParser()
24 apk_group
= parser
.add_mutually_exclusive_group(required
=True)
25 apk_group
.add_argument('--apk', dest
='apk_name',
26 help='DEPRECATED The name of the apk containing the'
27 ' application (with the .apk extension).')
28 apk_group
.add_argument('apk_path', nargs
='?',
29 help='The path to the APK to install.')
31 # TODO(jbudorick): Remove once no clients pass --apk_package
32 parser
.add_argument('--apk_package', help='DEPRECATED unused')
33 parser
.add_argument('--keep_data',
36 help='Keep the package data when installing '
38 parser
.add_argument('--debug', action
='store_const', const
='Debug',
40 default
=os
.environ
.get('BUILDTYPE', 'Debug'),
41 help='If set, run test suites under out/Debug. '
42 'Default is env var BUILDTYPE or Debug')
43 parser
.add_argument('--release', action
='store_const', const
='Release',
45 help='If set, run test suites under out/Release. '
46 'Default is env var BUILDTYPE or Debug.')
47 parser
.add_argument('-d', '--device', dest
='device',
48 help='Target device for apk to install on.')
49 parser
.add_argument('-v', '--verbose', action
='count',
50 help='Enable verbose logging.')
52 args
= parser
.parse_args()
54 run_tests_helper
.SetLogLevel(args
.verbose
)
55 constants
.SetBuildType(args
.build_type
)
57 apk
= args
.apk_path
or args
.apk_name
58 if not apk
.endswith('.apk'):
60 if not os
.path
.exists(apk
):
61 apk
= os
.path
.join(constants
.GetOutDirectory(), 'apks', apk
)
62 if not os
.path
.exists(apk
):
63 parser
.error('%s not found.' % apk
)
65 devices
= device_utils
.DeviceUtils
.HealthyDevices()
68 devices
= [d
for d
in devices
if d
== args
.device
]
70 raise device_errors
.DeviceUnreachableError(args
.device
)
72 raise device_errors
.NoDevicesError()
74 def blacklisting_install(device
):
76 device
.Install(apk
, reinstall
=args
.keep_data
)
77 except device_errors
.CommandFailedError
:
78 logging
.exception('Failed to install %s', args
.apk_name
)
79 device_blacklist
.ExtendBlacklist([str(device
)])
80 logging
.warning('Blacklisting %s', str(device
))
81 except device_errors
.CommandTimeoutError
:
82 logging
.exception('Timed out while installing %s', args
.apk_name
)
83 device_blacklist
.ExtendBlacklist([str(device
)])
84 logging
.warning('Blacklisting %s', str(device
))
86 device_utils
.DeviceUtils
.parallel(devices
).pMap(blacklisting_install
)
89 if __name__
== '__main__':