blob: 807ace378c4c892939b527bc7b52e00af80097b5 [file] [log] [blame]
yunlianb5851d32013-02-19 21:36:46 +00001#!/usr/bin/python
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
8import argparse
yunlianf4566c92013-02-19 22:34:13 +00009import glob
yunlianb5851d32013-02-19 21:36:46 +000010import os
yunlianf4566c92013-02-19 22:34:13 +000011import re
yunlianc9a6e772013-03-04 21:38:20 +000012import shutil
yunlianf4566c92013-02-19 22:34:13 +000013import socket
yunlianb5851d32013-02-19 21:36:46 +000014import sys
yunlianc9a6e772013-03-04 21:38:20 +000015import tempfile
yunlianf4566c92013-02-19 22:34:13 +000016import time
yunlianb5851d32013-02-19 21:36:46 +000017
18from utils import command_executer
yunlianf4566c92013-02-19 22:34:13 +000019from utils import logger
Luis Lozano8cf53082013-08-01 12:35:11 -070020from utils import manifest_versions
yunlianb5851d32013-02-19 21:36:46 +000021from utils import misc
22
Luis Lozanof2a3ef42015-12-15 13:49:30 -080023BRANCH = 'the_actual_branch_used_in_this_script'
24TMP_BRANCH = 'tmp_branch'
yunlianc9a6e772013-03-04 21:38:20 +000025SLEEP_TIME = 600
yunlianf4566c92013-02-19 22:34:13 +000026
27
28def GetPatchNum(output):
29 lines = output.splitlines()
Luis Lozanof2a3ef42015-12-15 13:49:30 -080030 line = [l for l in lines if 'googlesource' in l][0]
31 patch_num = re.findall(r'\d+', line)[0]
32 if 'chrome-internal' in line:
33 patch_num = '*' + patch_num
yunlianc9a6e772013-03-04 21:38:20 +000034 return str(patch_num)
yunlianf4566c92013-02-19 22:34:13 +000035
36
yunlianc9a6e772013-03-04 21:38:20 +000037def GetPatchString(patch):
38 if patch:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080039 return '+'.join(patch)
40 return 'NO_PATCH'
yunlianc9a6e772013-03-04 21:38:20 +000041
42
yunlianb7783c02013-03-12 18:31:43 +000043def FindVersionForToolchain(branch, chromeos_root):
44 """Find the version number in artifacts link in the tryserver email."""
45 # For example: input: toolchain-3701.42.B
yunlianc244ac02013-03-12 21:36:43 +000046 # output: R26-3701.42.1
Luis Lozanof2a3ef42015-12-15 13:49:30 -080047 digits = branch.split('-')[1].split('B')[0]
48 manifest_dir = os.path.join(chromeos_root, 'manifest-internal')
yunlianb7783c02013-03-12 18:31:43 +000049 os.chdir(manifest_dir)
Luis Lozanof2a3ef42015-12-15 13:49:30 -080050 major_version = digits.split('.')[0]
yunlianb7783c02013-03-12 18:31:43 +000051 ce = command_executer.GetCommandExecuter()
Luis Lozanof2a3ef42015-12-15 13:49:30 -080052 command = 'repo sync . && git branch -a | grep {0}'.format(major_version)
Luis Lozano036c9232015-12-10 10:47:01 -080053 _, branches, _ = ce.RunCommandWOutput(command, print_to_console=False)
Luis Lozanof2a3ef42015-12-15 13:49:30 -080054 m = re.search(r'(R\d+)', branches)
yunlianb7783c02013-03-12 18:31:43 +000055 if not m:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080056 logger.GetLogger().LogFatal('Cannot find version for branch {0}'
yunlianb7783c02013-03-12 18:31:43 +000057 .format(branch))
Luis Lozanof2a3ef42015-12-15 13:49:30 -080058 version = m.group(0) + '-' + digits + '1'
yunlianb7783c02013-03-12 18:31:43 +000059 return version
60
61
Luis Lozano8cf53082013-08-01 12:35:11 -070062def FindBuildId(description):
yunlianf4566c92013-02-19 22:34:13 +000063 """Find the build id of the build at trybot server."""
yunlianf4566c92013-02-19 22:34:13 +000064 running_time = 0
yunlianc9a6e772013-03-04 21:38:20 +000065 while True:
Luis Lozano8cf53082013-08-01 12:35:11 -070066 (result, number) = FindBuildIdFromLog(description)
67 if result >= 0:
68 return (result, number)
Luis Lozanof2a3ef42015-12-15 13:49:30 -080069 logger.GetLogger().LogOutput('{0} minutes passed.'
yunlianf4566c92013-02-19 22:34:13 +000070 .format(running_time / 60))
Luis Lozanof2a3ef42015-12-15 13:49:30 -080071 logger.GetLogger().LogOutput('Sleeping {0} seconds.'.format(SLEEP_TIME))
yunlianc9a6e772013-03-04 21:38:20 +000072 time.sleep(SLEEP_TIME)
73 running_time += SLEEP_TIME
yunlianf4566c92013-02-19 22:34:13 +000074
75
Luis Lozano8cf53082013-08-01 12:35:11 -070076def FindBuildIdFromLog(description):
77 """Get the build id from build log."""
78 # returns tuple (result, buildid)
79 # result == 0, buildid > 0, the build was successful and we have a build id
80 # result > 0, buildid > 0, the whole build failed for some reason but we
81 # do have a build id.
82 # result == -1, buildid == -1, we have not found a finished build for this
83 # description yet
yunlian1d2e8e12013-03-11 23:27:39 +000084
yunlian33fef352013-02-19 22:34:52 +000085 file_dir = os.path.dirname(os.path.realpath(__file__))
Luis Lozanof2a3ef42015-12-15 13:49:30 -080086 commands = ('{0}/utils/buildbot_json.py builds '
87 'http://chromegw/p/tryserver.chromiumos/'.format(file_dir))
yunlian33fef352013-02-19 22:34:52 +000088 ce = command_executer.GetCommandExecuter()
Luis Lozano036c9232015-12-10 10:47:01 -080089 _, buildinfo, _ = ce.RunCommandWOutput(commands, print_to_console=False)
yunlian33fef352013-02-19 22:34:52 +000090
yunlianf4566c92013-02-19 22:34:13 +000091 my_info = buildinfo.splitlines()
92 current_line = 1
yunlian1d2e8e12013-03-11 23:27:39 +000093 running_job = False
Luis Lozano8cf53082013-08-01 12:35:11 -070094 result = -1
95
96 # result == 0, we have a successful build
97 # result > 0, we have a failed build but build id may be valid
98 # result == -1, we have not found a finished build for this description
yunlianf4566c92013-02-19 22:34:13 +000099 while current_line < len(my_info):
100 my_dict = {}
101 while True:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800102 key = my_info[current_line].split(':')[0].strip()
103 value = my_info[current_line].split(':', 1)[1].strip()
yunlianf4566c92013-02-19 22:34:13 +0000104 my_dict[key] = value
105 current_line += 1
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800106 if 'Build' in key or current_line == len(my_info):
yunlianf4566c92013-02-19 22:34:13 +0000107 break
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800108 if ('True' not in my_dict['completed'] and
109 str(description) in my_dict['reason']):
yunlian1d2e8e12013-03-11 23:27:39 +0000110 running_job = True
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800111 if ('True' not in my_dict['completed'] or
112 str(description) not in my_dict['reason']):
yunlianf4566c92013-02-19 22:34:13 +0000113 continue
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800114 result = int(my_dict['result'])
115 build_id = int(my_dict['number'])
Luis Lozano8cf53082013-08-01 12:35:11 -0700116 if result == 0:
117 return (result, build_id)
Luis Lozanocd968c82013-08-19 10:42:10 -0700118 else:
Luis Lozano8cf53082013-08-01 12:35:11 -0700119 # Found a finished failed build.
120 # Keep searching to find a successful one
121 pass
122
123 if result > 0 and not running_job:
124 return (result, build_id)
125 return (-1, -1)
yunlianf4566c92013-02-19 22:34:13 +0000126
127
yunlianc9a6e772013-03-04 21:38:20 +0000128def DownloadImage(target, index, dest, version):
129 """Download artifacts from cloud."""
yunlianf4566c92013-02-19 22:34:13 +0000130 if not os.path.exists(dest):
131 os.makedirs(dest)
132
Luis Lozano8cf53082013-08-01 12:35:11 -0700133 rversion = manifest_versions.RFormatCrosVersion(version)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800134 # ls_cmd = ("gsutil ls gs://chromeos-image-archive/trybot-{0}/{1}-b{2}"
135 # .format(target, rversion, index))
136 ls_cmd = ('gsutil ls gs://chromeos-image-archive/trybot-{0}/*-b{2}'
Luis Lozano8cf53082013-08-01 12:35:11 -0700137 .format(target, rversion, index))
yunlianf4566c92013-02-19 22:34:13 +0000138
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800139 download_cmd = ('$(which gsutil) cp {0} {1}'.format('{0}', dest))
yunlianf4566c92013-02-19 22:34:13 +0000140 ce = command_executer.GetCommandExecuter()
141
Luis Lozano036c9232015-12-10 10:47:01 -0800142 _, out, _ = ce.RunCommandWOutput(ls_cmd, print_to_console=True)
yunlianf4566c92013-02-19 22:34:13 +0000143 lines = out.splitlines()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800144 download_files = ['autotest.tar', 'chromeos-chrome', 'chromiumos_test_image',
145 'debug.tgz', 'sysroot_chromeos-base_chromeos-chrome.tar.xz']
yunlianf4566c92013-02-19 22:34:13 +0000146 for line in lines:
147 if any([e in line for e in download_files]):
148 cmd = download_cmd.format(line)
149 if ce.RunCommand(cmd):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800150 logger.GetLogger().LogFatal('Command {0} failed, existing...'
yunlian33fef352013-02-19 22:34:52 +0000151 .format(cmd))
yunlianf4566c92013-02-19 22:34:13 +0000152
153
154def UnpackImage(dest):
155 """Unpack the image, the chroot build dir."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800156 chrome_tbz2 = glob.glob(dest + '/*.tbz2')[0]
157 commands = ('tar xJf {0}/sysroot_chromeos-base_chromeos-chrome.tar.xz '
158 '-C {0} &&'
159 'tar xjf {1} -C {0} &&'
160 'tar xzf {0}/debug.tgz -C {0}/usr/lib/ &&'
161 'tar xf {0}/autotest.tar -C {0}/usr/local/ &&'
162 'tar xJf {0}/chromiumos_test_image.tar.xz -C {0}'
yunlianf4566c92013-02-19 22:34:13 +0000163 .format(dest, chrome_tbz2))
164 ce = command_executer.GetCommandExecuter()
165 return ce.RunCommand(commands)
yunlianb5851d32013-02-19 21:36:46 +0000166
167
yunlianc9a6e772013-03-04 21:38:20 +0000168def RemoveOldBranch():
169 """Remove the branch with name BRANCH."""
170 ce = command_executer.GetCommandExecuter()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800171 command = 'git rev-parse --abbrev-ref HEAD'
Luis Lozano036c9232015-12-10 10:47:01 -0800172 _, out, _ = ce.RunCommandWOutput(command)
yunlianc9a6e772013-03-04 21:38:20 +0000173 if BRANCH in out:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800174 command = 'git checkout -B {0}'.format(TMP_BRANCH)
yunlianc9a6e772013-03-04 21:38:20 +0000175 ce.RunCommand(command)
176 command = "git commit -m 'nouse'"
177 ce.RunCommand(command)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800178 command = 'git branch -D {0}'.format(BRANCH)
yunlianc9a6e772013-03-04 21:38:20 +0000179 ce.RunCommand(command)
yunlianb5851d32013-02-19 21:36:46 +0000180
yunlianb5851d32013-02-19 21:36:46 +0000181
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800182def UploadManifest(manifest, chromeos_root, branch='master'):
yunlianc9a6e772013-03-04 21:38:20 +0000183 """Copy the manifest to $chromeos_root/manifest-internal and upload."""
184 chromeos_root = misc.CanonicalizePath(chromeos_root)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800185 manifest_dir = os.path.join(chromeos_root, 'manifest-internal')
yunlianc9a6e772013-03-04 21:38:20 +0000186 os.chdir(manifest_dir)
187 ce = command_executer.GetCommandExecuter()
yunlian33fef352013-02-19 22:34:52 +0000188
yunlianc9a6e772013-03-04 21:38:20 +0000189 RemoveOldBranch()
yunlianb5851d32013-02-19 21:36:46 +0000190
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800191 if branch != 'master':
192 branch = '{0}'.format(branch)
193 command = 'git checkout -b {0} -t cros-internal/{1}'.format(BRANCH, branch)
yunlianc9a6e772013-03-04 21:38:20 +0000194 ret = ce.RunCommand(command)
195 if ret:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800196 raise Exception('Command {0} failed'.format(command))
yunlianb5851d32013-02-19 21:36:46 +0000197
yunlianc9a6e772013-03-04 21:38:20 +0000198 # We remove the default.xml, which is the symbolic link of full.xml.
199 # After that, we copy our xml file to default.xml.
200 # We did this because the full.xml might be updated during the
201 # run of the script.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800202 os.remove(os.path.join(manifest_dir, 'default.xml'))
203 shutil.copyfile(manifest, os.path.join(manifest_dir, 'default.xml'))
yunlianc9a6e772013-03-04 21:38:20 +0000204 return UploadPatch(manifest)
yunlianb5851d32013-02-19 21:36:46 +0000205
yunlianb5851d32013-02-19 21:36:46 +0000206
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800207def GetManifestPatch(manifests, version, chromeos_root, branch='master'):
yunlianc9a6e772013-03-04 21:38:20 +0000208 """Return a gerrit patch number given a version of manifest file."""
209 temp_dir = tempfile.mkdtemp()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800210 to_file = os.path.join(temp_dir, 'default.xml')
Luis Lozano8cf53082013-08-01 12:35:11 -0700211 manifests.GetManifest(version, to_file)
yunlianc9a6e772013-03-04 21:38:20 +0000212 return UploadManifest(to_file, chromeos_root, branch)
yunlianb5851d32013-02-19 21:36:46 +0000213
yunlianf4566c92013-02-19 22:34:13 +0000214
yunlianc9a6e772013-03-04 21:38:20 +0000215def UploadPatch(source):
216 """Up load patch to gerrit, return patch number."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800217 commands = ('git add -A . &&'
yunlianc9a6e772013-03-04 21:38:20 +0000218 "git commit -m 'test' -m 'BUG=None' -m 'TEST=None' "
219 "-m 'hostname={0}' -m 'source={1}'"
220 .format(socket.gethostname(), source))
221 ce = command_executer.GetCommandExecuter()
222 ce.RunCommand(commands)
yunlian33fef352013-02-19 22:34:52 +0000223
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800224 commands = ('yes | repo upload . --cbr --no-verify')
Luis Lozano036c9232015-12-10 10:47:01 -0800225 _, _, err = ce.RunCommandWOutput(commands)
yunlianc9a6e772013-03-04 21:38:20 +0000226 return GetPatchNum(err)
yunlian952441d2013-02-19 22:34:53 +0000227
yunlian952441d2013-02-19 22:34:53 +0000228
yunlianb7783c02013-03-12 18:31:43 +0000229def ReplaceSysroot(chromeos_root, dest_dir, target):
yunlianc9a6e772013-03-04 21:38:20 +0000230 """Copy unpacked sysroot and image to chromeos_root."""
231 ce = command_executer.GetCommandExecuter()
Luis Lozano9d1db652013-03-25 11:28:57 -0700232 # get the board name from "board-release". board may contain "-"
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800233 board = target.rsplit('-', 1)[0]
234 board_dir = os.path.join(chromeos_root, 'chroot', 'build', board)
235 command = 'sudo rm -rf {0}'.format(board_dir)
yunlianc9a6e772013-03-04 21:38:20 +0000236 ce.RunCommand(command)
237
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800238 command = 'sudo mv {0} {1}'.format(dest_dir, board_dir)
yunlianc9a6e772013-03-04 21:38:20 +0000239 ce.RunCommand(command)
240
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800241 image_dir = os.path.join(chromeos_root, 'src', 'build', 'images', board,
242 'latest')
243 command = 'rm -rf {0} && mkdir -p {0}'.format(image_dir)
yunlianc9a6e772013-03-04 21:38:20 +0000244 ce.RunCommand(command)
245
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800246 command = 'mv {0}/chromiumos_test_image.bin {1}'.format(board_dir, image_dir)
yunlianc9a6e772013-03-04 21:38:20 +0000247 return ce.RunCommand(command)
248
249
250def GccBranchForToolchain(branch):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800251 if branch == 'toolchain-3428.65.B':
252 return 'release-R25-3428.B'
yunlianc9a6e772013-03-04 21:38:20 +0000253 else:
254 return None
255
256
257def GetGccBranch(branch):
258 """Get the remote branch name from branch or version."""
259 ce = command_executer.GetCommandExecuter()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800260 command = 'git branch -a | grep {0}'.format(branch)
Luis Lozano036c9232015-12-10 10:47:01 -0800261 _, out, _ = ce.RunCommandWOutput(command)
yunlianc9a6e772013-03-04 21:38:20 +0000262 if not out:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800263 release_num = re.match(r'.*(R\d+)-*', branch)
yunlianc9a6e772013-03-04 21:38:20 +0000264 if release_num:
265 release_num = release_num.group(0)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800266 command = 'git branch -a | grep {0}'.format(release_num)
Luis Lozano036c9232015-12-10 10:47:01 -0800267 _, out, _ = ce.RunCommandWOutput(command)
yunlianc9a6e772013-03-04 21:38:20 +0000268 if not out:
269 GccBranchForToolchain(branch)
270 if not out:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800271 out = 'remotes/cros/master'
yunlianc9a6e772013-03-04 21:38:20 +0000272 new_branch = out.splitlines()[0]
273 return new_branch
274
275
276def UploadGccPatch(chromeos_root, gcc_dir, branch):
277 """Upload local gcc to gerrit and get the CL number."""
278 ce = command_executer.GetCommandExecuter()
279 gcc_dir = misc.CanonicalizePath(gcc_dir)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800280 gcc_path = os.path.join(chromeos_root, 'src/third_party/gcc')
281 assert os.path.isdir(gcc_path), ('{0} is not a valid chromeos root'
yunlianc9a6e772013-03-04 21:38:20 +0000282 .format(chromeos_root))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800283 assert os.path.isdir(gcc_dir), ('{0} is not a valid dir for gcc'
284 'source'.format(gcc_dir))
yunlianc9a6e772013-03-04 21:38:20 +0000285 os.chdir(gcc_path)
286 RemoveOldBranch()
yunlianc9a6e772013-03-04 21:38:20 +0000287 if not branch:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800288 branch = 'master'
yunlianc9a6e772013-03-04 21:38:20 +0000289 branch = GetGccBranch(branch)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800290 command = ('git checkout -b {0} -t {1} && ' 'rm -rf *'.format(BRANCH, branch))
yunlianc9a6e772013-03-04 21:38:20 +0000291 ce.RunCommand(command, print_to_console=False)
292
293 command = ("rsync -az --exclude='*.svn' --exclude='*.git'"
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800294 ' {0}/ .'.format(gcc_dir))
yunlianc9a6e772013-03-04 21:38:20 +0000295 ce.RunCommand(command)
296 return UploadPatch(gcc_dir)
297
298
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800299def RunRemote(chromeos_root, branch, patches, is_local, target, chrome_version,
300 dest_dir):
yunlianc9a6e772013-03-04 21:38:20 +0000301 """The actual running commands."""
302 ce = command_executer.GetCommandExecuter()
303
304 if is_local:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800305 local_flag = '--local -r {0}'.format(dest_dir)
yunlianc9a6e772013-03-04 21:38:20 +0000306 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800307 local_flag = '--remote'
308 patch = ''
yunlianc9a6e772013-03-04 21:38:20 +0000309 for p in patches:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800310 patch += ' -g {0}'.format(p)
311 cbuildbot_path = os.path.join(chromeos_root, 'chromite/cbuildbot')
yunlianc9a6e772013-03-04 21:38:20 +0000312 os.chdir(cbuildbot_path)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800313 branch_flag = ''
314 if branch != 'master':
315 branch_flag = ' -b {0}'.format(branch)
316 chrome_version_flag = ''
yunlianc9a6e772013-03-04 21:38:20 +0000317 if chrome_version:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800318 chrome_version_flag = ' --chrome_version={0}'.format(chrome_version)
319 description = '{0}_{1}_{2}'.format(branch, GetPatchString(patches), target)
320 command = ('yes | ./cbuildbot {0} {1} {2} {3} {4} {5}'
321 ' --remote-description={6}'
322 ' --chrome_rev=tot'.format(patch, branch_flag, chrome_version,
323 local_flag, chrome_version_flag, target,
324 description))
yunlianc9a6e772013-03-04 21:38:20 +0000325 ce.RunCommand(command)
Luis Lozano8cf53082013-08-01 12:35:11 -0700326
yunlianc9a6e772013-03-04 21:38:20 +0000327 return description
yunlianb5851d32013-02-19 21:36:46 +0000328
329
330def Main(argv):
331 """The main function."""
332 # Common initializations
333 parser = argparse.ArgumentParser()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800334 parser.add_argument('-c',
335 '--chromeos_root',
336 required=True,
337 dest='chromeos_root',
338 help='The chromeos_root')
339 parser.add_argument('-g',
340 '--gcc_dir',
341 default='',
342 dest='gcc_dir',
343 help='The gcc dir')
344 parser.add_argument('-t',
345 '--target',
346 required=True,
347 dest='target',
348 help=('The target to be build, the list is at'
349 ' $(chromeos_root)/chromite/buildbot/cbuildbot'
350 ' --list -all'))
351 parser.add_argument('-l', '--local', action='store_true')
352 parser.add_argument('-d',
353 '--dest_dir',
354 dest='dest_dir',
355 help=('The dir to build the whole chromeos if'
356 ' --local is set'))
357 parser.add_argument('--chrome_version',
358 dest='chrome_version',
359 default='',
360 help='The chrome version to use. '
361 'Default it will use the latest one.')
362 parser.add_argument('--chromeos_version',
363 dest='chromeos_version',
364 default='',
365 help=('The chromeos version to use.'
366 '(1) A release version in the format: '
367 "'\d+\.\d+\.\d+\.\d+.*'"
Luis Lozano8cf53082013-08-01 12:35:11 -0700368 "(2) 'latest_lkgm' for the latest lkgm version"))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800369 parser.add_argument('-r',
370 '--replace_sysroot',
371 action='store_true',
372 help=('Whether or not to replace the build/$board dir'
373 'under the chroot of chromeos_root and copy '
374 'the image to src/build/image/$board/latest.'
375 ' Default is False'))
376 parser.add_argument('-b',
377 '--branch',
378 dest='branch',
379 default='',
380 help=('The branch to run trybot, default is None'))
381 parser.add_argument('-p',
382 '--patch',
383 dest='patch',
384 default='',
385 help=('The patches to be applied, the patches numbers '
yunlianc9a6e772013-03-04 21:38:20 +0000386 "be seperated by ','"))
yunlianf4566c92013-02-19 22:34:13 +0000387
388 script_dir = os.path.dirname(os.path.realpath(__file__))
yunlianb5851d32013-02-19 21:36:46 +0000389
390 args = parser.parse_args(argv[1:])
yunlianb5851d32013-02-19 21:36:46 +0000391 target = args.target
Yunlian Jiang6845e5f2013-04-10 13:53:37 -0700392 if args.patch:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800393 patch = args.patch.split(',')
Yunlian Jiang6845e5f2013-04-10 13:53:37 -0700394 else:
395 patch = []
yunlianc9a6e772013-03-04 21:38:20 +0000396 chromeos_root = misc.CanonicalizePath(args.chromeos_root)
Yunlian Jiang5b0ad872013-04-03 16:39:27 -0700397 if args.chromeos_version and args.branch:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800398 raise Exception('You can not set chromeos_version and branch at the '
399 'same time.')
Luis Lozano8cf53082013-08-01 12:35:11 -0700400
401 manifests = None
yunlianc9a6e772013-03-04 21:38:20 +0000402 if args.branch:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800403 chromeos_version = ''
Luis Lozano8cf53082013-08-01 12:35:11 -0700404 branch = args.branch
yunlianc9a6e772013-03-04 21:38:20 +0000405 else:
406 chromeos_version = args.chromeos_version
Luis Lozano8cf53082013-08-01 12:35:11 -0700407 manifests = manifest_versions.ManifestVersions()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800408 if chromeos_version == 'latest_lkgm':
Luis Lozano8cf53082013-08-01 12:35:11 -0700409 chromeos_version = manifests.TimeToVersion(time.mktime(time.gmtime()))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800410 logger.GetLogger().LogOutput('found version %s for latest LKGM' %
411 (chromeos_version))
Luis Lozano8cf53082013-08-01 12:35:11 -0700412 # TODO: this script currently does not handle the case where the version
413 # is not in the "master" branch
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800414 branch = 'master'
Luis Lozano8cf53082013-08-01 12:35:11 -0700415
yunlianc244ac02013-03-12 21:36:43 +0000416 if chromeos_version:
Luis Lozano8cf53082013-08-01 12:35:11 -0700417 manifest_patch = GetManifestPatch(manifests, chromeos_version,
yunlianc9a6e772013-03-04 21:38:20 +0000418 chromeos_root)
419 patch.append(manifest_patch)
420 if args.gcc_dir:
Luis Lozano8cf53082013-08-01 12:35:11 -0700421 # TODO: everytime we invoke this script we are getting a different
422 # patch for GCC even if GCC has not changed. The description should
423 # be based on the MD5 of the GCC patch contents.
yunlianc9a6e772013-03-04 21:38:20 +0000424 patch.append(UploadGccPatch(chromeos_root, args.gcc_dir, branch))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800425 description = RunRemote(chromeos_root, branch, patch, args.local, target,
426 args.chrome_version, args.dest_dir)
yunlianf4566c92013-02-19 22:34:13 +0000427 if args.local or not args.dest_dir:
Luis Lozano8cf53082013-08-01 12:35:11 -0700428 # TODO: We are not checktng the result of cbuild_bot in here!
yunlianf4566c92013-02-19 22:34:13 +0000429 return 0
Luis Lozano8cf53082013-08-01 12:35:11 -0700430
431 # return value:
432 # 0 => build bot was successful and image was put where requested
433 # 1 => Build bot FAILED but image was put where requested
434 # 2 => Build bot failed or BUild bot was successful but and image was
435 # not generated or could not be put where expected
436
yunlianf4566c92013-02-19 22:34:13 +0000437 os.chdir(script_dir)
438 dest_dir = misc.CanonicalizePath(args.dest_dir)
Luis Lozano8cf53082013-08-01 12:35:11 -0700439 (bot_result, build_id) = FindBuildId(description)
440 if bot_result > 0 and build_id > 0:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800441 logger.GetLogger().LogError('Remote trybot failed but image was generated')
Luis Lozano8cf53082013-08-01 12:35:11 -0700442 bot_result = 1
443 elif bot_result > 0:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800444 logger.GetLogger().LogError('Remote trybot failed. No image was generated')
Luis Lozano8cf53082013-08-01 12:35:11 -0700445 return 2
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800446 if 'toolchain' in branch:
yunlianb7783c02013-03-12 18:31:43 +0000447 chromeos_version = FindVersionForToolchain(branch, chromeos_root)
Luis Lozano8cf53082013-08-01 12:35:11 -0700448 assert not manifest_versions.IsRFormatCrosVersion(chromeos_version)
449 DownloadImage(target, build_id, dest_dir, chromeos_version)
yunlianc9a6e772013-03-04 21:38:20 +0000450 ret = UnpackImage(dest_dir)
Luis Lozano8cf53082013-08-01 12:35:11 -0700451 if ret != 0:
452 return 2
453 # todo: return a more inteligent return value
yunlianc9a6e772013-03-04 21:38:20 +0000454 if not args.replace_sysroot:
Luis Lozano8cf53082013-08-01 12:35:11 -0700455 return bot_result
456
457 ret = ReplaceSysroot(chromeos_root, args.dest_dir, target)
458 if ret != 0:
459 return 2
460
461 # got an image and we were successful in placing it where requested
462 return bot_result
yunlianb5851d32013-02-19 21:36:46 +0000463
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800464
465if __name__ == '__main__':
yunlianb5851d32013-02-19 21:36:46 +0000466 retval = Main(sys.argv)
467 sys.exit(retval)