blob: 768f81ad99cc657a80f9df0068f458dd09b4a766 [file] [log] [blame]
Kuang-che Wub9705bd2018-06-28 17:59:18 +08001#!/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 Wu99e808f2019-06-26 12:17:32 +08008It unpacks autotest prebuilt packages into
9$CHROMEOS_ROOT/src/third_party/autotest (that is, autotest_dir).
10Later, you can run tests using "eval_cros_autotest.py --prebuilt" or
Kuang-che Wub9705bd2018-06-28 17:59:18 +080011"test_that --autotest_dir".
12"""
13
14from __future__ import print_function
15import argparse
16import logging
17import os
18import shutil
19import tempfile
20
21from bisect_kit import cli
22from bisect_kit import common
23from bisect_kit import configure
24from bisect_kit import cros_util
25from bisect_kit import util
26
27logger = logging.getLogger(__name__)
28
29AUTOTEST_CLIENT_TARBALL = 'autotest_packages.tar'
Kuang-che Wub9705bd2018-06-28 17:59:18 +080030GS_BUILD_PATH = 'gs://chromeos-image-archive/{board}-release/{full_version}'
31GS_AUTOTEST_CLIENT_PATH = GS_BUILD_PATH + '/' + AUTOTEST_CLIENT_TARBALL
Kuang-che Wub9705bd2018-06-28 17:59:18 +080032
33
34def create_argument_parser():
35 parser = argparse.ArgumentParser(description=__doc__)
36 common.add_common_arguments(parser)
37 parser.add_argument(
38 '--chromeos_root',
39 type=cli.argtype_dir_path,
40 default=configure.get('CHROMEOS_ROOT', ''),
41 help='ChromeOS tree root')
42 parser.add_argument(
43 '--test_name',
44 help='Client test name, like "video_VideoDecodeAccelerator.h264"')
45 parser.add_argument(
46 '--board',
47 metavar='BOARD',
48 default=configure.get('BOARD', ''),
49 help='ChromeOS board name')
50 parser.add_argument(
51 'version',
52 nargs='?',
53 type=cros_util.argtype_cros_version,
54 metavar='CROS_VERSION',
55 default=configure.get('CROS_VERSION', ''),
56 help='ChromeOS version number, short (10162.0.0) or full (R64-10162.0.0)')
57
58 return parser
59
60
Kuang-che Wu99e808f2019-06-26 12:17:32 +080061def switch(autotest_dir, board, version):
Kuang-che Wub9705bd2018-06-28 17:59:18 +080062 full_version = cros_util.version_to_full(board, version)
63 logger.info('Unpack autotest packages for %s %s', board, full_version)
64
65 autotest_client_path = GS_AUTOTEST_CLIENT_PATH.format(
66 board=board, full_version=full_version)
Kuang-che Wub9705bd2018-06-28 17:59:18 +080067
68 # TODO(kcwu): cache downloaded tarballs
69 tmp_dir = tempfile.mkdtemp()
Kuang-che Wu99e808f2019-06-26 12:17:32 +080070 packages_dir = os.path.join(autotest_dir, 'packages')
71 if os.path.exists(packages_dir):
72 shutil.rmtree(packages_dir)
Kuang-che Wub9705bd2018-06-28 17:59:18 +080073
74 cros_util.gsutil('cp', autotest_client_path, tmp_dir)
75 tarball = os.path.join(tmp_dir, AUTOTEST_CLIENT_TARBALL)
76 # strip 'autotest/'
77 util.check_call(
Kuang-che Wu99e808f2019-06-26 12:17:32 +080078 'tar',
79 'xvf',
80 tarball,
81 '--strip-components=1',
82 'autotest/packages',
83 cwd=autotest_dir)
Kuang-che Wub9705bd2018-06-28 17:59:18 +080084
85 shutil.rmtree(tmp_dir)
86
87
88def main(args=None):
89 common.init()
90 parser = create_argument_parser()
91 opts = parser.parse_args(args)
92 common.config_logging(opts)
93
Kuang-che Wu99e808f2019-06-26 12:17:32 +080094 autotest_dir = os.path.join(opts.chromeos_root, cros_util.autotest_path)
Kuang-che Wub9705bd2018-06-28 17:59:18 +080095 # Verify test control file exists.
96 if opts.test_name:
97 found = cros_util.get_autotest_test_info(autotest_dir, opts.test_name)
Kuang-che Wu99e808f2019-06-26 12:17:32 +080098 if not found:
99 names = []
100 for control_file in cros_util.enumerate_autotest_control_files(
101 autotest_dir):
102 info = cros_util.parse_autotest_control_file(control_file)
103 names.append(info.name)
104 util.show_similar_candidates('test_name', opts.test_name, names)
105
106 switch(autotest_dir, opts.board, opts.version)
Kuang-che Wub9705bd2018-06-28 17:59:18 +0800107
108
109if __name__ == '__main__':
110 main()