Dan Shi | db0366c | 2016-02-19 10:36:18 -0800 | [diff] [blame] | 1 | # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Utility functions for interacting with devices supporting adb commands. The |
| 6 | functions only work with an Autotest instance setup, e.g., in a lab. |
Dan Shi | db0366c | 2016-02-19 10:36:18 -0800 | [diff] [blame] | 7 | """ |
| 8 | |
| 9 | |
| 10 | import os |
| 11 | import logging |
| 12 | |
| 13 | import common |
| 14 | from autotest_lib.client.common_lib import error |
| 15 | from autotest_lib.client.common_lib.cros import dev_server |
Dan Shi | db0366c | 2016-02-19 10:36:18 -0800 | [diff] [blame] | 16 | |
| 17 | |
| 18 | def install_apk_from_build(host, apk, build_artifact, package_name=None, |
Tien Chang | f420b78 | 2016-09-22 14:55:03 -0700 | [diff] [blame] | 19 | force_reinstall=False, build_name=None): |
Dan Shi | db0366c | 2016-02-19 10:36:18 -0800 | [diff] [blame] | 20 | """Install the specific apk from given build artifact. |
| 21 | |
| 22 | @param host: An ADBHost object to install apk. |
| 23 | @param apk: Name of the apk to install, e.g., sl4a.apk |
| 24 | @param build_artifact: Name of the build artifact, e.g., test_zip. Note |
| 25 | that it's not the name of the artifact file. Refer to |
| 26 | artifact_info.py in devserver code for the mapping between |
| 27 | artifact name to a build artifact. |
| 28 | @param package_name: Name of the package, e.g., com.android.phone. |
| 29 | If package_name is given, it checks if the package exists before |
| 30 | trying to install it, unless force_reinstall is set to True. |
| 31 | @param force_reinstall: True to reinstall the apk even if it's already |
| 32 | installed. Default is set to False. |
Tien Chang | f420b78 | 2016-09-22 14:55:03 -0700 | [diff] [blame] | 33 | @param build_name: None unless DUT is CrOS with ARC++ container. build_name |
| 34 | points to ARC++ build artifacts. |
Dan Shi | db0366c | 2016-02-19 10:36:18 -0800 | [diff] [blame] | 35 | """ |
| 36 | # Check if apk is already installed. |
| 37 | if package_name and not force_reinstall: |
| 38 | if host.is_apk_installed(package_name): |
| 39 | logging.info('Package %s is already installed.', package_name) |
| 40 | return |
Tien Chang | f420b78 | 2016-09-22 14:55:03 -0700 | [diff] [blame] | 41 | if build_name: |
| 42 | # Pull devserver_url given ARC++ enabled host |
| 43 | host_devserver_url = dev_server.AndroidBuildServer.resolve(build_name, |
| 44 | host.hostname).url() |
| 45 | # Return devserver_url given Android build path |
| 46 | job_repo_url = os.path.join(host_devserver_url, build_name) |
| 47 | else: |
Prathmesh Prabhu | 368abdf | 2017-02-14 11:23:47 -0800 | [diff] [blame] | 48 | info = host.host_info_store.get() |
| 49 | job_repo_url = info.attributes.get(host.job_repo_url_attribute, '') |
Dan Shi | db0366c | 2016-02-19 10:36:18 -0800 | [diff] [blame] | 50 | if not job_repo_url: |
| 51 | raise error.AutoservError( |
| 52 | 'The host %s has no attribute %s. `install_apk_from_build` ' |
| 53 | 'only works for test with image specified.' % |
| 54 | (host.hostname, host.job_repo_url_attribute)) |
xixuan | 9e2c98d | 2016-02-26 19:04:53 -0800 | [diff] [blame] | 55 | devserver_url = dev_server.AndroidBuildServer.get_server_url(job_repo_url) |
Dan Shi | db0366c | 2016-02-19 10:36:18 -0800 | [diff] [blame] | 56 | devserver = dev_server.AndroidBuildServer(devserver_url) |
| 57 | build_info = host.get_build_info_from_build_url(job_repo_url) |
| 58 | devserver.trigger_download(build_info['target'], build_info['build_id'], |
Benny Peake | bdbea87 | 2016-12-07 13:18:09 -0800 | [diff] [blame] | 59 | build_info['branch'], build_artifact, |
| 60 | synchronous=True) |
Dan Shi | db0366c | 2016-02-19 10:36:18 -0800 | [diff] [blame] | 61 | build_info['os_type'] = 'android' |
| 62 | apk_url = devserver.locate_file(apk, build_artifact, None, build_info) |
| 63 | logging.debug('Found apk at: %s', apk_url) |
| 64 | |
| 65 | tmp_dir = host.teststation.get_tmp_dir() |
| 66 | try: |
| 67 | host.download_file(apk_url, apk, tmp_dir) |
| 68 | result = host.install_apk(os.path.join(tmp_dir, apk), |
| 69 | force_reinstall=force_reinstall) |
| 70 | logging.debug(result.stdout) |
| 71 | if package_name and not host.is_apk_installed(package_name): |
| 72 | raise error.AutoservError('No package found with name of %s', |
| 73 | package_name) |
| 74 | logging.info('%s is installed successfully.', apk) |
| 75 | finally: |
| 76 | host.teststation.run('rm -rf %s' % tmp_dir) |