Home | History | Annotate | Download | only in android
      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 
      9 import optparse
     10 import os
     11 import sys
     12 
     13 from pylib import android_commands
     14 from pylib import constants
     15 from pylib.device import device_utils
     16 
     17 
     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.')
     42 
     43 
     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.')
     51 
     52   if not options.apk.endswith('.apk'):
     53     options.apk += '.apk'
     54 
     55   if not os.path.exists(options.apk):
     56     options.apk = os.path.join(constants.GetOutDirectory(), 'apks',
     57                                options.apk)
     58 
     59 
     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)
     65 
     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.")
     70 
     71   constants.SetBuildType(options.build_type)
     72   ValidateInstallAPKOption(parser, options, args)
     73 
     74   devices = android_commands.GetAttachedDevices()
     75 
     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]
     81 
     82   if not devices:
     83     raise Exception('Error: no connected devices')
     84 
     85   device_utils.DeviceUtils.parallel(devices).Install(
     86       options.apk, reinstall=options.keep_data)
     87 
     88 
     89 if __name__ == '__main__':
     90   sys.exit(main(sys.argv))
     91 
     92