Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame^] | 1 | #!/usr/bin/env python2 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # Copyright 2018 The Chromium OS 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 | """Switcher for ChromeOS autotest prebuilt |
| 7 | |
| 8 | It unpacks autotest prebuilt (both client and server packages) into |
| 9 | $CHROMEOS_ROOT/src/third_party/autotest-prebuilt (that is, autotest_dir). |
| 10 | Later, you can run the prebuilt tests using eval_cros_autotest.py or |
| 11 | "test_that --autotest_dir". |
| 12 | """ |
| 13 | |
| 14 | from __future__ import print_function |
| 15 | import argparse |
| 16 | import logging |
| 17 | import os |
| 18 | import shutil |
| 19 | import tempfile |
| 20 | |
| 21 | from bisect_kit import cli |
| 22 | from bisect_kit import common |
| 23 | from bisect_kit import configure |
| 24 | from bisect_kit import cros_util |
| 25 | from bisect_kit import util |
| 26 | |
| 27 | logger = logging.getLogger(__name__) |
| 28 | |
| 29 | AUTOTEST_CLIENT_TARBALL = 'autotest_packages.tar' |
| 30 | AUTOTEST_SERVER_TARBALL = 'autotest_server_package.tar.bz2' |
| 31 | GS_BUILD_PATH = 'gs://chromeos-image-archive/{board}-release/{full_version}' |
| 32 | GS_AUTOTEST_CLIENT_PATH = GS_BUILD_PATH + '/' + AUTOTEST_CLIENT_TARBALL |
| 33 | GS_AUTOTEST_SERVER_PATH = GS_BUILD_PATH + '/' + AUTOTEST_SERVER_TARBALL |
| 34 | |
| 35 | |
| 36 | def create_argument_parser(): |
| 37 | parser = argparse.ArgumentParser(description=__doc__) |
| 38 | common.add_common_arguments(parser) |
| 39 | parser.add_argument( |
| 40 | '--chromeos_root', |
| 41 | type=cli.argtype_dir_path, |
| 42 | default=configure.get('CHROMEOS_ROOT', ''), |
| 43 | help='ChromeOS tree root') |
| 44 | parser.add_argument( |
| 45 | '--test_name', |
| 46 | help='Client test name, like "video_VideoDecodeAccelerator.h264"') |
| 47 | parser.add_argument( |
| 48 | '--board', |
| 49 | metavar='BOARD', |
| 50 | default=configure.get('BOARD', ''), |
| 51 | help='ChromeOS board name') |
| 52 | parser.add_argument( |
| 53 | 'version', |
| 54 | nargs='?', |
| 55 | type=cros_util.argtype_cros_version, |
| 56 | metavar='CROS_VERSION', |
| 57 | default=configure.get('CROS_VERSION', ''), |
| 58 | help='ChromeOS version number, short (10162.0.0) or full (R64-10162.0.0)') |
| 59 | |
| 60 | return parser |
| 61 | |
| 62 | |
| 63 | def switch(autotest_dir, board, version, test_name): |
| 64 | full_version = cros_util.version_to_full(board, version) |
| 65 | logger.info('Unpack autotest packages for %s %s', board, full_version) |
| 66 | |
| 67 | autotest_client_path = GS_AUTOTEST_CLIENT_PATH.format( |
| 68 | board=board, full_version=full_version) |
| 69 | autotest_server_path = GS_AUTOTEST_SERVER_PATH.format( |
| 70 | board=board, full_version=full_version) |
| 71 | |
| 72 | # TODO(kcwu): cache downloaded tarballs |
| 73 | tmp_dir = tempfile.mkdtemp() |
| 74 | if os.path.exists(autotest_dir): |
| 75 | shutil.rmtree(autotest_dir) |
| 76 | os.makedirs(autotest_dir) |
| 77 | |
| 78 | cros_util.gsutil('cp', autotest_client_path, tmp_dir) |
| 79 | tarball = os.path.join(tmp_dir, AUTOTEST_CLIENT_TARBALL) |
| 80 | # strip 'autotest/' |
| 81 | util.check_call( |
| 82 | 'tar', 'xvf', tarball, '--strip-components=1', cwd=autotest_dir) |
| 83 | |
| 84 | cros_util.gsutil('cp', autotest_server_path, tmp_dir) |
| 85 | tarball = os.path.join(tmp_dir, AUTOTEST_SERVER_TARBALL) |
| 86 | util.check_call( |
| 87 | 'tar', 'xf', tarball, '--strip-components=1', cwd=autotest_dir) |
| 88 | |
| 89 | # Need to extract the control file if the target is a client site test. |
| 90 | if test_name: |
| 91 | test_name = test_name.split('.')[0] |
| 92 | client_tarball = os.path.join(autotest_dir, 'packages', |
| 93 | 'test-%s.tar.bz2' % test_name) |
| 94 | if os.path.exists(client_tarball): |
| 95 | client_test_dir = os.path.join(autotest_dir, 'client', 'site_tests', |
| 96 | test_name) |
| 97 | if not os.path.exists(client_test_dir): |
| 98 | os.makedirs(client_test_dir) |
| 99 | util.check_call('tar', 'xvf', client_tarball, cwd=client_test_dir) |
| 100 | |
| 101 | shutil.rmtree(tmp_dir) |
| 102 | |
| 103 | |
| 104 | def main(args=None): |
| 105 | common.init() |
| 106 | parser = create_argument_parser() |
| 107 | opts = parser.parse_args(args) |
| 108 | common.config_logging(opts) |
| 109 | |
| 110 | autotest_dir = os.path.join(opts.chromeos_root, |
| 111 | cros_util.prebuilt_autotest_dir) |
| 112 | switch(autotest_dir, opts.board, opts.version, opts.test_name) |
| 113 | |
| 114 | # Verify test control file exists. |
| 115 | if opts.test_name: |
| 116 | found = cros_util.get_autotest_test_info(autotest_dir, opts.test_name) |
| 117 | if found: |
| 118 | logger.info('found %s, done', opts.test_name) |
| 119 | else: |
| 120 | logger.warning('test "%s" not found', opts.test_name) |
| 121 | |
| 122 | |
| 123 | if __name__ == '__main__': |
| 124 | main() |