Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Implements ArtifactService.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import os |
| 11 | |
| 12 | from chromite.cbuildbot import commands |
| 13 | from chromite.lib import constants |
| 14 | from chromite.lib import cros_build_lib |
| 15 | from chromite.lib import osutils |
| 16 | |
| 17 | |
| 18 | def _GetTargetWorkingDirectory(build_root, target): |
| 19 | """Return the working directory for the given build target. |
| 20 | |
| 21 | See commands.py functions for more information on what this means. |
| 22 | |
| 23 | Args: |
| 24 | build_root (str): Root CrOS directory being built. |
| 25 | target (str): Name of the build target in question. |
| 26 | |
| 27 | Returns: |
| 28 | str: Path to the build target's working directory. |
| 29 | """ |
| 30 | return os.path.join(build_root, 'chroot', 'build', target, 'build') |
| 31 | |
| 32 | |
| 33 | def BundleTestUpdatePayloads(input_proto, output_proto): |
| 34 | """Generate minimal update payloads for the build target for testing. |
| 35 | |
| 36 | Args: |
| 37 | input_proto (BundleRequest): The input proto. |
| 38 | output_proto (BundleRequest): The output proto. |
| 39 | """ |
| 40 | target = input_proto.build_target.name |
| 41 | output_dir = input_proto.output_dir |
| 42 | build_root = constants.SOURCE_ROOT |
| 43 | |
| 44 | # Use the first available image to create the update payload. |
| 45 | img_root = os.path.join(build_root, 'src/build/images', target) |
| 46 | img_types = [ |
| 47 | constants.IMAGE_TYPE_TEST, constants.IMAGE_TYPE_DEV, |
| 48 | constants.IMAGE_TYPE_BASE |
| 49 | ] |
| 50 | img_paths = [] |
| 51 | for img_type in img_types: |
| 52 | img_path = os.path.join(img_root, constants.IMAGE_TYPE_TO_NAME[img_type]) |
| 53 | if os.path.exists(img_path): |
| 54 | img_paths.append(img_path) |
| 55 | |
| 56 | if not img_paths: |
| 57 | cros_build_lib.Die( |
| 58 | 'Expected to find an image of type among %r for target "%s" ' |
| 59 | 'at path %s.', img_types, target, img_root) |
| 60 | img = img_paths[0] |
| 61 | |
| 62 | # Unfortunately, the relevant commands.py functions do not return |
| 63 | # a list of generated files. As a workaround, we have commands.py |
| 64 | # put the files in a separate temporary directory so we can catalog them, |
| 65 | # then move them to the output dir. |
| 66 | # TODO(saklein): Repalce with a chromite/service implementation. |
| 67 | with osutils.TempDir() as temp: |
| 68 | commands.GeneratePayloads(img, temp, full=True, stateful=True, delta=True) |
| 69 | commands.GenerateQuickProvisionPayloads(img, temp) |
| 70 | for path in osutils.DirectoryIterator(temp): |
| 71 | if os.path.isfile(path): |
| 72 | rel_path = os.path.relpath(path, temp) |
| 73 | output_proto.artifacts.add().path = os.path.join(output_dir, rel_path) |
| 74 | osutils.CopyDirContents(temp, output_dir) |
| 75 | |
| 76 | |
| 77 | def BundleAutotestFiles(input_proto, output_proto): |
| 78 | """Tar the autotest files for a build target. |
| 79 | |
| 80 | Args: |
| 81 | input_proto (BundleRequest): The input proto. |
| 82 | output_proto (BundleRequest): The output proto. |
| 83 | """ |
| 84 | target = input_proto.build_target.name |
| 85 | output_dir = input_proto.output_dir |
| 86 | build_root = constants.SOURCE_ROOT |
| 87 | cwd = _GetTargetWorkingDirectory(build_root, target) |
| 88 | |
| 89 | # Note that unlike the functions below, this returns the full path |
| 90 | # to *multiple* tarballs. |
| 91 | # TODO(saklein): Replace with a chromite/service implementation. |
| 92 | archives = commands.BuildAutotestTarballsForHWTest(build_root, cwd, |
| 93 | output_dir) |
| 94 | |
| 95 | for archive in archives: |
| 96 | output_proto.artifacts.add().path = archive |
| 97 | |
| 98 | |
| 99 | def BundleTastFiles(input_proto, output_proto): |
| 100 | """Tar the tast files for a build target. |
| 101 | |
| 102 | Args: |
| 103 | input_proto (BundleRequest): The input proto. |
| 104 | output_proto (BundleRequest): The output proto. |
| 105 | """ |
| 106 | target = input_proto.build_target.name |
| 107 | output_dir = input_proto.output_dir |
| 108 | build_root = constants.SOURCE_ROOT |
| 109 | cwd = _GetTargetWorkingDirectory(build_root, target) |
| 110 | |
| 111 | # Note that unlike the functions below, this returns the full path |
| 112 | # to the tarball. |
| 113 | # TODO(saklein): Replace with a chromite/service implementation. |
| 114 | archive = commands.BuildTastBundleTarball(build_root, cwd, output_dir) |
| 115 | |
| 116 | output_proto.artifacts.add().path = archive |
| 117 | |
| 118 | |
| 119 | def BundlePinnedGuestImages(input_proto, output_proto): |
| 120 | """Tar the pinned guest images for a build target. |
| 121 | |
| 122 | Args: |
| 123 | input_proto (BundleRequest): The input proto. |
| 124 | output_proto (BundleRequest): The output proto. |
| 125 | """ |
| 126 | target = input_proto.build_target.name |
| 127 | output_dir = input_proto.output_dir |
| 128 | build_root = constants.SOURCE_ROOT |
| 129 | |
| 130 | # TODO(saklein): Replace with a chromite/service implementation. |
| 131 | archive = commands.BuildPinnedGuestImagesTarball(build_root, target, |
| 132 | output_dir) |
| 133 | |
| 134 | output_proto.artifacts.add().path = os.path.join(output_dir, archive) |
| 135 | |
| 136 | |
| 137 | def BundleFirmware(input_proto, output_proto): |
| 138 | """Tar the firmware images for a build target. |
| 139 | |
| 140 | Args: |
| 141 | input_proto (BundleRequest): The input proto. |
| 142 | output_proto (BundleRequest): The output proto. |
| 143 | """ |
| 144 | target = input_proto.build_target.name |
| 145 | output_dir = input_proto.output_dir |
| 146 | build_root = constants.SOURCE_ROOT |
| 147 | |
| 148 | # TODO(saklein): Replace with a chromite/service implementation. |
| 149 | archive = commands.BuildFirmwareArchive(build_root, target, output_dir) |
| 150 | |
| 151 | output_proto.artifacts.add().path = os.path.join(output_dir, archive) |
| 152 | |
| 153 | |
| 154 | def BundleEbuildLogs(input_proto, output_proto): |
| 155 | """Tar the ebuild logs for a build target. |
| 156 | |
| 157 | Args: |
| 158 | input_proto (BundleRequest): The input proto. |
| 159 | output_proto (BundleRequest): The output proto. |
| 160 | """ |
| 161 | target = input_proto.build_target.name |
| 162 | output_dir = input_proto.output_dir |
| 163 | build_root = constants.SOURCE_ROOT |
| 164 | |
| 165 | # TODO(saklein): Replace with a chromite/service implementation. |
| 166 | archive = commands.BuildEbuildLogsTarball(build_root, target, output_dir) |
| 167 | |
| 168 | output_proto.artifacts.add().path = os.path.join(output_dir, archive) |