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 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 18 | def BundleTestUpdatePayloads(input_proto, output_proto): |
| 19 | """Generate minimal update payloads for the build target for testing. |
| 20 | |
| 21 | Args: |
| 22 | input_proto (BundleRequest): The input proto. |
| 23 | output_proto (BundleRequest): The output proto. |
| 24 | """ |
| 25 | target = input_proto.build_target.name |
| 26 | output_dir = input_proto.output_dir |
| 27 | build_root = constants.SOURCE_ROOT |
| 28 | |
| 29 | # Use the first available image to create the update payload. |
| 30 | img_root = os.path.join(build_root, 'src/build/images', target) |
| 31 | img_types = [ |
| 32 | constants.IMAGE_TYPE_TEST, constants.IMAGE_TYPE_DEV, |
| 33 | constants.IMAGE_TYPE_BASE |
| 34 | ] |
| 35 | img_paths = [] |
| 36 | for img_type in img_types: |
| 37 | img_path = os.path.join(img_root, constants.IMAGE_TYPE_TO_NAME[img_type]) |
| 38 | if os.path.exists(img_path): |
| 39 | img_paths.append(img_path) |
| 40 | |
| 41 | if not img_paths: |
| 42 | cros_build_lib.Die( |
| 43 | 'Expected to find an image of type among %r for target "%s" ' |
| 44 | 'at path %s.', img_types, target, img_root) |
| 45 | img = img_paths[0] |
| 46 | |
| 47 | # Unfortunately, the relevant commands.py functions do not return |
| 48 | # a list of generated files. As a workaround, we have commands.py |
| 49 | # put the files in a separate temporary directory so we can catalog them, |
| 50 | # then move them to the output dir. |
| 51 | # TODO(saklein): Repalce with a chromite/service implementation. |
| 52 | with osutils.TempDir() as temp: |
| 53 | commands.GeneratePayloads(img, temp, full=True, stateful=True, delta=True) |
| 54 | commands.GenerateQuickProvisionPayloads(img, temp) |
| 55 | for path in osutils.DirectoryIterator(temp): |
| 56 | if os.path.isfile(path): |
| 57 | rel_path = os.path.relpath(path, temp) |
| 58 | output_proto.artifacts.add().path = os.path.join(output_dir, rel_path) |
| 59 | osutils.CopyDirContents(temp, output_dir) |
| 60 | |
| 61 | |
| 62 | def BundleAutotestFiles(input_proto, output_proto): |
| 63 | """Tar the autotest files for a build target. |
| 64 | |
| 65 | Args: |
| 66 | input_proto (BundleRequest): The input proto. |
| 67 | output_proto (BundleRequest): The output proto. |
| 68 | """ |
| 69 | target = input_proto.build_target.name |
| 70 | output_dir = input_proto.output_dir |
| 71 | build_root = constants.SOURCE_ROOT |
Evan Hernandez | 36589c6 | 2019-04-05 18:14:42 -0600 | [diff] [blame] | 72 | cwd = os.path.join(build_root, 'chroot/build', target, 'usr/local/build') |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 73 | |
| 74 | # Note that unlike the functions below, this returns the full path |
| 75 | # to *multiple* tarballs. |
| 76 | # TODO(saklein): Replace with a chromite/service implementation. |
| 77 | archives = commands.BuildAutotestTarballsForHWTest(build_root, cwd, |
| 78 | output_dir) |
| 79 | |
| 80 | for archive in archives: |
| 81 | output_proto.artifacts.add().path = archive |
| 82 | |
| 83 | |
| 84 | def BundleTastFiles(input_proto, output_proto): |
| 85 | """Tar the tast files for a build target. |
| 86 | |
| 87 | Args: |
| 88 | input_proto (BundleRequest): The input proto. |
| 89 | output_proto (BundleRequest): The output proto. |
| 90 | """ |
| 91 | target = input_proto.build_target.name |
| 92 | output_dir = input_proto.output_dir |
| 93 | build_root = constants.SOURCE_ROOT |
Evan Hernandez | 36589c6 | 2019-04-05 18:14:42 -0600 | [diff] [blame] | 94 | cwd = os.path.join(build_root, 'chroot/build', target, 'build') |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 95 | |
| 96 | # Note that unlike the functions below, this returns the full path |
| 97 | # to the tarball. |
| 98 | # TODO(saklein): Replace with a chromite/service implementation. |
| 99 | archive = commands.BuildTastBundleTarball(build_root, cwd, output_dir) |
| 100 | |
| 101 | output_proto.artifacts.add().path = archive |
| 102 | |
| 103 | |
| 104 | def BundlePinnedGuestImages(input_proto, output_proto): |
| 105 | """Tar the pinned guest images for a build target. |
| 106 | |
| 107 | Args: |
| 108 | input_proto (BundleRequest): The input proto. |
| 109 | output_proto (BundleRequest): The output proto. |
| 110 | """ |
| 111 | target = input_proto.build_target.name |
| 112 | output_dir = input_proto.output_dir |
| 113 | build_root = constants.SOURCE_ROOT |
| 114 | |
| 115 | # TODO(saklein): Replace with a chromite/service implementation. |
| 116 | archive = commands.BuildPinnedGuestImagesTarball(build_root, target, |
| 117 | output_dir) |
| 118 | |
| 119 | output_proto.artifacts.add().path = os.path.join(output_dir, archive) |
| 120 | |
| 121 | |
| 122 | def BundleFirmware(input_proto, output_proto): |
| 123 | """Tar the firmware images for a build target. |
| 124 | |
| 125 | Args: |
| 126 | input_proto (BundleRequest): The input proto. |
| 127 | output_proto (BundleRequest): The output proto. |
| 128 | """ |
| 129 | target = input_proto.build_target.name |
| 130 | output_dir = input_proto.output_dir |
| 131 | build_root = constants.SOURCE_ROOT |
| 132 | |
| 133 | # TODO(saklein): Replace with a chromite/service implementation. |
| 134 | archive = commands.BuildFirmwareArchive(build_root, target, output_dir) |
| 135 | |
| 136 | output_proto.artifacts.add().path = os.path.join(output_dir, archive) |
| 137 | |
| 138 | |
| 139 | def BundleEbuildLogs(input_proto, output_proto): |
| 140 | """Tar the ebuild logs for a build target. |
| 141 | |
| 142 | Args: |
| 143 | input_proto (BundleRequest): The input proto. |
| 144 | output_proto (BundleRequest): The output proto. |
| 145 | """ |
| 146 | target = input_proto.build_target.name |
| 147 | output_dir = input_proto.output_dir |
| 148 | build_root = constants.SOURCE_ROOT |
| 149 | |
| 150 | # TODO(saklein): Replace with a chromite/service implementation. |
| 151 | archive = commands.BuildEbuildLogsTarball(build_root, target, output_dir) |
| 152 | |
| 153 | output_proto.artifacts.add().path = os.path.join(output_dir, archive) |