aboutsummaryrefslogtreecommitdiff
path: root/remote_gcc_build.py
blob: ad6be7b18526c6e597c54f4a6a2ae72876a0497f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/python

# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Script to use remote try-bot build image with local gcc."""

import argparse
import glob
import os
import re
import shutil
import socket
import sys
import tempfile
import time

from utils import command_executer
from utils import logger
from utils import misc

BRANCH = "the_actual_branch_used_in_this_script"
TMP_BRANCH = "tmp_branch"
SLEEP_TIME = 600


def GetPatchNum(output):
  lines = output.splitlines()
  line = [l for l in lines if "gerrit" in l][0]
  patch_num = re.findall(r"\d+", line)[0]
  if "gerrit-int" in line:
    patch_num = "*" + patch_num
  return str(patch_num)


def GetPatchString(patch):
  if patch:
    return "+".join(patch)
  return "NO_PATCH"


def FindVersionForToolchain(branch, chromeos_root):
  """Find the version number in artifacts link in the tryserver email."""
  # For example: input:  toolchain-3701.42.B
  #              output: R26-3701.42.1
  digits = branch.split("-")[1].split("B")[0]
  manifest_dir = os.path.join(chromeos_root, "manifest-internal")
  os.chdir(manifest_dir)
  major_version = digits.split(".")[0]
  ce = command_executer.GetCommandExecuter()
  command = "repo sync . && git branch -a | grep {0}".format(major_version)
  _, branches, _ = ce.RunCommand(command, return_output=True,
                                 print_to_console=False)
  m = re.search(r"(R\d+)", branches)
  if not m:
    logger.GetLogger().LogFatal("Cannot find version for branch {0}"
                                .format(branch))
  version = m.group(0)+"-"+digits+"1"
  return version


def FindResultIndex(reason):
  """Find the build id of the build at trybot server."""
  running_time = 0
  while True:
    num = GetBuildNumber(reason)
    if num >= 0:
      return num
    logger.GetLogger().LogOutput("{0} minutes passed."
                                 .format(running_time / 60))
    logger.GetLogger().LogOutput("Sleeping {0} seconds.".format(SLEEP_TIME))
    time.sleep(SLEEP_TIME)
    running_time += SLEEP_TIME


def GetBuildNumber(reason):
  """Get the build num from build log."""
  # returns 0 if only failed build found.
  # returns -1 if no finished build found or one build is running.
  # returns build number is successful build found.

  file_dir = os.path.dirname(os.path.realpath(__file__))
  commands = ("{0}/utils/buildbot_json.py builds "
              "http://chromegw/p/tryserver.chromiumos/"
              .format(file_dir))
  ce = command_executer.GetCommandExecuter()
  _, buildinfo, _ = ce.RunCommand(commands, return_output=True,
                                  print_to_console=False)

  my_info = buildinfo.splitlines()
  current_line = 1
  running_job = False
  number = -1
  # number > 0, we have a successful build.
  # number = 0, we have a failed build.
  # number = -1, we do not have a finished build for this description.
  while current_line < len(my_info):
    my_dict = {}
    while True:
      key = my_info[current_line].split(":")[0].strip()
      value = my_info[current_line].split(":", 1)[1].strip()
      my_dict[key] = value
      current_line += 1
      if "Build" in key or current_line == len(my_info):
        break
    if ("True" not in my_dict["completed"] and
        str(reason) in my_dict["reason"]):
      running_job = True
    if ("True" not in my_dict["completed"] or
        str(reason) not in my_dict["reason"]):
      continue
    if my_dict["result"] == "0":
      number = int(my_dict["number"])
      return number
    else:
     # Record found a finished failed build.
     # Keep searching to find a successful one.
      number = 0
  if number == 0 and not running_job:
    return 0
  return -1


def DownloadImage(target, index, dest, version):
  """Download artifacts from cloud."""
  match = re.search(r"(.*-\d+\.\d+\.\d+)", version)
  version = match.group(0)
  if not os.path.exists(dest):
    os.makedirs(dest)

  ls_cmd = ("gsutil ls gs://chromeos-image-archive/trybot-{0}/{1}-b{2}"
            .format(target, version, index))

  download_cmd = ("$(which gsutil) cp {0} {1}".format("{0}", dest))
  ce = command_executer.GetCommandExecuter()

  _, out, _ = ce.RunCommand(ls_cmd, return_output=True, print_to_console=True)
  lines = out.splitlines()
  download_files = ["autotest.tar", "chromeos-chrome",
                    "chromiumos_test_image", "debug.tgz",
                    "sysroot_chromeos-base_chromeos-chrome.tar.xz"
                   ]
  for line in lines:
    if any([e in line for e in download_files]):
      cmd = download_cmd.format(line)
      if ce.RunCommand(cmd):
        logger.GetLogger().LogFatal("Command {0} failed, existing..."
                                    .format(cmd))


def UnpackImage(dest):
  """Unpack the image, the chroot build dir."""
  chrome_tbz2 = glob.glob(dest+"/*.tbz2")[0]
  commands = ("tar xJf {0}/sysroot_chromeos-base_chromeos-chrome.tar.xz "
              "-C {0} &&"
              "tar xjf {1} -C {0} &&"
              "tar xzf {0}/debug.tgz  -C {0}/usr/lib/ &&"
              "tar xf {0}/autotest.tar -C {0}/usr/local/ &&"
              "tar xJf {0}/chromiumos_test_image.tar.xz -C {0}"
              .format(dest, chrome_tbz2))
  ce = command_executer.GetCommandExecuter()
  return ce.RunCommand(commands)


def GetManifest(version, to_file):
  """Get the manifest file from a given chromeos-internal version."""
  temp_dir = tempfile.mkdtemp()
  version = version.split("-", 1)[1]
  os.chdir(temp_dir)
  command = ("git clone "
             "ssh://gerrit-int.chromium.org:29419/"
             "chromeos/manifest-versions.git")
  ce = command_executer.GetCommandExecuter()
  ce.RunCommand(command)
  files = [os.path.join(r, f)
           for r, _, fs in os.walk(".")
           for f in fs if version in f]
  if files:
    command = "cp {0} {1} && rm -rf {2}".format(files[0], to_file, temp_dir)
    ret = ce.RunCommand(command)
    if ret:
      raise Exception("Cannot copy manifest to {0}".format(to_file))
  else:
    command = "rm -rf {0}".format(temp_dir)
    ce.RunCommand(command)
    raise Exception("Version {0} is not available.".format(version))


def RemoveOldBranch():
  """Remove the branch with name BRANCH."""
  ce = command_executer.GetCommandExecuter()
  command = "git rev-parse --abbrev-ref HEAD"
  _, out, _ = ce.RunCommand(command, return_output=True)
  if BRANCH in out:
    command = "git checkout -B {0}".format(TMP_BRANCH)
    ce.RunCommand(command)
  command = "git commit -m 'nouse'"
  ce.RunCommand(command)
  command = "git branch -D {0}".format(BRANCH)
  ce.RunCommand(command)


def UploadManifest(manifest, chromeos_root, branch="master"):
  """Copy the manifest to $chromeos_root/manifest-internal and upload."""
  chromeos_root = misc.CanonicalizePath(chromeos_root)
  manifest_dir = os.path.join(chromeos_root, "manifest-internal")
  os.chdir(manifest_dir)
  ce = command_executer.GetCommandExecuter()

  RemoveOldBranch()

  if branch != "master":
    branch = "{0}".format(branch)
  command = "git checkout -b {0} -t cros-internal/{1}".format(BRANCH, branch)
  ret = ce.RunCommand(command)
  if ret:
    raise Exception("Command {0} failed".format(command))

  # We remove the default.xml, which is the symbolic link of full.xml.
  # After that, we copy our xml file to default.xml.
  # We did this because the full.xml might be updated during the
  # run of the script.
  os.remove(os.path.join(manifest_dir, "default.xml"))
  shutil.copyfile(manifest, os.path.join(manifest_dir, "default.xml"))
  return UploadPatch(manifest)


def GetManifestPatch(version, chromeos_root, branch="master"):
  """Return a gerrit patch number given a version of manifest file."""
  temp_dir = tempfile.mkdtemp()
  to_file = os.path.join(temp_dir, "default.xml")
  GetManifest(version, to_file)
  return UploadManifest(to_file, chromeos_root, branch)


def UploadPatch(source):
  """Up load patch to gerrit, return patch number."""
  commands = ("git add -A . &&"
              "git commit -m 'test' -m 'BUG=None' -m 'TEST=None' "
              "-m 'hostname={0}' -m 'source={1}'"
              .format(socket.gethostname(), source))
  ce = command_executer.GetCommandExecuter()
  ce.RunCommand(commands)

  commands = ("yes | repo upload .   --cbr --no-verify")
  _, _, err = ce.RunCommand(commands, return_output=True)
  return GetPatchNum(err)


def ReplaceSysroot(chromeos_root, dest_dir, target):
  """Copy unpacked sysroot and image to chromeos_root."""
  ce = command_executer.GetCommandExecuter()
  # get the board name from "board-release". board may contain "-"
  board = target.rsplit("-", 1)[0]
  board_dir = os.path.join(chromeos_root, "chroot", "build", board)
  command = "sudo rm -rf {0}".format(board_dir)
  ce.RunCommand(command)

  command = "sudo mv {0} {1}".format(dest_dir, board_dir)
  ce.RunCommand(command)

  image_dir = os.path.join(chromeos_root, "src", "build", "images",
                           board, "latest")
  command = "rm -rf {0} && mkdir -p {0}".format(image_dir)
  ce.RunCommand(command)

  command = "mv {0}/chromiumos_test_image.bin {1}".format(board_dir, image_dir)
  return ce.RunCommand(command)


def GccBranchForToolchain(branch):
  if branch == "toolchain-3428.65.B":
    return "release-R25-3428.B"
  else:
    return None


def GetGccBranch(branch):
  """Get the remote branch name from branch or version."""
  ce = command_executer.GetCommandExecuter()
  command = "git branch -a | grep {0}".format(branch)
  _, out, _ = ce.RunCommand(command, return_output=True)
  if not out:
    release_num = re.match(r".*(R\d+)-*", branch)
    if release_num:
      release_num = release_num.group(0)
      command = "git branch -a | grep {0}".format(release_num)
      _, out, _ = ce.RunCommand(command, return_output=True)
      if not out:
        GccBranchForToolchain(branch)
  if not out:
    out = "remotes/cros/master"
  new_branch = out.splitlines()[0]
  return new_branch


def UploadGccPatch(chromeos_root, gcc_dir, branch):
  """Upload local gcc to gerrit and get the CL number."""
  ce = command_executer.GetCommandExecuter()
  gcc_dir = misc.CanonicalizePath(gcc_dir)
  gcc_path = os.path.join(chromeos_root, "src/third_party/gcc")
  assert os.path.isdir(gcc_path), ("{0} is not a valid chromeos root"
                                   .format(chromeos_root))
  assert os.path.isdir(gcc_dir), ("{0} is not a valid dir for gcc"
                                  "source".format(gcc_dir))
  os.chdir(gcc_path)
  RemoveOldBranch()
  if not branch:
    branch = "master"
  branch = GetGccBranch(branch)
  command = ("git checkout -b {0} -t {1} && "
             "rm -rf *".format(BRANCH, branch))
  ce.RunCommand(command, print_to_console=False)

  command = ("rsync -az --exclude='*.svn' --exclude='*.git'"
             " {0}/ .".format(gcc_dir))
  ce.RunCommand(command)
  return UploadPatch(gcc_dir)


def RunRemote(chromeos_root, branch, patches, is_local,
              target, chrome_version, dest_dir):
  """The actual running commands."""
  ce = command_executer.GetCommandExecuter()

  if is_local:
    local_flag = "--local -r {0}".format(dest_dir)
  else:
    local_flag = "--remote"
  patch = ""
  for p in patches:
    patch += " -g {0}".format(p)
  cbuildbot_path = os.path.join(chromeos_root, "chromite/buildbot")
  os.chdir(cbuildbot_path)
  branch_flag = ""
  if branch != "master":
    branch_flag = " -b {0}".format(branch)
  chrome_version_flag = ""
  if chrome_version:
    chrome_version_flag = " --chrome_version={0}".format(chrome_version)
  description = "{0}_{1}_{2}".format(branch, GetPatchString(patches), target)
  command = ("yes | ./cbuildbot {0} {1} {2} {3} {4} {5}"
             " --remote-description={6}"
             .format(patch, branch_flag, chrome_version, local_flag,
                     chrome_version_flag, target, description))
  ce.RunCommand(command)
  return description


def Main(argv):
  """The main function."""
  # Common initializations
  parser = argparse.ArgumentParser()
  parser.add_argument("-c", "--chromeos_root", required=True,
                      dest="chromeos_root", help="The chromeos_root")
  parser.add_argument("-g", "--gcc_dir", default="", dest="gcc_dir",
                      help="The gcc dir")
  parser.add_argument("-t", "--target", required=True, dest="target",
                      help=("The target to be build, the list is at"
                            " $(chromeos_root)/chromite/buildbot/cbuildbot"
                            " --list -all"))
  parser.add_argument("-l", "--local", action="store_true")
  parser.add_argument("-d", "--dest_dir", dest="dest_dir",
                      help=("The dir to build the whole chromeos if"
                            " --local is set"))
  parser.add_argument("--chrome_version", dest="chrome_version",
                      default="", help="The chrome version to use. "
                      "Default it will use the latest one.")
  parser.add_argument("--chromeos_version", dest="chromeos_version",
                      default="", help="The chromeos version to use.")

  parser.add_argument("-r", "--replace_sysroot", action="store_true",
                      help=("Whether or not to replace the build/$board dir"
                            "under the chroot of chromeos_root and copy "
                            "the image to src/build/image/$board/latest."
                            " Default is False"))
  parser.add_argument("-b", "--branch", dest="branch", default="",
                      help=("The branch to run trybot, default is None"))
  parser.add_argument("-p", "--patch", dest="patch", default="",
                      help=("The patches to be applied, the patches numbers "
                            "be seperated by ','"))

  script_dir = os.path.dirname(os.path.realpath(__file__))

  args = parser.parse_args(argv[1:])
  target = args.target
  if args.patch:
    patch = args.patch.split(",")
  else:
    patch = []
  chromeos_root = misc.CanonicalizePath(args.chromeos_root)
  branch = args.branch
  if not branch:
    branch = chromeos_version
  # descritption is the keyword of the build in build log.
  # Here we use [{branch)_{patchnumber}_{target}]
  description = "{0}_{1}_{2}".format(branch, GetPatchString(patch), target)
  if args.chromeos_version and args.branch:
    raise Exception("You can not set chromeos_version and branch at the "
                    "same time.")
  chromeos_version = args.chromeos_version
  if args.branch:
    chromeos_version = 0
  else:
    chromeos_version = args.chromeos_version
  if chromeos_version:
    manifest_patch = GetManifestPatch(chromeos_version,
                                      chromeos_root)
    patch.append(manifest_patch)
  if args.gcc_dir:
    patch.append(UploadGccPatch(chromeos_root, args.gcc_dir, branch))
  index = 0
  description = RunRemote(chromeos_root, branch, patch, args.local,
                          target, args.chrome_version, args.dest_dir)
  if args.local or not args.dest_dir:
    return 0
  os.chdir(script_dir)
  dest_dir = misc.CanonicalizePath(args.dest_dir)
  if index <= 0:
    index = FindResultIndex(description)
  if not index:
    logger.GetLogger().LogFatal("Remote trybot failed.")
  if "toolchain" in branch:
    chromeos_version = FindVersionForToolchain(branch, chromeos_root)
  DownloadImage(target, index, dest_dir, chromeos_version)
  ret = UnpackImage(dest_dir)
  if not args.replace_sysroot:
    return ret
  else:
    return ReplaceSysroot(chromeos_root, args.dest_dir, target)

if __name__ == "__main__":
  retval = Main(sys.argv)
  sys.exit(retval)