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