blob: 9f280a7d9039ee6c8eaf4dbdb8cd46ee32648e37 [file] [log] [blame]
Kuang-che Wu875c89a2020-01-08 14:30:55 +08001#!/usr/bin/env python3
Kuang-che Wu6e4beca2018-06-27 17:45:02 +08002# -*- coding: utf-8 -*-
Kuang-che Wuacb6efd2018-04-25 18:52:58 +08003# 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 Android container localbuild for ChromeOS bisecting."""
7
8from __future__ import print_function
9import argparse
10import logging
11
12from bisect_kit import android_util
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080013from bisect_kit import arc_util
Kuang-che Wue4bae0b2018-07-19 12:10:14 +080014from bisect_kit import cli
15from bisect_kit import codechange
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080016from bisect_kit import common
17from bisect_kit import configure
Kuang-che Wu0ebbf7c2019-08-28 18:19:19 +080018from bisect_kit import errors
Kuang-che Wufb553102018-10-02 18:14:29 +080019from bisect_kit import locking
Kuang-che Wue4bae0b2018-07-19 12:10:14 +080020from bisect_kit import repo_util
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080021
22logger = logging.getLogger(__name__)
23
24
25def create_argument_parser():
Kuang-che Wud2d6e412021-01-28 16:26:41 +080026 parents = [common.common_argument_parser, common.session_optional_parser]
27 parser = argparse.ArgumentParser(parents=parents)
Kuang-che Wufe1e88a2019-09-10 21:52:25 +080028 cli.patching_argparser_exit(parser)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080029 parser.add_argument(
Kuang-che Wu0ebbf7c2019-08-28 18:19:19 +080030 '--dut',
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080031 type=cli.argtype_notempty,
32 metavar='DUT',
Kuang-che Wu0ebbf7c2019-08-28 18:19:19 +080033 default=configure.get('DUT'),
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080034 help='DUT address')
35 parser.add_argument(
36 'rev',
37 nargs='?',
38 type=cli.argtype_notempty,
39 metavar='INTRA_REV',
40 default=configure.get('INTRA_REV', ''),
41 help='Android build id, or bisecting intra version number')
42 parser.add_argument(
43 '--android_root',
Kuang-che Wud1d45b42018-07-05 00:46:45 +080044 type=cli.argtype_dir_path,
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080045 metavar='ANDROID_ROOT',
46 default=configure.get('ANDROID_ROOT', ''),
47 help='Android tree root')
48 parser.add_argument(
Kuang-che Wud8fc9572018-10-03 21:00:41 +080049 '--android_mirror',
Kuang-che Wud1d45b42018-07-05 00:46:45 +080050 type=cli.argtype_dir_path,
Kuang-che Wud8fc9572018-10-03 21:00:41 +080051 default=configure.get('ANDROID_MIRROR', ''),
Kuang-che Wud1d45b42018-07-05 00:46:45 +080052 help='Android repo mirror path')
53 parser.add_argument(
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080054 '--flavor',
55 metavar='ANDROID_FLAVOR',
56 default=configure.get('ANDROID_FLAVOR'),
57 help='example: cheets_x86-user')
58 parser.add_argument(
Kuang-che Wua2fe9882018-09-05 17:16:31 +080059 '--nobuild',
60 action='store_true',
61 help='Sync source code only; do not build; imply --nodeploy')
62 parser.add_argument(
63 '--nodeploy', action='store_true', help='Do not deploy after build')
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080064 return parser
65
66
67def switch(opts):
Kuang-che Wud1d45b42018-07-05 00:46:45 +080068 config = dict(
69 android_root=opts.android_root,
70 flavor=opts.flavor,
Kuang-che Wud8fc9572018-10-03 21:00:41 +080071 android_mirror=opts.android_mirror)
Kuang-che Wud1d45b42018-07-05 00:46:45 +080072 spec_manager = android_util.AndroidSpecManager(config)
Kuang-che Wud8fc9572018-10-03 21:00:41 +080073 cache = repo_util.RepoMirror(opts.android_mirror)
Kuang-che Wud1d45b42018-07-05 00:46:45 +080074 code_manager = codechange.CodeManager(opts.android_root, spec_manager, cache)
75
76 logger.info('switch source code')
77 code_manager.switch(opts.rev)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080078
Kuang-che Wua2fe9882018-09-05 17:16:31 +080079 if opts.nobuild:
80 return
81
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080082 logger.info('build')
83 # TODO(kcwu): use lower job count for non distributed compilation.
Kuang-che Wufb553102018-10-02 18:14:29 +080084 with locking.lock_file(locking.LOCK_FILE_FOR_BUILD):
85 android_util.lunch(opts.android_root, opts.flavor, 'm', '-j1000')
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080086
87 if not opts.nodeploy:
88 logger.info('deploy')
89 arc_util.push_localbuild_to_device(opts.android_root, opts.dut, opts.flavor)
90
91
Kuang-che Wufe1e88a2019-09-10 21:52:25 +080092@cli.fatal_error_handler
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080093def main(args=None):
94 common.init()
95 parser = create_argument_parser()
96 opts = parser.parse_args(args)
97 common.config_logging(opts)
98
Kuang-che Wu0ebbf7c2019-08-28 18:19:19 +080099 if not opts.dut:
100 if not opts.nodeploy:
101 raise errors.ArgumentError('--dut',
102 'DUT can be omitted only if --nodeploy')
103 if not opts.flavor:
104 raise errors.ArgumentError('--flavor',
105 'flavor must be specified if no --dut')
106
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800107 if not opts.flavor:
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800108 opts.flavor = arc_util.query_flavor(opts.dut)
109 assert opts.flavor
110 logger.info('use flavor=%s', opts.flavor)
111
112 switch(opts)
113
114
115if __name__ == '__main__':
116 main()