yunlian | b5851d3 | 2013-02-19 21:36:46 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
Yunlian Jiang | 2c4b2a1 | 2013-04-03 11:52:09 -0700 | [diff] [blame] | 2 | |
| 3 | # Copyright (c) 2013 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. |
yunlian | b5851d3 | 2013-02-19 21:36:46 +0000 | [diff] [blame] | 6 | """Script to use remote try-bot build image with local gcc.""" |
| 7 | |
| 8 | import argparse |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 9 | import glob |
yunlian | b5851d3 | 2013-02-19 21:36:46 +0000 | [diff] [blame] | 10 | import os |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 11 | import re |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 12 | import shutil |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 13 | import socket |
yunlian | b5851d3 | 2013-02-19 21:36:46 +0000 | [diff] [blame] | 14 | import sys |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 15 | import tempfile |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 16 | import time |
yunlian | b5851d3 | 2013-02-19 21:36:46 +0000 | [diff] [blame] | 17 | |
| 18 | from utils import command_executer |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 19 | from utils import logger |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 20 | from utils import manifest_versions |
yunlian | b5851d3 | 2013-02-19 21:36:46 +0000 | [diff] [blame] | 21 | from utils import misc |
| 22 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 23 | BRANCH = 'the_actual_branch_used_in_this_script' |
| 24 | TMP_BRANCH = 'tmp_branch' |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 25 | SLEEP_TIME = 600 |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 26 | |
| 27 | |
| 28 | def GetPatchNum(output): |
| 29 | lines = output.splitlines() |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 30 | line = [l for l in lines if 'googlesource' in l][0] |
| 31 | patch_num = re.findall(r'\d+', line)[0] |
| 32 | if 'chrome-internal' in line: |
| 33 | patch_num = '*' + patch_num |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 34 | return str(patch_num) |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 35 | |
| 36 | |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 37 | def GetPatchString(patch): |
| 38 | if patch: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 39 | return '+'.join(patch) |
| 40 | return 'NO_PATCH' |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 41 | |
| 42 | |
yunlian | b7783c0 | 2013-03-12 18:31:43 +0000 | [diff] [blame] | 43 | def FindVersionForToolchain(branch, chromeos_root): |
| 44 | """Find the version number in artifacts link in the tryserver email.""" |
| 45 | # For example: input: toolchain-3701.42.B |
yunlian | c244ac0 | 2013-03-12 21:36:43 +0000 | [diff] [blame] | 46 | # output: R26-3701.42.1 |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 47 | digits = branch.split('-')[1].split('B')[0] |
| 48 | manifest_dir = os.path.join(chromeos_root, 'manifest-internal') |
yunlian | b7783c0 | 2013-03-12 18:31:43 +0000 | [diff] [blame] | 49 | os.chdir(manifest_dir) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 50 | major_version = digits.split('.')[0] |
yunlian | b7783c0 | 2013-03-12 18:31:43 +0000 | [diff] [blame] | 51 | ce = command_executer.GetCommandExecuter() |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 52 | command = 'repo sync . && git branch -a | grep {0}'.format(major_version) |
Luis Lozano | 036c923 | 2015-12-10 10:47:01 -0800 | [diff] [blame] | 53 | _, branches, _ = ce.RunCommandWOutput(command, print_to_console=False) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 54 | m = re.search(r'(R\d+)', branches) |
yunlian | b7783c0 | 2013-03-12 18:31:43 +0000 | [diff] [blame] | 55 | if not m: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 56 | logger.GetLogger().LogFatal('Cannot find version for branch {0}' |
yunlian | b7783c0 | 2013-03-12 18:31:43 +0000 | [diff] [blame] | 57 | .format(branch)) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 58 | version = m.group(0) + '-' + digits + '1' |
yunlian | b7783c0 | 2013-03-12 18:31:43 +0000 | [diff] [blame] | 59 | return version |
| 60 | |
| 61 | |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 62 | def FindBuildId(description): |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 63 | """Find the build id of the build at trybot server.""" |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 64 | running_time = 0 |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 65 | while True: |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 66 | (result, number) = FindBuildIdFromLog(description) |
| 67 | if result >= 0: |
| 68 | return (result, number) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 69 | logger.GetLogger().LogOutput('{0} minutes passed.' |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 70 | .format(running_time / 60)) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 71 | logger.GetLogger().LogOutput('Sleeping {0} seconds.'.format(SLEEP_TIME)) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 72 | time.sleep(SLEEP_TIME) |
| 73 | running_time += SLEEP_TIME |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 74 | |
| 75 | |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 76 | def FindBuildIdFromLog(description): |
| 77 | """Get the build id from build log.""" |
| 78 | # returns tuple (result, buildid) |
| 79 | # result == 0, buildid > 0, the build was successful and we have a build id |
| 80 | # result > 0, buildid > 0, the whole build failed for some reason but we |
| 81 | # do have a build id. |
| 82 | # result == -1, buildid == -1, we have not found a finished build for this |
| 83 | # description yet |
yunlian | 1d2e8e1 | 2013-03-11 23:27:39 +0000 | [diff] [blame] | 84 | |
yunlian | 33fef35 | 2013-02-19 22:34:52 +0000 | [diff] [blame] | 85 | file_dir = os.path.dirname(os.path.realpath(__file__)) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 86 | commands = ('{0}/utils/buildbot_json.py builds ' |
| 87 | 'http://chromegw/p/tryserver.chromiumos/'.format(file_dir)) |
yunlian | 33fef35 | 2013-02-19 22:34:52 +0000 | [diff] [blame] | 88 | ce = command_executer.GetCommandExecuter() |
Luis Lozano | 036c923 | 2015-12-10 10:47:01 -0800 | [diff] [blame] | 89 | _, buildinfo, _ = ce.RunCommandWOutput(commands, print_to_console=False) |
yunlian | 33fef35 | 2013-02-19 22:34:52 +0000 | [diff] [blame] | 90 | |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 91 | my_info = buildinfo.splitlines() |
| 92 | current_line = 1 |
yunlian | 1d2e8e1 | 2013-03-11 23:27:39 +0000 | [diff] [blame] | 93 | running_job = False |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 94 | result = -1 |
| 95 | |
| 96 | # result == 0, we have a successful build |
| 97 | # result > 0, we have a failed build but build id may be valid |
| 98 | # result == -1, we have not found a finished build for this description |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 99 | while current_line < len(my_info): |
| 100 | my_dict = {} |
| 101 | while True: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 102 | key = my_info[current_line].split(':')[0].strip() |
| 103 | value = my_info[current_line].split(':', 1)[1].strip() |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 104 | my_dict[key] = value |
| 105 | current_line += 1 |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 106 | if 'Build' in key or current_line == len(my_info): |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 107 | break |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 108 | if ('True' not in my_dict['completed'] and |
| 109 | str(description) in my_dict['reason']): |
yunlian | 1d2e8e1 | 2013-03-11 23:27:39 +0000 | [diff] [blame] | 110 | running_job = True |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 111 | if ('True' not in my_dict['completed'] or |
| 112 | str(description) not in my_dict['reason']): |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 113 | continue |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 114 | result = int(my_dict['result']) |
| 115 | build_id = int(my_dict['number']) |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 116 | if result == 0: |
| 117 | return (result, build_id) |
Luis Lozano | cd968c8 | 2013-08-19 10:42:10 -0700 | [diff] [blame] | 118 | else: |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 119 | # Found a finished failed build. |
| 120 | # Keep searching to find a successful one |
| 121 | pass |
| 122 | |
| 123 | if result > 0 and not running_job: |
| 124 | return (result, build_id) |
| 125 | return (-1, -1) |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 126 | |
| 127 | |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 128 | def DownloadImage(target, index, dest, version): |
| 129 | """Download artifacts from cloud.""" |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 130 | if not os.path.exists(dest): |
| 131 | os.makedirs(dest) |
| 132 | |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 133 | rversion = manifest_versions.RFormatCrosVersion(version) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 134 | # ls_cmd = ("gsutil ls gs://chromeos-image-archive/trybot-{0}/{1}-b{2}" |
| 135 | # .format(target, rversion, index)) |
| 136 | ls_cmd = ('gsutil ls gs://chromeos-image-archive/trybot-{0}/*-b{2}' |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 137 | .format(target, rversion, index)) |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 138 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 139 | download_cmd = ('$(which gsutil) cp {0} {1}'.format('{0}', dest)) |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 140 | ce = command_executer.GetCommandExecuter() |
| 141 | |
Luis Lozano | 036c923 | 2015-12-10 10:47:01 -0800 | [diff] [blame] | 142 | _, out, _ = ce.RunCommandWOutput(ls_cmd, print_to_console=True) |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 143 | lines = out.splitlines() |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 144 | download_files = ['autotest.tar', 'chromeos-chrome', 'chromiumos_test_image', |
| 145 | 'debug.tgz', 'sysroot_chromeos-base_chromeos-chrome.tar.xz'] |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 146 | for line in lines: |
| 147 | if any([e in line for e in download_files]): |
| 148 | cmd = download_cmd.format(line) |
| 149 | if ce.RunCommand(cmd): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 150 | logger.GetLogger().LogFatal('Command {0} failed, existing...' |
yunlian | 33fef35 | 2013-02-19 22:34:52 +0000 | [diff] [blame] | 151 | .format(cmd)) |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 152 | |
| 153 | |
| 154 | def UnpackImage(dest): |
| 155 | """Unpack the image, the chroot build dir.""" |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 156 | chrome_tbz2 = glob.glob(dest + '/*.tbz2')[0] |
| 157 | commands = ('tar xJf {0}/sysroot_chromeos-base_chromeos-chrome.tar.xz ' |
| 158 | '-C {0} &&' |
| 159 | 'tar xjf {1} -C {0} &&' |
| 160 | 'tar xzf {0}/debug.tgz -C {0}/usr/lib/ &&' |
| 161 | 'tar xf {0}/autotest.tar -C {0}/usr/local/ &&' |
| 162 | 'tar xJf {0}/chromiumos_test_image.tar.xz -C {0}' |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 163 | .format(dest, chrome_tbz2)) |
| 164 | ce = command_executer.GetCommandExecuter() |
| 165 | return ce.RunCommand(commands) |
yunlian | b5851d3 | 2013-02-19 21:36:46 +0000 | [diff] [blame] | 166 | |
| 167 | |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 168 | def RemoveOldBranch(): |
| 169 | """Remove the branch with name BRANCH.""" |
| 170 | ce = command_executer.GetCommandExecuter() |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 171 | command = 'git rev-parse --abbrev-ref HEAD' |
Luis Lozano | 036c923 | 2015-12-10 10:47:01 -0800 | [diff] [blame] | 172 | _, out, _ = ce.RunCommandWOutput(command) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 173 | if BRANCH in out: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 174 | command = 'git checkout -B {0}'.format(TMP_BRANCH) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 175 | ce.RunCommand(command) |
| 176 | command = "git commit -m 'nouse'" |
| 177 | ce.RunCommand(command) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 178 | command = 'git branch -D {0}'.format(BRANCH) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 179 | ce.RunCommand(command) |
yunlian | b5851d3 | 2013-02-19 21:36:46 +0000 | [diff] [blame] | 180 | |
yunlian | b5851d3 | 2013-02-19 21:36:46 +0000 | [diff] [blame] | 181 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 182 | def UploadManifest(manifest, chromeos_root, branch='master'): |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 183 | """Copy the manifest to $chromeos_root/manifest-internal and upload.""" |
| 184 | chromeos_root = misc.CanonicalizePath(chromeos_root) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 185 | manifest_dir = os.path.join(chromeos_root, 'manifest-internal') |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 186 | os.chdir(manifest_dir) |
| 187 | ce = command_executer.GetCommandExecuter() |
yunlian | 33fef35 | 2013-02-19 22:34:52 +0000 | [diff] [blame] | 188 | |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 189 | RemoveOldBranch() |
yunlian | b5851d3 | 2013-02-19 21:36:46 +0000 | [diff] [blame] | 190 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 191 | if branch != 'master': |
| 192 | branch = '{0}'.format(branch) |
| 193 | command = 'git checkout -b {0} -t cros-internal/{1}'.format(BRANCH, branch) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 194 | ret = ce.RunCommand(command) |
| 195 | if ret: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 196 | raise Exception('Command {0} failed'.format(command)) |
yunlian | b5851d3 | 2013-02-19 21:36:46 +0000 | [diff] [blame] | 197 | |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 198 | # We remove the default.xml, which is the symbolic link of full.xml. |
| 199 | # After that, we copy our xml file to default.xml. |
| 200 | # We did this because the full.xml might be updated during the |
| 201 | # run of the script. |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 202 | os.remove(os.path.join(manifest_dir, 'default.xml')) |
| 203 | shutil.copyfile(manifest, os.path.join(manifest_dir, 'default.xml')) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 204 | return UploadPatch(manifest) |
yunlian | b5851d3 | 2013-02-19 21:36:46 +0000 | [diff] [blame] | 205 | |
yunlian | b5851d3 | 2013-02-19 21:36:46 +0000 | [diff] [blame] | 206 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 207 | def GetManifestPatch(manifests, version, chromeos_root, branch='master'): |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 208 | """Return a gerrit patch number given a version of manifest file.""" |
| 209 | temp_dir = tempfile.mkdtemp() |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 210 | to_file = os.path.join(temp_dir, 'default.xml') |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 211 | manifests.GetManifest(version, to_file) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 212 | return UploadManifest(to_file, chromeos_root, branch) |
yunlian | b5851d3 | 2013-02-19 21:36:46 +0000 | [diff] [blame] | 213 | |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 214 | |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 215 | def UploadPatch(source): |
| 216 | """Up load patch to gerrit, return patch number.""" |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 217 | commands = ('git add -A . &&' |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 218 | "git commit -m 'test' -m 'BUG=None' -m 'TEST=None' " |
| 219 | "-m 'hostname={0}' -m 'source={1}'" |
| 220 | .format(socket.gethostname(), source)) |
| 221 | ce = command_executer.GetCommandExecuter() |
| 222 | ce.RunCommand(commands) |
yunlian | 33fef35 | 2013-02-19 22:34:52 +0000 | [diff] [blame] | 223 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 224 | commands = ('yes | repo upload . --cbr --no-verify') |
Luis Lozano | 036c923 | 2015-12-10 10:47:01 -0800 | [diff] [blame] | 225 | _, _, err = ce.RunCommandWOutput(commands) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 226 | return GetPatchNum(err) |
yunlian | 952441d | 2013-02-19 22:34:53 +0000 | [diff] [blame] | 227 | |
yunlian | 952441d | 2013-02-19 22:34:53 +0000 | [diff] [blame] | 228 | |
yunlian | b7783c0 | 2013-03-12 18:31:43 +0000 | [diff] [blame] | 229 | def ReplaceSysroot(chromeos_root, dest_dir, target): |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 230 | """Copy unpacked sysroot and image to chromeos_root.""" |
| 231 | ce = command_executer.GetCommandExecuter() |
Luis Lozano | 9d1db65 | 2013-03-25 11:28:57 -0700 | [diff] [blame] | 232 | # get the board name from "board-release". board may contain "-" |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 233 | board = target.rsplit('-', 1)[0] |
| 234 | board_dir = os.path.join(chromeos_root, 'chroot', 'build', board) |
| 235 | command = 'sudo rm -rf {0}'.format(board_dir) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 236 | ce.RunCommand(command) |
| 237 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 238 | command = 'sudo mv {0} {1}'.format(dest_dir, board_dir) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 239 | ce.RunCommand(command) |
| 240 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 241 | image_dir = os.path.join(chromeos_root, 'src', 'build', 'images', board, |
| 242 | 'latest') |
| 243 | command = 'rm -rf {0} && mkdir -p {0}'.format(image_dir) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 244 | ce.RunCommand(command) |
| 245 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 246 | command = 'mv {0}/chromiumos_test_image.bin {1}'.format(board_dir, image_dir) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 247 | return ce.RunCommand(command) |
| 248 | |
| 249 | |
| 250 | def GccBranchForToolchain(branch): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 251 | if branch == 'toolchain-3428.65.B': |
| 252 | return 'release-R25-3428.B' |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 253 | else: |
| 254 | return None |
| 255 | |
| 256 | |
| 257 | def GetGccBranch(branch): |
| 258 | """Get the remote branch name from branch or version.""" |
| 259 | ce = command_executer.GetCommandExecuter() |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 260 | command = 'git branch -a | grep {0}'.format(branch) |
Luis Lozano | 036c923 | 2015-12-10 10:47:01 -0800 | [diff] [blame] | 261 | _, out, _ = ce.RunCommandWOutput(command) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 262 | if not out: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 263 | release_num = re.match(r'.*(R\d+)-*', branch) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 264 | if release_num: |
| 265 | release_num = release_num.group(0) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 266 | command = 'git branch -a | grep {0}'.format(release_num) |
Luis Lozano | 036c923 | 2015-12-10 10:47:01 -0800 | [diff] [blame] | 267 | _, out, _ = ce.RunCommandWOutput(command) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 268 | if not out: |
| 269 | GccBranchForToolchain(branch) |
| 270 | if not out: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 271 | out = 'remotes/cros/master' |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 272 | new_branch = out.splitlines()[0] |
| 273 | return new_branch |
| 274 | |
| 275 | |
| 276 | def UploadGccPatch(chromeos_root, gcc_dir, branch): |
| 277 | """Upload local gcc to gerrit and get the CL number.""" |
| 278 | ce = command_executer.GetCommandExecuter() |
| 279 | gcc_dir = misc.CanonicalizePath(gcc_dir) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 280 | gcc_path = os.path.join(chromeos_root, 'src/third_party/gcc') |
| 281 | assert os.path.isdir(gcc_path), ('{0} is not a valid chromeos root' |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 282 | .format(chromeos_root)) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 283 | assert os.path.isdir(gcc_dir), ('{0} is not a valid dir for gcc' |
| 284 | 'source'.format(gcc_dir)) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 285 | os.chdir(gcc_path) |
| 286 | RemoveOldBranch() |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 287 | if not branch: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 288 | branch = 'master' |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 289 | branch = GetGccBranch(branch) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 290 | command = ('git checkout -b {0} -t {1} && ' 'rm -rf *'.format(BRANCH, branch)) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 291 | ce.RunCommand(command, print_to_console=False) |
| 292 | |
| 293 | command = ("rsync -az --exclude='*.svn' --exclude='*.git'" |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 294 | ' {0}/ .'.format(gcc_dir)) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 295 | ce.RunCommand(command) |
| 296 | return UploadPatch(gcc_dir) |
| 297 | |
| 298 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 299 | def RunRemote(chromeos_root, branch, patches, is_local, target, chrome_version, |
| 300 | dest_dir): |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 301 | """The actual running commands.""" |
| 302 | ce = command_executer.GetCommandExecuter() |
| 303 | |
| 304 | if is_local: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 305 | local_flag = '--local -r {0}'.format(dest_dir) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 306 | else: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 307 | local_flag = '--remote' |
| 308 | patch = '' |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 309 | for p in patches: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 310 | patch += ' -g {0}'.format(p) |
| 311 | cbuildbot_path = os.path.join(chromeos_root, 'chromite/cbuildbot') |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 312 | os.chdir(cbuildbot_path) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 313 | branch_flag = '' |
| 314 | if branch != 'master': |
| 315 | branch_flag = ' -b {0}'.format(branch) |
| 316 | chrome_version_flag = '' |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 317 | if chrome_version: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 318 | chrome_version_flag = ' --chrome_version={0}'.format(chrome_version) |
| 319 | description = '{0}_{1}_{2}'.format(branch, GetPatchString(patches), target) |
| 320 | command = ('yes | ./cbuildbot {0} {1} {2} {3} {4} {5}' |
| 321 | ' --remote-description={6}' |
| 322 | ' --chrome_rev=tot'.format(patch, branch_flag, chrome_version, |
| 323 | local_flag, chrome_version_flag, target, |
| 324 | description)) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 325 | ce.RunCommand(command) |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 326 | |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 327 | return description |
yunlian | b5851d3 | 2013-02-19 21:36:46 +0000 | [diff] [blame] | 328 | |
| 329 | |
| 330 | def Main(argv): |
| 331 | """The main function.""" |
| 332 | # Common initializations |
| 333 | parser = argparse.ArgumentParser() |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 334 | parser.add_argument('-c', |
| 335 | '--chromeos_root', |
| 336 | required=True, |
| 337 | dest='chromeos_root', |
| 338 | help='The chromeos_root') |
| 339 | parser.add_argument('-g', |
| 340 | '--gcc_dir', |
| 341 | default='', |
| 342 | dest='gcc_dir', |
| 343 | help='The gcc dir') |
| 344 | parser.add_argument('-t', |
| 345 | '--target', |
| 346 | required=True, |
| 347 | dest='target', |
| 348 | help=('The target to be build, the list is at' |
| 349 | ' $(chromeos_root)/chromite/buildbot/cbuildbot' |
| 350 | ' --list -all')) |
| 351 | parser.add_argument('-l', '--local', action='store_true') |
| 352 | parser.add_argument('-d', |
| 353 | '--dest_dir', |
| 354 | dest='dest_dir', |
| 355 | help=('The dir to build the whole chromeos if' |
| 356 | ' --local is set')) |
| 357 | parser.add_argument('--chrome_version', |
| 358 | dest='chrome_version', |
| 359 | default='', |
| 360 | help='The chrome version to use. ' |
| 361 | 'Default it will use the latest one.') |
| 362 | parser.add_argument('--chromeos_version', |
| 363 | dest='chromeos_version', |
| 364 | default='', |
| 365 | help=('The chromeos version to use.' |
| 366 | '(1) A release version in the format: ' |
| 367 | "'\d+\.\d+\.\d+\.\d+.*'" |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 368 | "(2) 'latest_lkgm' for the latest lkgm version")) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 369 | parser.add_argument('-r', |
| 370 | '--replace_sysroot', |
| 371 | action='store_true', |
| 372 | help=('Whether or not to replace the build/$board dir' |
| 373 | 'under the chroot of chromeos_root and copy ' |
| 374 | 'the image to src/build/image/$board/latest.' |
| 375 | ' Default is False')) |
| 376 | parser.add_argument('-b', |
| 377 | '--branch', |
| 378 | dest='branch', |
| 379 | default='', |
| 380 | help=('The branch to run trybot, default is None')) |
| 381 | parser.add_argument('-p', |
| 382 | '--patch', |
| 383 | dest='patch', |
| 384 | default='', |
| 385 | help=('The patches to be applied, the patches numbers ' |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 386 | "be seperated by ','")) |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 387 | |
| 388 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
yunlian | b5851d3 | 2013-02-19 21:36:46 +0000 | [diff] [blame] | 389 | |
| 390 | args = parser.parse_args(argv[1:]) |
yunlian | b5851d3 | 2013-02-19 21:36:46 +0000 | [diff] [blame] | 391 | target = args.target |
Yunlian Jiang | 6845e5f | 2013-04-10 13:53:37 -0700 | [diff] [blame] | 392 | if args.patch: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 393 | patch = args.patch.split(',') |
Yunlian Jiang | 6845e5f | 2013-04-10 13:53:37 -0700 | [diff] [blame] | 394 | else: |
| 395 | patch = [] |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 396 | chromeos_root = misc.CanonicalizePath(args.chromeos_root) |
Yunlian Jiang | 5b0ad87 | 2013-04-03 16:39:27 -0700 | [diff] [blame] | 397 | if args.chromeos_version and args.branch: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 398 | raise Exception('You can not set chromeos_version and branch at the ' |
| 399 | 'same time.') |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 400 | |
| 401 | manifests = None |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 402 | if args.branch: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 403 | chromeos_version = '' |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 404 | branch = args.branch |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 405 | else: |
| 406 | chromeos_version = args.chromeos_version |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 407 | manifests = manifest_versions.ManifestVersions() |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 408 | if chromeos_version == 'latest_lkgm': |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 409 | chromeos_version = manifests.TimeToVersion(time.mktime(time.gmtime())) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 410 | logger.GetLogger().LogOutput('found version %s for latest LKGM' % |
| 411 | (chromeos_version)) |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 412 | # TODO: this script currently does not handle the case where the version |
| 413 | # is not in the "master" branch |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 414 | branch = 'master' |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 415 | |
yunlian | c244ac0 | 2013-03-12 21:36:43 +0000 | [diff] [blame] | 416 | if chromeos_version: |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 417 | manifest_patch = GetManifestPatch(manifests, chromeos_version, |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 418 | chromeos_root) |
| 419 | patch.append(manifest_patch) |
| 420 | if args.gcc_dir: |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 421 | # TODO: everytime we invoke this script we are getting a different |
| 422 | # patch for GCC even if GCC has not changed. The description should |
| 423 | # be based on the MD5 of the GCC patch contents. |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 424 | patch.append(UploadGccPatch(chromeos_root, args.gcc_dir, branch)) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 425 | description = RunRemote(chromeos_root, branch, patch, args.local, target, |
| 426 | args.chrome_version, args.dest_dir) |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 427 | if args.local or not args.dest_dir: |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 428 | # TODO: We are not checktng the result of cbuild_bot in here! |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 429 | return 0 |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 430 | |
| 431 | # return value: |
| 432 | # 0 => build bot was successful and image was put where requested |
| 433 | # 1 => Build bot FAILED but image was put where requested |
| 434 | # 2 => Build bot failed or BUild bot was successful but and image was |
| 435 | # not generated or could not be put where expected |
| 436 | |
yunlian | f4566c9 | 2013-02-19 22:34:13 +0000 | [diff] [blame] | 437 | os.chdir(script_dir) |
| 438 | dest_dir = misc.CanonicalizePath(args.dest_dir) |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 439 | (bot_result, build_id) = FindBuildId(description) |
| 440 | if bot_result > 0 and build_id > 0: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 441 | logger.GetLogger().LogError('Remote trybot failed but image was generated') |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 442 | bot_result = 1 |
| 443 | elif bot_result > 0: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 444 | logger.GetLogger().LogError('Remote trybot failed. No image was generated') |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 445 | return 2 |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 446 | if 'toolchain' in branch: |
yunlian | b7783c0 | 2013-03-12 18:31:43 +0000 | [diff] [blame] | 447 | chromeos_version = FindVersionForToolchain(branch, chromeos_root) |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 448 | assert not manifest_versions.IsRFormatCrosVersion(chromeos_version) |
| 449 | DownloadImage(target, build_id, dest_dir, chromeos_version) |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 450 | ret = UnpackImage(dest_dir) |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 451 | if ret != 0: |
| 452 | return 2 |
| 453 | # todo: return a more inteligent return value |
yunlian | c9a6e77 | 2013-03-04 21:38:20 +0000 | [diff] [blame] | 454 | if not args.replace_sysroot: |
Luis Lozano | 8cf5308 | 2013-08-01 12:35:11 -0700 | [diff] [blame] | 455 | return bot_result |
| 456 | |
| 457 | ret = ReplaceSysroot(chromeos_root, args.dest_dir, target) |
| 458 | if ret != 0: |
| 459 | return 2 |
| 460 | |
| 461 | # got an image and we were successful in placing it where requested |
| 462 | return bot_result |
yunlian | b5851d3 | 2013-02-19 21:36:46 +0000 | [diff] [blame] | 463 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 464 | |
| 465 | if __name__ == '__main__': |
yunlian | b5851d3 | 2013-02-19 21:36:46 +0000 | [diff] [blame] | 466 | retval = Main(sys.argv) |
| 467 | sys.exit(retval) |