blob: edd0d2b6e8475037e9efc2fd107cda95fa6a9f6f [file] [log] [blame]
Caroline Ticef6ef4392017-04-06 17:16:05 -07001#!/usr/bin/env python2
Yunlian Jiang2c4b2a12013-04-03 11:52:09 -07002
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.
yunlianb5851d32013-02-19 21:36:46 +00006"""Script to use remote try-bot build image with local gcc."""
7
Caroline Tice88272d42016-01-13 09:48:29 -08008from __future__ import print_function
9
yunlianb5851d32013-02-19 21:36:46 +000010import argparse
yunlianf4566c92013-02-19 22:34:13 +000011import glob
yunlianb5851d32013-02-19 21:36:46 +000012import os
yunlianf4566c92013-02-19 22:34:13 +000013import re
yunlianc9a6e772013-03-04 21:38:20 +000014import shutil
yunlianf4566c92013-02-19 22:34:13 +000015import socket
yunlianb5851d32013-02-19 21:36:46 +000016import sys
yunlianc9a6e772013-03-04 21:38:20 +000017import tempfile
yunlianf4566c92013-02-19 22:34:13 +000018import time
yunlianb5851d32013-02-19 21:36:46 +000019
Caroline Tice88272d42016-01-13 09:48:29 -080020from cros_utils import command_executer
21from cros_utils import logger
22from cros_utils import manifest_versions
23from cros_utils import misc
yunlianb5851d32013-02-19 21:36:46 +000024
Luis Lozanof2a3ef42015-12-15 13:49:30 -080025BRANCH = 'the_actual_branch_used_in_this_script'
26TMP_BRANCH = 'tmp_branch'
yunlianc9a6e772013-03-04 21:38:20 +000027SLEEP_TIME = 600
yunlianf4566c92013-02-19 22:34:13 +000028
Caroline Tice88272d42016-01-13 09:48:29 -080029# pylint: disable=anomalous-backslash-in-string
yunlianf4566c92013-02-19 22:34:13 +000030
Caroline Ticef6ef4392017-04-06 17:16:05 -070031
yunlianf4566c92013-02-19 22:34:13 +000032def GetPatchNum(output):
33 lines = output.splitlines()
Luis Lozanof2a3ef42015-12-15 13:49:30 -080034 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
yunlianc9a6e772013-03-04 21:38:20 +000038 return str(patch_num)
yunlianf4566c92013-02-19 22:34:13 +000039
40
yunlianc9a6e772013-03-04 21:38:20 +000041def GetPatchString(patch):
42 if patch:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080043 return '+'.join(patch)
44 return 'NO_PATCH'
yunlianc9a6e772013-03-04 21:38:20 +000045
46
yunlianb7783c02013-03-12 18:31:43 +000047def FindVersionForToolchain(branch, chromeos_root):
48 """Find the version number in artifacts link in the tryserver email."""
49 # For example: input: toolchain-3701.42.B
yunlianc244ac02013-03-12 21:36:43 +000050 # output: R26-3701.42.1
Luis Lozanof2a3ef42015-12-15 13:49:30 -080051 digits = branch.split('-')[1].split('B')[0]
52 manifest_dir = os.path.join(chromeos_root, 'manifest-internal')
yunlianb7783c02013-03-12 18:31:43 +000053 os.chdir(manifest_dir)
Luis Lozanof2a3ef42015-12-15 13:49:30 -080054 major_version = digits.split('.')[0]
yunlianb7783c02013-03-12 18:31:43 +000055 ce = command_executer.GetCommandExecuter()
Luis Lozanof2a3ef42015-12-15 13:49:30 -080056 command = 'repo sync . && git branch -a | grep {0}'.format(major_version)
Luis Lozano036c9232015-12-10 10:47:01 -080057 _, branches, _ = ce.RunCommandWOutput(command, print_to_console=False)
Luis Lozanof2a3ef42015-12-15 13:49:30 -080058 m = re.search(r'(R\d+)', branches)
yunlianb7783c02013-03-12 18:31:43 +000059 if not m:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080060 logger.GetLogger().LogFatal('Cannot find version for branch {0}'
yunlianb7783c02013-03-12 18:31:43 +000061 .format(branch))
Luis Lozanof2a3ef42015-12-15 13:49:30 -080062 version = m.group(0) + '-' + digits + '1'
yunlianb7783c02013-03-12 18:31:43 +000063 return version
64
65
Luis Lozano8cf53082013-08-01 12:35:11 -070066def FindBuildId(description):
yunlianf4566c92013-02-19 22:34:13 +000067 """Find the build id of the build at trybot server."""
yunlianf4566c92013-02-19 22:34:13 +000068 running_time = 0
yunlianc9a6e772013-03-04 21:38:20 +000069 while True:
Luis Lozano8cf53082013-08-01 12:35:11 -070070 (result, number) = FindBuildIdFromLog(description)
71 if result >= 0:
72 return (result, number)
Luis Lozanof2a3ef42015-12-15 13:49:30 -080073 logger.GetLogger().LogOutput('{0} minutes passed.'
yunlianf4566c92013-02-19 22:34:13 +000074 .format(running_time / 60))
Luis Lozanof2a3ef42015-12-15 13:49:30 -080075 logger.GetLogger().LogOutput('Sleeping {0} seconds.'.format(SLEEP_TIME))
yunlianc9a6e772013-03-04 21:38:20 +000076 time.sleep(SLEEP_TIME)
77 running_time += SLEEP_TIME
yunlianf4566c92013-02-19 22:34:13 +000078
79
Luis Lozano8cf53082013-08-01 12:35:11 -070080def 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
yunlian1d2e8e12013-03-11 23:27:39 +000088
yunlian33fef352013-02-19 22:34:52 +000089 file_dir = os.path.dirname(os.path.realpath(__file__))
Caroline Ticea8af9a72016-07-20 12:52:59 -070090 commands = ('{0}/cros_utils/buildbot_json.py builds '
Luis Lozanof2a3ef42015-12-15 13:49:30 -080091 'http://chromegw/p/tryserver.chromiumos/'.format(file_dir))
yunlian33fef352013-02-19 22:34:52 +000092 ce = command_executer.GetCommandExecuter()
Luis Lozano036c9232015-12-10 10:47:01 -080093 _, buildinfo, _ = ce.RunCommandWOutput(commands, print_to_console=False)
yunlian33fef352013-02-19 22:34:52 +000094
yunlianf4566c92013-02-19 22:34:13 +000095 my_info = buildinfo.splitlines()
96 current_line = 1
yunlian1d2e8e12013-03-11 23:27:39 +000097 running_job = False
Luis Lozano8cf53082013-08-01 12:35:11 -070098 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
yunlianf4566c92013-02-19 22:34:13 +0000103 while current_line < len(my_info):
104 my_dict = {}
105 while True:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800106 key = my_info[current_line].split(':')[0].strip()
107 value = my_info[current_line].split(':', 1)[1].strip()
yunlianf4566c92013-02-19 22:34:13 +0000108 my_dict[key] = value
109 current_line += 1
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800110 if 'Build' in key or current_line == len(my_info):
yunlianf4566c92013-02-19 22:34:13 +0000111 break
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800112 if ('True' not in my_dict['completed'] and
113 str(description) in my_dict['reason']):
yunlian1d2e8e12013-03-11 23:27:39 +0000114 running_job = True
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800115 if ('True' not in my_dict['completed'] or
116 str(description) not in my_dict['reason']):
yunlianf4566c92013-02-19 22:34:13 +0000117 continue
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800118 result = int(my_dict['result'])
119 build_id = int(my_dict['number'])
Luis Lozano8cf53082013-08-01 12:35:11 -0700120 if result == 0:
121 return (result, build_id)
Luis Lozanocd968c82013-08-19 10:42:10 -0700122 else:
Luis Lozano8cf53082013-08-01 12:35:11 -0700123 # 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)
yunlianf4566c92013-02-19 22:34:13 +0000130
131
yunlianc9a6e772013-03-04 21:38:20 +0000132def DownloadImage(target, index, dest, version):
133 """Download artifacts from cloud."""
yunlianf4566c92013-02-19 22:34:13 +0000134 if not os.path.exists(dest):
135 os.makedirs(dest)
136
Luis Lozano8cf53082013-08-01 12:35:11 -0700137 rversion = manifest_versions.RFormatCrosVersion(version)
Caroline Tice88272d42016-01-13 09:48:29 -0800138 print(str(rversion))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800139 # ls_cmd = ("gsutil ls gs://chromeos-image-archive/trybot-{0}/{1}-b{2}"
140 # .format(target, rversion, index))
Caroline Ticef6ef4392017-04-06 17:16:05 -0700141 ls_cmd = ('gsutil ls gs://chromeos-image-archive/trybot-{0}/*-b{2}'.format(
142 target, index))
yunlianf4566c92013-02-19 22:34:13 +0000143
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800144 download_cmd = ('$(which gsutil) cp {0} {1}'.format('{0}', dest))
yunlianf4566c92013-02-19 22:34:13 +0000145 ce = command_executer.GetCommandExecuter()
146
Luis Lozano036c9232015-12-10 10:47:01 -0800147 _, out, _ = ce.RunCommandWOutput(ls_cmd, print_to_console=True)
yunlianf4566c92013-02-19 22:34:13 +0000148 lines = out.splitlines()
Caroline Ticef6ef4392017-04-06 17:16:05 -0700149 download_files = [
150 'autotest.tar', 'chromeos-chrome', 'chromiumos_test_image', 'debug.tgz',
151 'sysroot_chromeos-base_chromeos-chrome.tar.xz'
152 ]
yunlianf4566c92013-02-19 22:34:13 +0000153 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 Lozanof2a3ef42015-12-15 13:49:30 -0800157 logger.GetLogger().LogFatal('Command {0} failed, existing...'
yunlian33fef352013-02-19 22:34:52 +0000158 .format(cmd))
yunlianf4566c92013-02-19 22:34:13 +0000159
160
161def UnpackImage(dest):
162 """Unpack the image, the chroot build dir."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800163 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 Ticef6ef4392017-04-06 17:16:05 -0700169 'tar xJf {0}/chromiumos_test_image.tar.xz -C {0}'.format(
170 dest, chrome_tbz2))
yunlianf4566c92013-02-19 22:34:13 +0000171 ce = command_executer.GetCommandExecuter()
172 return ce.RunCommand(commands)
yunlianb5851d32013-02-19 21:36:46 +0000173
174
yunlianc9a6e772013-03-04 21:38:20 +0000175def RemoveOldBranch():
176 """Remove the branch with name BRANCH."""
177 ce = command_executer.GetCommandExecuter()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800178 command = 'git rev-parse --abbrev-ref HEAD'
Luis Lozano036c9232015-12-10 10:47:01 -0800179 _, out, _ = ce.RunCommandWOutput(command)
yunlianc9a6e772013-03-04 21:38:20 +0000180 if BRANCH in out:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800181 command = 'git checkout -B {0}'.format(TMP_BRANCH)
yunlianc9a6e772013-03-04 21:38:20 +0000182 ce.RunCommand(command)
183 command = "git commit -m 'nouse'"
184 ce.RunCommand(command)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800185 command = 'git branch -D {0}'.format(BRANCH)
yunlianc9a6e772013-03-04 21:38:20 +0000186 ce.RunCommand(command)
yunlianb5851d32013-02-19 21:36:46 +0000187
yunlianb5851d32013-02-19 21:36:46 +0000188
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800189def UploadManifest(manifest, chromeos_root, branch='master'):
yunlianc9a6e772013-03-04 21:38:20 +0000190 """Copy the manifest to $chromeos_root/manifest-internal and upload."""
191 chromeos_root = misc.CanonicalizePath(chromeos_root)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800192 manifest_dir = os.path.join(chromeos_root, 'manifest-internal')
yunlianc9a6e772013-03-04 21:38:20 +0000193 os.chdir(manifest_dir)
194 ce = command_executer.GetCommandExecuter()
yunlian33fef352013-02-19 22:34:52 +0000195
yunlianc9a6e772013-03-04 21:38:20 +0000196 RemoveOldBranch()
yunlianb5851d32013-02-19 21:36:46 +0000197
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800198 if branch != 'master':
199 branch = '{0}'.format(branch)
200 command = 'git checkout -b {0} -t cros-internal/{1}'.format(BRANCH, branch)
yunlianc9a6e772013-03-04 21:38:20 +0000201 ret = ce.RunCommand(command)
202 if ret:
Caroline Tice9099a782016-07-22 16:28:12 -0700203 raise RuntimeError('Command {0} failed'.format(command))
yunlianb5851d32013-02-19 21:36:46 +0000204
yunlianc9a6e772013-03-04 21:38:20 +0000205 # 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 Lozanof2a3ef42015-12-15 13:49:30 -0800209 os.remove(os.path.join(manifest_dir, 'default.xml'))
210 shutil.copyfile(manifest, os.path.join(manifest_dir, 'default.xml'))
yunlianc9a6e772013-03-04 21:38:20 +0000211 return UploadPatch(manifest)
yunlianb5851d32013-02-19 21:36:46 +0000212
yunlianb5851d32013-02-19 21:36:46 +0000213
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800214def GetManifestPatch(manifests, version, chromeos_root, branch='master'):
yunlianc9a6e772013-03-04 21:38:20 +0000215 """Return a gerrit patch number given a version of manifest file."""
216 temp_dir = tempfile.mkdtemp()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800217 to_file = os.path.join(temp_dir, 'default.xml')
Luis Lozano8cf53082013-08-01 12:35:11 -0700218 manifests.GetManifest(version, to_file)
yunlianc9a6e772013-03-04 21:38:20 +0000219 return UploadManifest(to_file, chromeos_root, branch)
yunlianb5851d32013-02-19 21:36:46 +0000220
yunlianf4566c92013-02-19 22:34:13 +0000221
yunlianc9a6e772013-03-04 21:38:20 +0000222def UploadPatch(source):
223 """Up load patch to gerrit, return patch number."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800224 commands = ('git add -A . &&'
yunlianc9a6e772013-03-04 21:38:20 +0000225 "git commit -m 'test' -m 'BUG=None' -m 'TEST=None' "
Caroline Ticef6ef4392017-04-06 17:16:05 -0700226 "-m 'hostname={0}' -m 'source={1}'".format(
227 socket.gethostname(), source))
yunlianc9a6e772013-03-04 21:38:20 +0000228 ce = command_executer.GetCommandExecuter()
229 ce.RunCommand(commands)
yunlian33fef352013-02-19 22:34:52 +0000230
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800231 commands = ('yes | repo upload . --cbr --no-verify')
Luis Lozano036c9232015-12-10 10:47:01 -0800232 _, _, err = ce.RunCommandWOutput(commands)
yunlianc9a6e772013-03-04 21:38:20 +0000233 return GetPatchNum(err)
yunlian952441d2013-02-19 22:34:53 +0000234
yunlian952441d2013-02-19 22:34:53 +0000235
yunlianb7783c02013-03-12 18:31:43 +0000236def ReplaceSysroot(chromeos_root, dest_dir, target):
yunlianc9a6e772013-03-04 21:38:20 +0000237 """Copy unpacked sysroot and image to chromeos_root."""
238 ce = command_executer.GetCommandExecuter()
Luis Lozano9d1db652013-03-25 11:28:57 -0700239 # get the board name from "board-release". board may contain "-"
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800240 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)
yunlianc9a6e772013-03-04 21:38:20 +0000243 ce.RunCommand(command)
244
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800245 command = 'sudo mv {0} {1}'.format(dest_dir, board_dir)
yunlianc9a6e772013-03-04 21:38:20 +0000246 ce.RunCommand(command)
247
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800248 image_dir = os.path.join(chromeos_root, 'src', 'build', 'images', board,
249 'latest')
250 command = 'rm -rf {0} && mkdir -p {0}'.format(image_dir)
yunlianc9a6e772013-03-04 21:38:20 +0000251 ce.RunCommand(command)
252
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800253 command = 'mv {0}/chromiumos_test_image.bin {1}'.format(board_dir, image_dir)
yunlianc9a6e772013-03-04 21:38:20 +0000254 return ce.RunCommand(command)
255
256
257def GccBranchForToolchain(branch):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800258 if branch == 'toolchain-3428.65.B':
259 return 'release-R25-3428.B'
yunlianc9a6e772013-03-04 21:38:20 +0000260 else:
261 return None
262
263
264def GetGccBranch(branch):
265 """Get the remote branch name from branch or version."""
266 ce = command_executer.GetCommandExecuter()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800267 command = 'git branch -a | grep {0}'.format(branch)
Luis Lozano036c9232015-12-10 10:47:01 -0800268 _, out, _ = ce.RunCommandWOutput(command)
yunlianc9a6e772013-03-04 21:38:20 +0000269 if not out:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800270 release_num = re.match(r'.*(R\d+)-*', branch)
yunlianc9a6e772013-03-04 21:38:20 +0000271 if release_num:
272 release_num = release_num.group(0)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800273 command = 'git branch -a | grep {0}'.format(release_num)
Luis Lozano036c9232015-12-10 10:47:01 -0800274 _, out, _ = ce.RunCommandWOutput(command)
yunlianc9a6e772013-03-04 21:38:20 +0000275 if not out:
276 GccBranchForToolchain(branch)
277 if not out:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800278 out = 'remotes/cros/master'
yunlianc9a6e772013-03-04 21:38:20 +0000279 new_branch = out.splitlines()[0]
280 return new_branch
281
282
283def 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 Lozanof2a3ef42015-12-15 13:49:30 -0800287 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'
yunlianc9a6e772013-03-04 21:38:20 +0000289 .format(chromeos_root))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800290 assert os.path.isdir(gcc_dir), ('{0} is not a valid dir for gcc'
291 'source'.format(gcc_dir))
yunlianc9a6e772013-03-04 21:38:20 +0000292 os.chdir(gcc_path)
293 RemoveOldBranch()
yunlianc9a6e772013-03-04 21:38:20 +0000294 if not branch:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800295 branch = 'master'
yunlianc9a6e772013-03-04 21:38:20 +0000296 branch = GetGccBranch(branch)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800297 command = ('git checkout -b {0} -t {1} && ' 'rm -rf *'.format(BRANCH, branch))
yunlianc9a6e772013-03-04 21:38:20 +0000298 ce.RunCommand(command, print_to_console=False)
299
300 command = ("rsync -az --exclude='*.svn' --exclude='*.git'"
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800301 ' {0}/ .'.format(gcc_dir))
yunlianc9a6e772013-03-04 21:38:20 +0000302 ce.RunCommand(command)
303 return UploadPatch(gcc_dir)
304
305
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800306def RunRemote(chromeos_root, branch, patches, is_local, target, chrome_version,
307 dest_dir):
yunlianc9a6e772013-03-04 21:38:20 +0000308 """The actual running commands."""
309 ce = command_executer.GetCommandExecuter()
310
311 if is_local:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800312 local_flag = '--local -r {0}'.format(dest_dir)
yunlianc9a6e772013-03-04 21:38:20 +0000313 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800314 local_flag = '--remote'
315 patch = ''
yunlianc9a6e772013-03-04 21:38:20 +0000316 for p in patches:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800317 patch += ' -g {0}'.format(p)
318 cbuildbot_path = os.path.join(chromeos_root, 'chromite/cbuildbot')
yunlianc9a6e772013-03-04 21:38:20 +0000319 os.chdir(cbuildbot_path)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800320 branch_flag = ''
321 if branch != 'master':
322 branch_flag = ' -b {0}'.format(branch)
323 chrome_version_flag = ''
yunlianc9a6e772013-03-04 21:38:20 +0000324 if chrome_version:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800325 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))
yunlianc9a6e772013-03-04 21:38:20 +0000332 ce.RunCommand(command)
Luis Lozano8cf53082013-08-01 12:35:11 -0700333
yunlianc9a6e772013-03-04 21:38:20 +0000334 return description
yunlianb5851d32013-02-19 21:36:46 +0000335
336
337def Main(argv):
338 """The main function."""
339 # Common initializations
340 parser = argparse.ArgumentParser()
Caroline Ticef6ef4392017-04-06 17:16:05 -0700341 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 Lozanof2a3ef42015-12-15 13:49:30 -0800357 parser.add_argument('-l', '--local', action='store_true')
Caroline Ticef6ef4392017-04-06 17:16:05 -0700358 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 ','"))
yunlianf4566c92013-02-19 22:34:13 +0000399
400 script_dir = os.path.dirname(os.path.realpath(__file__))
yunlianb5851d32013-02-19 21:36:46 +0000401
402 args = parser.parse_args(argv[1:])
yunlianb5851d32013-02-19 21:36:46 +0000403 target = args.target
Yunlian Jiang6845e5f2013-04-10 13:53:37 -0700404 if args.patch:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800405 patch = args.patch.split(',')
Yunlian Jiang6845e5f2013-04-10 13:53:37 -0700406 else:
407 patch = []
yunlianc9a6e772013-03-04 21:38:20 +0000408 chromeos_root = misc.CanonicalizePath(args.chromeos_root)
Yunlian Jiang5b0ad872013-04-03 16:39:27 -0700409 if args.chromeos_version and args.branch:
Caroline Tice9099a782016-07-22 16:28:12 -0700410 raise RuntimeError('You can not set chromeos_version and branch at the '
Caroline Ticef6ef4392017-04-06 17:16:05 -0700411 'same time.')
Luis Lozano8cf53082013-08-01 12:35:11 -0700412
413 manifests = None
yunlianc9a6e772013-03-04 21:38:20 +0000414 if args.branch:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800415 chromeos_version = ''
Luis Lozano8cf53082013-08-01 12:35:11 -0700416 branch = args.branch
yunlianc9a6e772013-03-04 21:38:20 +0000417 else:
418 chromeos_version = args.chromeos_version
Luis Lozano8cf53082013-08-01 12:35:11 -0700419 manifests = manifest_versions.ManifestVersions()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800420 if chromeos_version == 'latest_lkgm':
Luis Lozano8cf53082013-08-01 12:35:11 -0700421 chromeos_version = manifests.TimeToVersion(time.mktime(time.gmtime()))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800422 logger.GetLogger().LogOutput('found version %s for latest LKGM' %
423 (chromeos_version))
Luis Lozano8cf53082013-08-01 12:35:11 -0700424 # TODO: this script currently does not handle the case where the version
425 # is not in the "master" branch
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800426 branch = 'master'
Luis Lozano8cf53082013-08-01 12:35:11 -0700427
yunlianc244ac02013-03-12 21:36:43 +0000428 if chromeos_version:
Luis Lozano8cf53082013-08-01 12:35:11 -0700429 manifest_patch = GetManifestPatch(manifests, chromeos_version,
yunlianc9a6e772013-03-04 21:38:20 +0000430 chromeos_root)
431 patch.append(manifest_patch)
432 if args.gcc_dir:
Luis Lozano8cf53082013-08-01 12:35:11 -0700433 # 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.
yunlianc9a6e772013-03-04 21:38:20 +0000436 patch.append(UploadGccPatch(chromeos_root, args.gcc_dir, branch))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800437 description = RunRemote(chromeos_root, branch, patch, args.local, target,
438 args.chrome_version, args.dest_dir)
yunlianf4566c92013-02-19 22:34:13 +0000439 if args.local or not args.dest_dir:
Luis Lozano8cf53082013-08-01 12:35:11 -0700440 # TODO: We are not checktng the result of cbuild_bot in here!
yunlianf4566c92013-02-19 22:34:13 +0000441 return 0
Luis Lozano8cf53082013-08-01 12:35:11 -0700442
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
yunlianf4566c92013-02-19 22:34:13 +0000449 os.chdir(script_dir)
450 dest_dir = misc.CanonicalizePath(args.dest_dir)
Luis Lozano8cf53082013-08-01 12:35:11 -0700451 (bot_result, build_id) = FindBuildId(description)
452 if bot_result > 0 and build_id > 0:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800453 logger.GetLogger().LogError('Remote trybot failed but image was generated')
Luis Lozano8cf53082013-08-01 12:35:11 -0700454 bot_result = 1
455 elif bot_result > 0:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800456 logger.GetLogger().LogError('Remote trybot failed. No image was generated')
Luis Lozano8cf53082013-08-01 12:35:11 -0700457 return 2
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800458 if 'toolchain' in branch:
yunlianb7783c02013-03-12 18:31:43 +0000459 chromeos_version = FindVersionForToolchain(branch, chromeos_root)
Luis Lozano8cf53082013-08-01 12:35:11 -0700460 assert not manifest_versions.IsRFormatCrosVersion(chromeos_version)
461 DownloadImage(target, build_id, dest_dir, chromeos_version)
yunlianc9a6e772013-03-04 21:38:20 +0000462 ret = UnpackImage(dest_dir)
Luis Lozano8cf53082013-08-01 12:35:11 -0700463 if ret != 0:
464 return 2
465 # todo: return a more inteligent return value
yunlianc9a6e772013-03-04 21:38:20 +0000466 if not args.replace_sysroot:
Luis Lozano8cf53082013-08-01 12:35:11 -0700467 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
yunlianb5851d32013-02-19 21:36:46 +0000475
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800476
477if __name__ == '__main__':
yunlianb5851d32013-02-19 21:36:46 +0000478 retval = Main(sys.argv)
479 sys.exit(retval)