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