blob: 49cc71f0c077e50df2ae0b0e043822b9815eb794 [file] [log] [blame]
Don Garrettc4114cc2016-11-01 20:04:06 -07001# Copyright 2016 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Bootstrap for cbuildbot.
6
7This script is intended to checkout chromite on the branch specified by -b or
8--branch (as normally accepted by cbuildbot), and then invoke cbuildbot. Most
9arguments are not parsed, only passed along. If a branch is not specified, this
10script will use 'master'.
11
12Among other things, this allows us to invoke build configs that exist on a given
13branch, but not on TOT.
14"""
15
16from __future__ import print_function
17
18import os
19
20from chromite.cbuildbot import repository
21from chromite.lib import commandline
22from chromite.lib import constants
23from chromite.lib import cros_build_lib
24from chromite.lib import cros_logging as logging
25from chromite.lib import git
26from chromite.lib import osutils
27
28def ExtractBranchName(argv):
29 """Extract the branch name from cbuildbot command line arguments.
30
31 Ignores all arguments, other than the branch name.
32
33 Args:
34 argv: The command line arguments to parse.
35
36 Returns:
37 Branch as a string ('master' if nothing is specified).
38 """
39 # Must match cbuildbot._CreateParser().
40 parser = commandline.ArgumentParser(description=__doc__)
41 parser.add_argument('-b', '--branch',
42 default='master',
43 help='The manifest branch to test. The branch to '
44 'check the buildroot out to.')
45
46 # Extract the branch argument, if present, ignore the rest.
47 options, _ = parser.parse_known_args(argv)
48 return options.branch
49
50
51def CloneChromiteOnBranch(branch_name, branch_dir):
52 """Create a worktree of the current chromite checkout.
53
54 Args:
55 branch_name: Name of the branch to checkout, in --branch format, or None.
56 None means use the current branch.
57 branch_dir: Empty directory to worktree into.
58 """
59 assert branch_name
60 logging.info('Creating chromite clone of branch %s in %s',
61 branch_name, branch_dir)
62
63 reference_repo = os.path.join(constants.CHROMITE_DIR, '.git')
64 repository.CloneGitRepo(branch_dir, constants.CHROMITE_URL,
65 reference=reference_repo)
66 git.RunGit(branch_dir, ['checkout', branch_name])
67
68
69def RunCbuildbot(chromite_dir, argv):
70 """Start cbuildbot in specified directory with all arguments.
71
72 Args:
73 chromite_dir: Root of chromite checkout to run cbuildbot in.
74 argv: All command line arguments to pass as list of strings.
75
76 Returns:
77 Return code of cbuildbot as an integer.
78 """
79 logging.info('Bootstrap cbuildbot in: %s', chromite_dir)
80 cbuildbot = os.path.join(chromite_dir, 'bin', 'cbuildbot')
81 result = cros_build_lib.RunCommand([cbuildbot] + argv,
82 error_code_ok=True,
83 cwd=chromite_dir)
84
85 logging.debug('cbuildbot result is: %s', result.returncode)
86 return result.returncode
87
88
89def main(argv):
90 """main method of script.
91
92 Args:
93 argv: All command line arguments to pass as list of strings.
94
95 Returns:
96 Return code of cbuildbot as an integer.
97 """
98 # Specified branch, or 'master'
99 branch_name = ExtractBranchName(argv)
100
101 # Run cbuildbot in branched clone of current repo.
102 with osutils.TempDir(prefix='bootstrap_branched_chromite-') as branch_dir:
103 CloneChromiteOnBranch(branch_name, branch_dir)
104 return RunCbuildbot(branch_dir, argv)