blob: 6af122c10d39f1cd61257103b9c1726080d8e5a3 [file] [log] [blame]
Evan Hernandezf388cbf2019-04-01 11:15:23 -06001# -*- 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
8from __future__ import print_function
9
10import os
11
12from chromite.cbuildbot import commands
13from chromite.lib import constants
14from chromite.lib import cros_build_lib
15from chromite.lib import osutils
16
17
Evan Hernandez9f125ac2019-04-08 17:18:47 -060018def _GetImageDir(build_root, target):
19 """Return path containing images for the given build target.
20
21 Args:
22 build_root (str): Path to checkout where build occurs.
23 target (str): Name of the build target.
24
25 Returns:
26 Path to the directory containing target images.
27
28 Raises:
29 DieSystemExit: If the image dir does not exist.
30 """
31 image_dir = os.path.join(build_root, 'src/build/images', target, 'latest')
32 if not os.path.exists(image_dir):
33 cros_build_lib.Die('Expected to find image output for target %s at %s, '
34 'but path does not exist' % (target, image_dir))
35 return image_dir
36
37
38def BundleImageZip(input_proto, output_proto):
39 """Bundle image.zip.
40
41 Args:
42 input_proto (BundleRequest): The input proto.
43 output_proto (BundleRequest): The output proto.
44 """
45 target = input_proto.build_target.name
46 output_dir = input_proto.output_dir
47 image_dir = _GetImageDir(constants.SOURCE_ROOT, target)
48 archive = commands.BuildImageZip(output_dir, image_dir)
49 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
50
51
Evan Hernandezf388cbf2019-04-01 11:15:23 -060052def BundleTestUpdatePayloads(input_proto, output_proto):
53 """Generate minimal update payloads for the build target for testing.
54
55 Args:
56 input_proto (BundleRequest): The input proto.
57 output_proto (BundleRequest): The output proto.
58 """
59 target = input_proto.build_target.name
60 output_dir = input_proto.output_dir
61 build_root = constants.SOURCE_ROOT
62
63 # Use the first available image to create the update payload.
Evan Hernandez9f125ac2019-04-08 17:18:47 -060064 img_dir = _GetImageDir(build_root, target)
Evan Hernandezf388cbf2019-04-01 11:15:23 -060065 img_types = [
66 constants.IMAGE_TYPE_TEST, constants.IMAGE_TYPE_DEV,
67 constants.IMAGE_TYPE_BASE
68 ]
69 img_paths = []
70 for img_type in img_types:
Evan Hernandez9f125ac2019-04-08 17:18:47 -060071 img_path = os.path.join(img_dir, constants.IMAGE_TYPE_TO_NAME[img_type])
Evan Hernandezf388cbf2019-04-01 11:15:23 -060072 if os.path.exists(img_path):
73 img_paths.append(img_path)
74
75 if not img_paths:
76 cros_build_lib.Die(
77 'Expected to find an image of type among %r for target "%s" '
Evan Hernandez9f125ac2019-04-08 17:18:47 -060078 'at path %s.', img_types, target, img_dir)
Evan Hernandezf388cbf2019-04-01 11:15:23 -060079 img = img_paths[0]
80
81 # Unfortunately, the relevant commands.py functions do not return
82 # a list of generated files. As a workaround, we have commands.py
83 # put the files in a separate temporary directory so we can catalog them,
84 # then move them to the output dir.
85 # TODO(saklein): Repalce with a chromite/service implementation.
86 with osutils.TempDir() as temp:
87 commands.GeneratePayloads(img, temp, full=True, stateful=True, delta=True)
88 commands.GenerateQuickProvisionPayloads(img, temp)
89 for path in osutils.DirectoryIterator(temp):
90 if os.path.isfile(path):
91 rel_path = os.path.relpath(path, temp)
92 output_proto.artifacts.add().path = os.path.join(output_dir, rel_path)
Evan Hernandezb04e2aa2019-04-08 16:55:54 -060093 osutils.CopyDirContents(temp, output_dir, allow_nonempty=True)
Evan Hernandezf388cbf2019-04-01 11:15:23 -060094
95
96def BundleAutotestFiles(input_proto, output_proto):
97 """Tar the autotest files for a build target.
98
99 Args:
100 input_proto (BundleRequest): The input proto.
101 output_proto (BundleRequest): The output proto.
102 """
103 target = input_proto.build_target.name
104 output_dir = input_proto.output_dir
105 build_root = constants.SOURCE_ROOT
Evan Hernandez36589c62019-04-05 18:14:42 -0600106 cwd = os.path.join(build_root, 'chroot/build', target, 'usr/local/build')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600107
108 # Note that unlike the functions below, this returns the full path
109 # to *multiple* tarballs.
110 # TODO(saklein): Replace with a chromite/service implementation.
111 archives = commands.BuildAutotestTarballsForHWTest(build_root, cwd,
112 output_dir)
113
114 for archive in archives:
115 output_proto.artifacts.add().path = archive
116
117
118def BundleTastFiles(input_proto, output_proto):
119 """Tar the tast files for a build target.
120
121 Args:
122 input_proto (BundleRequest): The input proto.
123 output_proto (BundleRequest): The output proto.
124 """
125 target = input_proto.build_target.name
126 output_dir = input_proto.output_dir
127 build_root = constants.SOURCE_ROOT
Evan Hernandez36589c62019-04-05 18:14:42 -0600128 cwd = os.path.join(build_root, 'chroot/build', target, 'build')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600129
130 # Note that unlike the functions below, this returns the full path
131 # to the tarball.
132 # TODO(saklein): Replace with a chromite/service implementation.
133 archive = commands.BuildTastBundleTarball(build_root, cwd, output_dir)
134
135 output_proto.artifacts.add().path = archive
136
137
138def BundlePinnedGuestImages(input_proto, output_proto):
139 """Tar the pinned guest images for a build target.
140
141 Args:
142 input_proto (BundleRequest): The input proto.
143 output_proto (BundleRequest): The output proto.
144 """
145 target = input_proto.build_target.name
146 output_dir = input_proto.output_dir
147 build_root = constants.SOURCE_ROOT
148
149 # TODO(saklein): Replace with a chromite/service implementation.
150 archive = commands.BuildPinnedGuestImagesTarball(build_root, target,
151 output_dir)
152
153 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
154
155
156def BundleFirmware(input_proto, output_proto):
157 """Tar the firmware images for a build target.
158
159 Args:
160 input_proto (BundleRequest): The input proto.
161 output_proto (BundleRequest): The output proto.
162 """
163 target = input_proto.build_target.name
164 output_dir = input_proto.output_dir
165 build_root = constants.SOURCE_ROOT
166
167 # TODO(saklein): Replace with a chromite/service implementation.
168 archive = commands.BuildFirmwareArchive(build_root, target, output_dir)
169
170 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
171
172
173def BundleEbuildLogs(input_proto, output_proto):
174 """Tar the ebuild logs for a build target.
175
176 Args:
177 input_proto (BundleRequest): The input proto.
178 output_proto (BundleRequest): The output proto.
179 """
180 target = input_proto.build_target.name
181 output_dir = input_proto.output_dir
Evan Hernandeza478d802019-04-08 15:08:24 -0600182
183 # commands.BuildEbuildLogsTarball conflates "buildroot" with sysroot.
184 # Adjust accordingly...
185 build_root = os.path.join(constants.SOURCE_ROOT, 'chroot', 'build')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600186
187 # TODO(saklein): Replace with a chromite/service implementation.
188 archive = commands.BuildEbuildLogsTarball(build_root, target, output_dir)
189
190 output_proto.artifacts.add().path = os.path.join(output_dir, archive)