blob: 4cbe00f35442f64d365342abc94da31c06714394 [file] [log] [blame]
Kuang-che Wu5963ebf2020-10-21 09:01:04 +08001#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright 2020 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 tast prebuilt
7
8It unpacks tast server side prebuilt into $CHROMEOS_ROOT/tmp/tast-prebuilt
9Later, you can run tests using "eval_cros_tast.py --prebuilt" or
10"$CHROMEOS_ROOT/tmp/tast-prebuilt/run_tast.sh".
11"""
12
13from __future__ import print_function
14import argparse
15import logging
16import os
17import shutil
18import tempfile
19
20from bisect_kit import cli
21from bisect_kit import common
22from bisect_kit import configure
23from bisect_kit import cros_util
24from bisect_kit import util
25
26logger = logging.getLogger(__name__)
27
28AUTOTEST_SERVER_TARBALL = 'autotest_server_package.tar.bz2'
29GS_BUILD_PATH = 'gs://chromeos-image-archive/{board}-release/{full_version}'
30GS_AUTOTEST_SERVER_PATH = GS_BUILD_PATH + '/' + AUTOTEST_SERVER_TARBALL
31
32GS_BUILDBUCKET_BUILD_PATH = 'gs://chromeos-image-archive/{path}'
33GS_BUILDBUCKET_BUILD_AUTOTEST_SERVER_PATH = (
34 GS_BUILDBUCKET_BUILD_PATH + '/' + AUTOTEST_SERVER_TARBALL)
35
36
37def create_argument_parser():
Kuang-che Wud2d6e412021-01-28 16:26:41 +080038 parents = [common.common_argument_parser, common.session_optional_parser]
39 parser = argparse.ArgumentParser(description=__doc__, parents=parents)
Kuang-che Wu5963ebf2020-10-21 09:01:04 +080040 cli.patching_argparser_exit(parser)
Kuang-che Wu5963ebf2020-10-21 09:01:04 +080041 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 '--board',
48 metavar='BOARD',
49 default=configure.get('BOARD', ''),
50 help='ChromeOS board name')
51 parser.add_argument(
52 '--dut',
53 type=cli.argtype_notempty,
54 metavar='DUT',
Kuang-che Wu4b3da492020-10-23 17:29:45 +080055 default=configure.get('DUT', ''),
Kuang-che Wu5963ebf2020-10-21 09:01:04 +080056 help='Address of DUT')
57 parser.add_argument(
58 'version',
59 nargs='?',
60 type=cli.argtype_notempty,
61 metavar='CROS_VERSION',
62 default=configure.get('CROS_VERSION', ''),
63 help='ChromeOS local build version string, in format short version, '
64 'full version, or "full,full+N"')
65
66 return parser
67
68
69def switch(opts, board, version, dut):
70 logger.info('Unpack autotest packages for %s %s', board, version)
71 if cros_util.is_cros_short_version(version) or cros_util.is_cros_full_version(
72 version):
73 full_version = cros_util.version_to_full(board, version)
74 autotest_server_path = GS_AUTOTEST_SERVER_PATH.format(
75 board=board, full_version=full_version)
76 else:
77 gs_path = cros_util.query_dut_lsb_release(dut).get(
78 'CHROMEOS_RELEASE_BUILDER_PATH')
79 autotest_server_path = GS_BUILDBUCKET_BUILD_AUTOTEST_SERVER_PATH.format(
80 path=gs_path)
81
82 logger.info('autotest_server_path %s', autotest_server_path)
83
84 # TODO(kcwu): cache downloaded tarballs
85 tmp_dir = tempfile.mkdtemp()
86 tast_dir = os.path.join(opts.chromeos_root, cros_util.prebuilt_tast_dir)
87 if os.path.exists(tast_dir):
88 shutil.rmtree(tast_dir)
89 os.makedirs(tast_dir)
90
91 cros_util.gsutil('cp', autotest_server_path, tmp_dir)
92 tarball = os.path.join(tmp_dir, AUTOTEST_SERVER_TARBALL)
93 util.check_call(
94 'tar', 'xf', tarball, '--strip-components=1', 'tast', cwd=tast_dir)
95
96 shutil.rmtree(tmp_dir)
97
98
99@cli.fatal_error_handler
100def main(args=None):
101 common.init()
102 parser = create_argument_parser()
103 opts = parser.parse_args(args)
104 common.config_logging(opts)
105
106 switch(opts, opts.board, opts.version, opts.dut)
107
108
109if __name__ == '__main__':
110 main()