blob: 2e010983425af6222a68c934c379e3abce27d54b [file] [log] [blame]
Caroline Tice88272d42016-01-13 09:48:29 -08001#!/usr/bin/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
31def GetPatchNum(output):
32 lines = output.splitlines()
Luis Lozanof2a3ef42015-12-15 13:49:30 -080033 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
yunlianc9a6e772013-03-04 21:38:20 +000037 return str(patch_num)
yunlianf4566c92013-02-19 22:34:13 +000038
39
yunlianc9a6e772013-03-04 21:38:20 +000040def GetPatchString(patch):
41 if patch:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080042 return '+'.join(patch)
43 return 'NO_PATCH'
yunlianc9a6e772013-03-04 21:38:20 +000044
45
yunlianb7783c02013-03-12 18:31:43 +000046def FindVersionForToolchain(branch, chromeos_root):
47 """Find the version number in artifacts link in the tryserver email."""
48 # For example: input: toolchain-3701.42.B
yunlianc244ac02013-03-12 21:36:43 +000049 # output: R26-3701.42.1
Luis Lozanof2a3ef42015-12-15 13:49:30 -080050 digits = branch.split('-')[1].split('B')[0]
51 manifest_dir = os.path.join(chromeos_root, 'manifest-internal')
yunlianb7783c02013-03-12 18:31:43 +000052 os.chdir(manifest_dir)
Luis Lozanof2a3ef42015-12-15 13:49:30 -080053 major_version = digits.split('.')[0]
yunlianb7783c02013-03-12 18:31:43 +000054 ce = command_executer.GetCommandExecuter()
Luis Lozanof2a3ef42015-12-15 13:49:30 -080055 command = 'repo sync . && git branch -a | grep {0}'.format(major_version)
Luis Lozano036c9232015-12-10 10:47:01 -080056 _, branches, _ = ce.RunCommandWOutput(command, print_to_console=False)
Luis Lozanof2a3ef42015-12-15 13:49:30 -080057 m = re.search(r'(R\d+)', branches)
yunlianb7783c02013-03-12 18:31:43 +000058 if not m:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080059 logger.GetLogger().LogFatal('Cannot find version for branch {0}'
yunlianb7783c02013-03-12 18:31:43 +000060 .format(branch))
Luis Lozanof2a3ef42015-12-15 13:49:30 -080061 version = m.group(0) + '-' + digits + '1'
yunlianb7783c02013-03-12 18:31:43 +000062 return version
63
64
Luis Lozano8cf53082013-08-01 12:35:11 -070065def FindBuildId(description):
yunlianf4566c92013-02-19 22:34:13 +000066 """Find the build id of the build at trybot server."""
yunlianf4566c92013-02-19 22:34:13 +000067 running_time = 0
yunlianc9a6e772013-03-04 21:38:20 +000068 while True:
Luis Lozano8cf53082013-08-01 12:35:11 -070069 (result, number) = FindBuildIdFromLog(description)
70 if result >= 0:
71 return (result, number)
Luis Lozanof2a3ef42015-12-15 13:49:30 -080072 logger.GetLogger().LogOutput('{0} minutes passed.'
yunlianf4566c92013-02-19 22:34:13 +000073 .format(running_time / 60))
Luis Lozanof2a3ef42015-12-15 13:49:30 -080074 logger.GetLogger().LogOutput('Sleeping {0} seconds.'.format(SLEEP_TIME))
yunlianc9a6e772013-03-04 21:38:20 +000075 time.sleep(SLEEP_TIME)
76 running_time += SLEEP_TIME
yunlianf4566c92013-02-19 22:34:13 +000077
78
Luis Lozano8cf53082013-08-01 12:35:11 -070079def 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
yunlian1d2e8e12013-03-11 23:27:39 +000087
yunlian33fef352013-02-19 22:34:52 +000088 file_dir = os.path.dirname(os.path.realpath(__file__))
Luis Lozanof2a3ef42015-12-15 13:49:30 -080089 commands = ('{0}/utils/buildbot_json.py builds '
90 'http://chromegw/p/tryserver.chromiumos/'.format(file_dir))
yunlian33fef352013-02-19 22:34:52 +000091 ce = command_executer.GetCommandExecuter()
Luis Lozano036c9232015-12-10 10:47:01 -080092 _, buildinfo, _ = ce.RunCommandWOutput(commands, print_to_console=False)
yunlian33fef352013-02-19 22:34:52 +000093
yunlianf4566c92013-02-19 22:34:13 +000094 my_info = buildinfo.splitlines()
95 current_line = 1
yunlian1d2e8e12013-03-11 23:27:39 +000096 running_job = False
Luis Lozano8cf53082013-08-01 12:35:11 -070097 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
yunlianf4566c92013-02-19 22:34:13 +0000102 while current_line < len(my_info):
103 my_dict = {}
104 while True:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800105 key = my_info[current_line].split(':')[0].strip()
106 value = my_info[current_line].split(':', 1)[1].strip()
yunlianf4566c92013-02-19 22:34:13 +0000107 my_dict[key] = value
108 current_line += 1
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800109 if 'Build' in key or current_line == len(my_info):
yunlianf4566c92013-02-19 22:34:13 +0000110 break
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800111 if ('True' not in my_dict['completed'] and
112 str(description) in my_dict['reason']):
yunlian1d2e8e12013-03-11 23:27:39 +0000113 running_job = True
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800114 if ('True' not in my_dict['completed'] or
115 str(description) not in my_dict['reason']):
yunlianf4566c92013-02-19 22:34:13 +0000116 continue
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800117 result = int(my_dict['result'])
118 build_id = int(my_dict['number'])
Luis Lozano8cf53082013-08-01 12:35:11 -0700119 if result == 0:
120 return (result, build_id)
Luis Lozanocd968c82013-08-19 10:42:10 -0700121 else:
Luis Lozano8cf53082013-08-01 12:35:11 -0700122 # 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)
yunlianf4566c92013-02-19 22:34:13 +0000129
130
yunlianc9a6e772013-03-04 21:38:20 +0000131def DownloadImage(target, index, dest, version):
132 """Download artifacts from cloud."""
yunlianf4566c92013-02-19 22:34:13 +0000133 if not os.path.exists(dest):
134 os.makedirs(dest)
135
Luis Lozano8cf53082013-08-01 12:35:11 -0700136 rversion = manifest_versions.RFormatCrosVersion(version)
Caroline Tice88272d42016-01-13 09:48:29 -0800137 print(str(rversion))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800138 # 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 Tice88272d42016-01-13 09:48:29 -0800141 .format(target, index))
yunlianf4566c92013-02-19 22:34:13 +0000142
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800143 download_cmd = ('$(which gsutil) cp {0} {1}'.format('{0}', dest))
yunlianf4566c92013-02-19 22:34:13 +0000144 ce = command_executer.GetCommandExecuter()
145
Luis Lozano036c9232015-12-10 10:47:01 -0800146 _, out, _ = ce.RunCommandWOutput(ls_cmd, print_to_console=True)
yunlianf4566c92013-02-19 22:34:13 +0000147 lines = out.splitlines()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800148 download_files = ['autotest.tar', 'chromeos-chrome', 'chromiumos_test_image',
149 'debug.tgz', 'sysroot_chromeos-base_chromeos-chrome.tar.xz']
yunlianf4566c92013-02-19 22:34:13 +0000150 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 Lozanof2a3ef42015-12-15 13:49:30 -0800154 logger.GetLogger().LogFatal('Command {0} failed, existing...'
yunlian33fef352013-02-19 22:34:52 +0000155 .format(cmd))
yunlianf4566c92013-02-19 22:34:13 +0000156
157
158def UnpackImage(dest):
159 """Unpack the image, the chroot build dir."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800160 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}'
yunlianf4566c92013-02-19 22:34:13 +0000167 .format(dest, chrome_tbz2))
168 ce = command_executer.GetCommandExecuter()
169 return ce.RunCommand(commands)
yunlianb5851d32013-02-19 21:36:46 +0000170
171
yunlianc9a6e772013-03-04 21:38:20 +0000172def RemoveOldBranch():
173 """Remove the branch with name BRANCH."""
174 ce = command_executer.GetCommandExecuter()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800175 command = 'git rev-parse --abbrev-ref HEAD'
Luis Lozano036c9232015-12-10 10:47:01 -0800176 _, out, _ = ce.RunCommandWOutput(command)
yunlianc9a6e772013-03-04 21:38:20 +0000177 if BRANCH in out:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800178 command = 'git checkout -B {0}'.format(TMP_BRANCH)
yunlianc9a6e772013-03-04 21:38:20 +0000179 ce.RunCommand(command)
180 command = "git commit -m 'nouse'"
181 ce.RunCommand(command)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800182 command = 'git branch -D {0}'.format(BRANCH)
yunlianc9a6e772013-03-04 21:38:20 +0000183 ce.RunCommand(command)
yunlianb5851d32013-02-19 21:36:46 +0000184
yunlianb5851d32013-02-19 21:36:46 +0000185
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800186def UploadManifest(manifest, chromeos_root, branch='master'):
yunlianc9a6e772013-03-04 21:38:20 +0000187 """Copy the manifest to $chromeos_root/manifest-internal and upload."""
188 chromeos_root = misc.CanonicalizePath(chromeos_root)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800189 manifest_dir = os.path.join(chromeos_root, 'manifest-internal')
yunlianc9a6e772013-03-04 21:38:20 +0000190 os.chdir(manifest_dir)
191 ce = command_executer.GetCommandExecuter()
yunlian33fef352013-02-19 22:34:52 +0000192
yunlianc9a6e772013-03-04 21:38:20 +0000193 RemoveOldBranch()
yunlianb5851d32013-02-19 21:36:46 +0000194
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800195 if branch != 'master':
196 branch = '{0}'.format(branch)
197 command = 'git checkout -b {0} -t cros-internal/{1}'.format(BRANCH, branch)
yunlianc9a6e772013-03-04 21:38:20 +0000198 ret = ce.RunCommand(command)
199 if ret:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800200 raise Exception('Command {0} failed'.format(command))
yunlianb5851d32013-02-19 21:36:46 +0000201
yunlianc9a6e772013-03-04 21:38:20 +0000202 # 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 Lozanof2a3ef42015-12-15 13:49:30 -0800206 os.remove(os.path.join(manifest_dir, 'default.xml'))
207 shutil.copyfile(manifest, os.path.join(manifest_dir, 'default.xml'))
yunlianc9a6e772013-03-04 21:38:20 +0000208 return UploadPatch(manifest)
yunlianb5851d32013-02-19 21:36:46 +0000209
yunlianb5851d32013-02-19 21:36:46 +0000210
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800211def GetManifestPatch(manifests, version, chromeos_root, branch='master'):
yunlianc9a6e772013-03-04 21:38:20 +0000212 """Return a gerrit patch number given a version of manifest file."""
213 temp_dir = tempfile.mkdtemp()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800214 to_file = os.path.join(temp_dir, 'default.xml')
Luis Lozano8cf53082013-08-01 12:35:11 -0700215 manifests.GetManifest(version, to_file)
yunlianc9a6e772013-03-04 21:38:20 +0000216 return UploadManifest(to_file, chromeos_root, branch)
yunlianb5851d32013-02-19 21:36:46 +0000217
yunlianf4566c92013-02-19 22:34:13 +0000218
yunlianc9a6e772013-03-04 21:38:20 +0000219def UploadPatch(source):
220 """Up load patch to gerrit, return patch number."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800221 commands = ('git add -A . &&'
yunlianc9a6e772013-03-04 21:38:20 +0000222 "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)
yunlian33fef352013-02-19 22:34:52 +0000227
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800228 commands = ('yes | repo upload . --cbr --no-verify')
Luis Lozano036c9232015-12-10 10:47:01 -0800229 _, _, err = ce.RunCommandWOutput(commands)
yunlianc9a6e772013-03-04 21:38:20 +0000230 return GetPatchNum(err)
yunlian952441d2013-02-19 22:34:53 +0000231
yunlian952441d2013-02-19 22:34:53 +0000232
yunlianb7783c02013-03-12 18:31:43 +0000233def ReplaceSysroot(chromeos_root, dest_dir, target):
yunlianc9a6e772013-03-04 21:38:20 +0000234 """Copy unpacked sysroot and image to chromeos_root."""
235 ce = command_executer.GetCommandExecuter()
Luis Lozano9d1db652013-03-25 11:28:57 -0700236 # get the board name from "board-release". board may contain "-"
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800237 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)
yunlianc9a6e772013-03-04 21:38:20 +0000240 ce.RunCommand(command)
241
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800242 command = 'sudo mv {0} {1}'.format(dest_dir, board_dir)
yunlianc9a6e772013-03-04 21:38:20 +0000243 ce.RunCommand(command)
244
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800245 image_dir = os.path.join(chromeos_root, 'src', 'build', 'images', board,
246 'latest')
247 command = 'rm -rf {0} && mkdir -p {0}'.format(image_dir)
yunlianc9a6e772013-03-04 21:38:20 +0000248 ce.RunCommand(command)
249
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800250 command = 'mv {0}/chromiumos_test_image.bin {1}'.format(board_dir, image_dir)
yunlianc9a6e772013-03-04 21:38:20 +0000251 return ce.RunCommand(command)
252
253
254def GccBranchForToolchain(branch):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800255 if branch == 'toolchain-3428.65.B':
256 return 'release-R25-3428.B'
yunlianc9a6e772013-03-04 21:38:20 +0000257 else:
258 return None
259
260
261def GetGccBranch(branch):
262 """Get the remote branch name from branch or version."""
263 ce = command_executer.GetCommandExecuter()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800264 command = 'git branch -a | grep {0}'.format(branch)
Luis Lozano036c9232015-12-10 10:47:01 -0800265 _, out, _ = ce.RunCommandWOutput(command)
yunlianc9a6e772013-03-04 21:38:20 +0000266 if not out:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800267 release_num = re.match(r'.*(R\d+)-*', branch)
yunlianc9a6e772013-03-04 21:38:20 +0000268 if release_num:
269 release_num = release_num.group(0)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800270 command = 'git branch -a | grep {0}'.format(release_num)
Luis Lozano036c9232015-12-10 10:47:01 -0800271 _, out, _ = ce.RunCommandWOutput(command)
yunlianc9a6e772013-03-04 21:38:20 +0000272 if not out:
273 GccBranchForToolchain(branch)
274 if not out:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800275 out = 'remotes/cros/master'
yunlianc9a6e772013-03-04 21:38:20 +0000276 new_branch = out.splitlines()[0]
277 return new_branch
278
279
280def 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 Lozanof2a3ef42015-12-15 13:49:30 -0800284 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'
yunlianc9a6e772013-03-04 21:38:20 +0000286 .format(chromeos_root))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800287 assert os.path.isdir(gcc_dir), ('{0} is not a valid dir for gcc'
288 'source'.format(gcc_dir))
yunlianc9a6e772013-03-04 21:38:20 +0000289 os.chdir(gcc_path)
290 RemoveOldBranch()
yunlianc9a6e772013-03-04 21:38:20 +0000291 if not branch:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800292 branch = 'master'
yunlianc9a6e772013-03-04 21:38:20 +0000293 branch = GetGccBranch(branch)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800294 command = ('git checkout -b {0} -t {1} && ' 'rm -rf *'.format(BRANCH, branch))
yunlianc9a6e772013-03-04 21:38:20 +0000295 ce.RunCommand(command, print_to_console=False)
296
297 command = ("rsync -az --exclude='*.svn' --exclude='*.git'"
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800298 ' {0}/ .'.format(gcc_dir))
yunlianc9a6e772013-03-04 21:38:20 +0000299 ce.RunCommand(command)
300 return UploadPatch(gcc_dir)
301
302
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800303def RunRemote(chromeos_root, branch, patches, is_local, target, chrome_version,
304 dest_dir):
yunlianc9a6e772013-03-04 21:38:20 +0000305 """The actual running commands."""
306 ce = command_executer.GetCommandExecuter()
307
308 if is_local:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800309 local_flag = '--local -r {0}'.format(dest_dir)
yunlianc9a6e772013-03-04 21:38:20 +0000310 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800311 local_flag = '--remote'
312 patch = ''
yunlianc9a6e772013-03-04 21:38:20 +0000313 for p in patches:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800314 patch += ' -g {0}'.format(p)
315 cbuildbot_path = os.path.join(chromeos_root, 'chromite/cbuildbot')
yunlianc9a6e772013-03-04 21:38:20 +0000316 os.chdir(cbuildbot_path)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800317 branch_flag = ''
318 if branch != 'master':
319 branch_flag = ' -b {0}'.format(branch)
320 chrome_version_flag = ''
yunlianc9a6e772013-03-04 21:38:20 +0000321 if chrome_version:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800322 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))
yunlianc9a6e772013-03-04 21:38:20 +0000329 ce.RunCommand(command)
Luis Lozano8cf53082013-08-01 12:35:11 -0700330
yunlianc9a6e772013-03-04 21:38:20 +0000331 return description
yunlianb5851d32013-02-19 21:36:46 +0000332
333
334def Main(argv):
335 """The main function."""
336 # Common initializations
337 parser = argparse.ArgumentParser()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800338 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 Lozano8cf53082013-08-01 12:35:11 -0700372 "(2) 'latest_lkgm' for the latest lkgm version"))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800373 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 '
yunlianc9a6e772013-03-04 21:38:20 +0000390 "be seperated by ','"))
yunlianf4566c92013-02-19 22:34:13 +0000391
392 script_dir = os.path.dirname(os.path.realpath(__file__))
yunlianb5851d32013-02-19 21:36:46 +0000393
394 args = parser.parse_args(argv[1:])
yunlianb5851d32013-02-19 21:36:46 +0000395 target = args.target
Yunlian Jiang6845e5f2013-04-10 13:53:37 -0700396 if args.patch:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800397 patch = args.patch.split(',')
Yunlian Jiang6845e5f2013-04-10 13:53:37 -0700398 else:
399 patch = []
yunlianc9a6e772013-03-04 21:38:20 +0000400 chromeos_root = misc.CanonicalizePath(args.chromeos_root)
Yunlian Jiang5b0ad872013-04-03 16:39:27 -0700401 if args.chromeos_version and args.branch:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800402 raise Exception('You can not set chromeos_version and branch at the '
403 'same time.')
Luis Lozano8cf53082013-08-01 12:35:11 -0700404
405 manifests = None
yunlianc9a6e772013-03-04 21:38:20 +0000406 if args.branch:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800407 chromeos_version = ''
Luis Lozano8cf53082013-08-01 12:35:11 -0700408 branch = args.branch
yunlianc9a6e772013-03-04 21:38:20 +0000409 else:
410 chromeos_version = args.chromeos_version
Luis Lozano8cf53082013-08-01 12:35:11 -0700411 manifests = manifest_versions.ManifestVersions()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800412 if chromeos_version == 'latest_lkgm':
Luis Lozano8cf53082013-08-01 12:35:11 -0700413 chromeos_version = manifests.TimeToVersion(time.mktime(time.gmtime()))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800414 logger.GetLogger().LogOutput('found version %s for latest LKGM' %
415 (chromeos_version))
Luis Lozano8cf53082013-08-01 12:35:11 -0700416 # TODO: this script currently does not handle the case where the version
417 # is not in the "master" branch
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800418 branch = 'master'
Luis Lozano8cf53082013-08-01 12:35:11 -0700419
yunlianc244ac02013-03-12 21:36:43 +0000420 if chromeos_version:
Luis Lozano8cf53082013-08-01 12:35:11 -0700421 manifest_patch = GetManifestPatch(manifests, chromeos_version,
yunlianc9a6e772013-03-04 21:38:20 +0000422 chromeos_root)
423 patch.append(manifest_patch)
424 if args.gcc_dir:
Luis Lozano8cf53082013-08-01 12:35:11 -0700425 # 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.
yunlianc9a6e772013-03-04 21:38:20 +0000428 patch.append(UploadGccPatch(chromeos_root, args.gcc_dir, branch))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800429 description = RunRemote(chromeos_root, branch, patch, args.local, target,
430 args.chrome_version, args.dest_dir)
yunlianf4566c92013-02-19 22:34:13 +0000431 if args.local or not args.dest_dir:
Luis Lozano8cf53082013-08-01 12:35:11 -0700432 # TODO: We are not checktng the result of cbuild_bot in here!
yunlianf4566c92013-02-19 22:34:13 +0000433 return 0
Luis Lozano8cf53082013-08-01 12:35:11 -0700434
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
yunlianf4566c92013-02-19 22:34:13 +0000441 os.chdir(script_dir)
442 dest_dir = misc.CanonicalizePath(args.dest_dir)
Luis Lozano8cf53082013-08-01 12:35:11 -0700443 (bot_result, build_id) = FindBuildId(description)
444 if bot_result > 0 and build_id > 0:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800445 logger.GetLogger().LogError('Remote trybot failed but image was generated')
Luis Lozano8cf53082013-08-01 12:35:11 -0700446 bot_result = 1
447 elif bot_result > 0:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800448 logger.GetLogger().LogError('Remote trybot failed. No image was generated')
Luis Lozano8cf53082013-08-01 12:35:11 -0700449 return 2
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800450 if 'toolchain' in branch:
yunlianb7783c02013-03-12 18:31:43 +0000451 chromeos_version = FindVersionForToolchain(branch, chromeos_root)
Luis Lozano8cf53082013-08-01 12:35:11 -0700452 assert not manifest_versions.IsRFormatCrosVersion(chromeos_version)
453 DownloadImage(target, build_id, dest_dir, chromeos_version)
yunlianc9a6e772013-03-04 21:38:20 +0000454 ret = UnpackImage(dest_dir)
Luis Lozano8cf53082013-08-01 12:35:11 -0700455 if ret != 0:
456 return 2
457 # todo: return a more inteligent return value
yunlianc9a6e772013-03-04 21:38:20 +0000458 if not args.replace_sysroot:
Luis Lozano8cf53082013-08-01 12:35:11 -0700459 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
yunlianb5851d32013-02-19 21:36:46 +0000467
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800468
469if __name__ == '__main__':
yunlianb5851d32013-02-19 21:36:46 +0000470 retval = Main(sys.argv)
471 sys.exit(retval)