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