Kuang-che Wu | 5963ebf | 2020-10-21 09:01:04 +0800 | [diff] [blame] | 1 | #!/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 | |
| 8 | It unpacks tast server side prebuilt into $CHROMEOS_ROOT/tmp/tast-prebuilt |
| 9 | Later, you can run tests using "eval_cros_tast.py --prebuilt" or |
| 10 | "$CHROMEOS_ROOT/tmp/tast-prebuilt/run_tast.sh". |
| 11 | """ |
| 12 | |
| 13 | from __future__ import print_function |
| 14 | import argparse |
| 15 | import logging |
| 16 | import os |
| 17 | import shutil |
| 18 | import tempfile |
| 19 | |
| 20 | from bisect_kit import cli |
| 21 | from bisect_kit import common |
| 22 | from bisect_kit import configure |
| 23 | from bisect_kit import cros_util |
| 24 | from bisect_kit import util |
| 25 | |
| 26 | logger = logging.getLogger(__name__) |
| 27 | |
| 28 | AUTOTEST_SERVER_TARBALL = 'autotest_server_package.tar.bz2' |
| 29 | GS_BUILD_PATH = 'gs://chromeos-image-archive/{board}-release/{full_version}' |
| 30 | GS_AUTOTEST_SERVER_PATH = GS_BUILD_PATH + '/' + AUTOTEST_SERVER_TARBALL |
| 31 | |
| 32 | GS_BUILDBUCKET_BUILD_PATH = 'gs://chromeos-image-archive/{path}' |
| 33 | GS_BUILDBUCKET_BUILD_AUTOTEST_SERVER_PATH = ( |
| 34 | GS_BUILDBUCKET_BUILD_PATH + '/' + AUTOTEST_SERVER_TARBALL) |
| 35 | |
| 36 | |
| 37 | def create_argument_parser(): |
Kuang-che Wu | d2d6e41 | 2021-01-28 16:26:41 +0800 | [diff] [blame] | 38 | parents = [common.common_argument_parser, common.session_optional_parser] |
| 39 | parser = argparse.ArgumentParser(description=__doc__, parents=parents) |
Kuang-che Wu | 5963ebf | 2020-10-21 09:01:04 +0800 | [diff] [blame] | 40 | cli.patching_argparser_exit(parser) |
Kuang-che Wu | 5963ebf | 2020-10-21 09:01:04 +0800 | [diff] [blame] | 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 | '--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 Wu | 4b3da49 | 2020-10-23 17:29:45 +0800 | [diff] [blame] | 55 | default=configure.get('DUT', ''), |
Kuang-che Wu | 5963ebf | 2020-10-21 09:01:04 +0800 | [diff] [blame] | 56 | 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 | |
| 69 | def 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 |
| 100 | def 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 | |
| 109 | if __name__ == '__main__': |
| 110 | main() |