blob: 83a6910e6f6613375e2383a4ea6b619178543bab [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
Alex Klein231d2da2019-07-22 16:44:45 -060012from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060013from chromite.api import faux
Alex Klein2b236722019-06-19 15:44:26 -060014from chromite.api import validate
Alex Klein238d8862019-05-07 11:32:46 -060015from chromite.api.controller import controller_util
Tiancong Wang24a3df72019-08-20 15:48:51 -070016from chromite.api.gen.chromite.api import toolchain_pb2
Evan Hernandezf388cbf2019-04-01 11:15:23 -060017from chromite.cbuildbot import commands
Alex Klein2275d692019-04-23 16:04:12 -060018from chromite.lib import build_target_util
19from chromite.lib import chroot_lib
Evan Hernandezf388cbf2019-04-01 11:15:23 -060020from chromite.lib import constants
21from chromite.lib import cros_build_lib
Evan Hernandezde445982019-04-22 13:42:34 -060022from chromite.lib import cros_logging as logging
Alex Klein2275d692019-04-23 16:04:12 -060023from chromite.lib import sysroot_lib
24from chromite.service import artifacts
Evan Hernandezf388cbf2019-04-01 11:15:23 -060025
26
Evan Hernandez9f125ac2019-04-08 17:18:47 -060027def _GetImageDir(build_root, target):
28 """Return path containing images for the given build target.
29
Alex Kleine2612a02019-04-18 13:51:06 -060030 TODO(saklein) Expand image_lib.GetLatestImageLink to support this use case.
31
Evan Hernandez9f125ac2019-04-08 17:18:47 -060032 Args:
33 build_root (str): Path to checkout where build occurs.
34 target (str): Name of the build target.
35
36 Returns:
37 Path to the directory containing target images.
38
39 Raises:
40 DieSystemExit: If the image dir does not exist.
41 """
42 image_dir = os.path.join(build_root, 'src/build/images', target, 'latest')
43 if not os.path.exists(image_dir):
44 cros_build_lib.Die('Expected to find image output for target %s at %s, '
45 'but path does not exist' % (target, image_dir))
46 return image_dir
47
48
Alex Klein076841b2019-08-29 15:19:39 -060049@faux.all_empty
Michael Mortensen01910922019-07-24 14:48:10 -060050@validate.require('build_target.name', 'output_dir')
Alex Klein231d2da2019-07-22 16:44:45 -060051@validate.exists('output_dir')
52@validate.validation_complete
53def BundleImageZip(input_proto, output_proto, _config):
Evan Hernandez9f125ac2019-04-08 17:18:47 -060054 """Bundle image.zip.
55
56 Args:
57 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -060058 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -060059 _config (api_config.ApiConfig): The API call config.
Evan Hernandez9f125ac2019-04-08 17:18:47 -060060 """
61 target = input_proto.build_target.name
62 output_dir = input_proto.output_dir
63 image_dir = _GetImageDir(constants.SOURCE_ROOT, target)
Alex Klein231d2da2019-07-22 16:44:45 -060064
Michael Mortensen01910922019-07-24 14:48:10 -060065 archive = artifacts.BundleImageZip(output_dir, image_dir)
Evan Hernandez9f125ac2019-04-08 17:18:47 -060066 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
67
68
Alex Klein076841b2019-08-29 15:19:39 -060069@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -060070@validate.require('build_target.name', 'output_dir')
71@validate.exists('output_dir')
72@validate.validation_complete
73def BundleTestUpdatePayloads(input_proto, output_proto, _config):
Evan Hernandezf388cbf2019-04-01 11:15:23 -060074 """Generate minimal update payloads for the build target for testing.
75
76 Args:
77 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -060078 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -060079 _config (api_config.ApiConfig): The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -060080 """
81 target = input_proto.build_target.name
82 output_dir = input_proto.output_dir
83 build_root = constants.SOURCE_ROOT
84
85 # Use the first available image to create the update payload.
Evan Hernandez9f125ac2019-04-08 17:18:47 -060086 img_dir = _GetImageDir(build_root, target)
Alex Kleincb541e82019-06-26 15:06:11 -060087 img_types = [constants.IMAGE_TYPE_TEST, constants.IMAGE_TYPE_DEV,
88 constants.IMAGE_TYPE_BASE]
89 img_names = [constants.IMAGE_TYPE_TO_NAME[t] for t in img_types]
Mike Frysinger66ce4132019-07-17 22:52:52 -040090 img_paths = [os.path.join(img_dir, x) for x in img_names]
Mike Frysingera552be42018-08-17 14:39:32 -040091 valid_images = [x for x in img_paths if os.path.exists(x)]
Evan Hernandezf388cbf2019-04-01 11:15:23 -060092
Alex Kleincb541e82019-06-26 15:06:11 -060093 if not valid_images:
Evan Hernandezf388cbf2019-04-01 11:15:23 -060094 cros_build_lib.Die(
95 'Expected to find an image of type among %r for target "%s" '
Evan Hernandez9f125ac2019-04-08 17:18:47 -060096 'at path %s.', img_types, target, img_dir)
Alex Kleincb541e82019-06-26 15:06:11 -060097 image = valid_images[0]
Evan Hernandezf388cbf2019-04-01 11:15:23 -060098
Alex Kleincb541e82019-06-26 15:06:11 -060099 payloads = artifacts.BundleTestUpdatePayloads(image, output_dir)
100 for payload in payloads:
101 output_proto.artifacts.add().path = payload
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600102
103
Alex Klein076841b2019-08-29 15:19:39 -0600104@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600105@validate.require('output_dir')
106@validate.exists('output_dir')
107def BundleAutotestFiles(input_proto, output_proto, config):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600108 """Tar the autotest files for a build target.
109
110 Args:
111 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600112 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600113 config (api_config.ApiConfig): The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600114 """
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600115 output_dir = input_proto.output_dir
Alex Klein238d8862019-05-07 11:32:46 -0600116 target = input_proto.build_target.name
Alex Kleine21a0952019-08-23 16:08:16 -0600117 chroot = controller_util.ParseChroot(input_proto.chroot)
118
Alex Klein238d8862019-05-07 11:32:46 -0600119 if target:
Alex Kleine21a0952019-08-23 16:08:16 -0600120 sysroot_path = os.path.join('/build', target)
Alex Klein238d8862019-05-07 11:32:46 -0600121 else:
122 # New style call, use chroot and sysroot.
Alex Klein238d8862019-05-07 11:32:46 -0600123 sysroot_path = input_proto.sysroot.path
124 if not sysroot_path:
125 cros_build_lib.Die('sysroot.path is required.')
126
Alex Kleine21a0952019-08-23 16:08:16 -0600127 sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein238d8862019-05-07 11:32:46 -0600128
Alex Klein231d2da2019-07-22 16:44:45 -0600129 # TODO(saklein): Switch to the validate_only decorator when legacy handling
130 # is removed.
131 if config.validate_only:
132 return controller.RETURN_CODE_VALID_INPUT
133
Alex Kleine21a0952019-08-23 16:08:16 -0600134 if not sysroot.Exists(chroot=chroot):
Alex Klein238d8862019-05-07 11:32:46 -0600135 cros_build_lib.Die('Sysroot path must exist: %s', sysroot.path)
136
137 try:
138 # Note that this returns the full path to *multiple* tarballs.
Alex Kleine21a0952019-08-23 16:08:16 -0600139 archives = artifacts.BundleAutotestFiles(chroot, sysroot, output_dir)
Alex Klein238d8862019-05-07 11:32:46 -0600140 except artifacts.Error as e:
141 cros_build_lib.Die(e.message)
142
143 for archive in archives.values():
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600144 output_proto.artifacts.add().path = archive
145
146
Alex Klein076841b2019-08-29 15:19:39 -0600147@faux.all_empty
Alex Kleinb9d810b2019-07-01 12:38:02 -0600148@validate.require('output_dir')
Alex Klein231d2da2019-07-22 16:44:45 -0600149@validate.exists('output_dir')
150def BundleTastFiles(input_proto, output_proto, config):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600151 """Tar the tast files for a build target.
152
153 Args:
154 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600155 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600156 config (api_config.ApiConfig): The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600157 """
158 target = input_proto.build_target.name
159 output_dir = input_proto.output_dir
160 build_root = constants.SOURCE_ROOT
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600161
Alex Kleinb9d810b2019-07-01 12:38:02 -0600162 chroot = controller_util.ParseChroot(input_proto.chroot)
163 sysroot_path = input_proto.sysroot.path
164
165 # TODO(saklein) Cleanup legacy handling after it has been switched over.
166 if target:
167 # Legacy handling.
Alex Klein171da612019-08-06 14:00:42 -0600168 chroot = chroot_lib.Chroot(path=os.path.join(build_root, 'chroot'))
Alex Kleinb9d810b2019-07-01 12:38:02 -0600169 sysroot_path = os.path.join('/build', target)
170
171 # New handling - chroot & sysroot based.
172 # TODO(saklein) Switch this to the require decorator when legacy is removed.
173 if not sysroot_path:
174 cros_build_lib.Die('sysroot.path is required.')
175
Alex Klein231d2da2019-07-22 16:44:45 -0600176 # TODO(saklein): Switch to the validation_complete decorator when legacy
177 # handling is removed.
178 if config.validate_only:
179 return controller.RETURN_CODE_VALID_INPUT
180
Alex Kleinb9d810b2019-07-01 12:38:02 -0600181 sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein231d2da2019-07-22 16:44:45 -0600182 if not sysroot.Exists(chroot=chroot):
Alex Kleinb9d810b2019-07-01 12:38:02 -0600183 cros_build_lib.Die('Sysroot must exist.')
184
185 archive = artifacts.BundleTastFiles(chroot, sysroot, output_dir)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600186
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600187 if archive is None:
188 cros_build_lib.Die(
189 'Could not bundle Tast files. '
190 'No Tast directories found for %s.', target)
191
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600192 output_proto.artifacts.add().path = archive
193
194
Alex Klein076841b2019-08-29 15:19:39 -0600195@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600196@validate.require('build_target.name', 'output_dir')
197@validate.exists('output_dir')
198@validate.validation_complete
199def BundlePinnedGuestImages(input_proto, output_proto, _config):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600200 """Tar the pinned guest images for a build target.
201
202 Args:
203 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600204 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600205 _config (api_config.ApiConfig): The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600206 """
207 target = input_proto.build_target.name
208 output_dir = input_proto.output_dir
209 build_root = constants.SOURCE_ROOT
210
Alex Kleine2612a02019-04-18 13:51:06 -0600211 # TODO(crbug.com/954299): Replace with a chromite/service implementation.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600212 archive = commands.BuildPinnedGuestImagesTarball(build_root, target,
213 output_dir)
214
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600215 if archive is None:
Evan Hernandezde445982019-04-22 13:42:34 -0600216 logging.warning('Found no pinned guest images for %s.', target)
217 return
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600218
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600219 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
220
221
Alex Klein076841b2019-08-29 15:19:39 -0600222@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600223@validate.require('sysroot.path')
224@validate.validation_complete
225def FetchPinnedGuestImages(input_proto, output_proto, _config):
Alex Klein7bf0ecb2019-06-25 10:04:15 -0600226 """Get the pinned guest image information."""
227 sysroot_path = input_proto.sysroot.path
Alex Klein7bf0ecb2019-06-25 10:04:15 -0600228
229 chroot = controller_util.ParseChroot(input_proto.chroot)
230 sysroot = sysroot_lib.Sysroot(sysroot_path)
231
232 if not chroot.exists():
233 cros_build_lib.Die('Chroot does not exist: %s', chroot.path)
Alex Klein231d2da2019-07-22 16:44:45 -0600234 elif not sysroot.Exists(chroot=chroot):
Alex Klein7bf0ecb2019-06-25 10:04:15 -0600235 cros_build_lib.Die('Sysroot does not exist: %s',
236 chroot.full_path(sysroot.path))
237
238 pins = artifacts.FetchPinnedGuestImages(chroot, sysroot)
239
240 for pin in pins:
241 pinned_image = output_proto.pinned_images.add()
242 pinned_image.filename = pin.filename
243 pinned_image.uri = pin.uri
244
245
Alex Klein076841b2019-08-29 15:19:39 -0600246@faux.all_empty
Michael Mortensen38675192019-06-28 16:52:55 +0000247@validate.require('output_dir', 'sysroot.path')
Alex Klein231d2da2019-07-22 16:44:45 -0600248@validate.exists('output_dir')
249@validate.validation_complete
250def BundleFirmware(input_proto, output_proto, _config):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600251 """Tar the firmware images for a build target.
252
253 Args:
254 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600255 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600256 _config (api_config.ApiConfig): The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600257 """
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600258 output_dir = input_proto.output_dir
Michael Mortensen38675192019-06-28 16:52:55 +0000259 chroot = controller_util.ParseChroot(input_proto.chroot)
260 sysroot_path = input_proto.sysroot.path
261 sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein231d2da2019-07-22 16:44:45 -0600262
263 if not chroot.exists():
264 cros_build_lib.Die('Chroot does not exist: %s', chroot.path)
265 elif not sysroot.Exists(chroot=chroot):
266 cros_build_lib.Die('Sysroot does not exist: %s',
267 chroot.full_path(sysroot.path))
268
Michael Mortensen38675192019-06-28 16:52:55 +0000269 archive = artifacts.BuildFirmwareArchive(chroot, sysroot, output_dir)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600270
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600271 if archive is None:
272 cros_build_lib.Die(
Michael Mortensen38675192019-06-28 16:52:55 +0000273 'Could not create firmware archive. No firmware found for %s.',
274 sysroot_path)
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600275
Alex Klein231d2da2019-07-22 16:44:45 -0600276 output_proto.artifacts.add().path = archive
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600277
278
Alex Klein076841b2019-08-29 15:19:39 -0600279@faux.all_empty
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600280@validate.exists('output_dir')
Alex Klein231d2da2019-07-22 16:44:45 -0600281def BundleEbuildLogs(input_proto, output_proto, config):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600282 """Tar the ebuild logs for a build target.
283
284 Args:
285 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600286 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600287 config (api_config.ApiConfig): The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600288 """
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600289 output_dir = input_proto.output_dir
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600290 sysroot_path = input_proto.sysroot.path
291 chroot = controller_util.ParseChroot(input_proto.chroot)
Evan Hernandeza478d802019-04-08 15:08:24 -0600292
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600293 # TODO(mmortensen) Cleanup legacy handling after it has been switched over.
294 target = input_proto.build_target.name
295 if target:
296 # Legacy handling.
297 build_root = constants.SOURCE_ROOT
Alex Klein171da612019-08-06 14:00:42 -0600298 chroot = chroot_lib.Chroot(path=os.path.join(build_root, 'chroot'))
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600299 sysroot_path = os.path.join('/build', target)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600300
Alex Klein231d2da2019-07-22 16:44:45 -0600301 # TODO(saklein): Switch to validation_complete decorator after legacy
302 # handling has been cleaned up.
303 if config.validate_only:
304 return controller.RETURN_CODE_VALID_INPUT
305
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600306 sysroot = sysroot_lib.Sysroot(sysroot_path)
307 archive = artifacts.BundleEBuildLogsTarball(chroot, sysroot, output_dir)
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600308 if archive is None:
309 cros_build_lib.Die(
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600310 'Could not create ebuild logs archive. No logs found for %s.',
311 sysroot.path)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600312 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
Alex Klein6504eca2019-04-18 15:37:56 -0600313
314
Alex Klein076841b2019-08-29 15:19:39 -0600315@faux.all_empty
Andrew Lamb811aead2019-08-12 10:25:05 -0600316@validate.exists('output_dir')
317@validate.validation_complete
318def BundleChromeOSConfig(input_proto, output_proto, _config):
319 """Output the ChromeOS Config payload for a build target.
320
321 Args:
322 input_proto (BundleRequest): The input proto.
323 output_proto (BundleResponse): The output proto.
324 _config (api_config.ApiConfig): The API call config.
325 """
326 output_dir = input_proto.output_dir
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600327 sysroot_path = input_proto.sysroot.path
Andrew Lamb811aead2019-08-12 10:25:05 -0600328 chroot = controller_util.ParseChroot(input_proto.chroot)
329
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600330 # TODO(mmortensen) Cleanup legacy handling after it has been switched over.
331 target = input_proto.build_target.name
332 if target:
333 # Legacy handling.
334 build_root = constants.SOURCE_ROOT
335 chroot = chroot_lib.Chroot(path=os.path.join(build_root, 'chroot'))
336 sysroot_path = os.path.join('/build', target)
337
338 sysroot = sysroot_lib.Sysroot(sysroot_path)
Andrew Lamb811aead2019-08-12 10:25:05 -0600339 chromeos_config = artifacts.BundleChromeOSConfig(chroot, sysroot, output_dir)
340 if chromeos_config is None:
341 cros_build_lib.Die(
342 'Could not create ChromeOS Config payload. No config found for %s.',
343 sysroot.path)
344 output_proto.artifacts.add().path = os.path.join(output_dir, chromeos_config)
345
346
Alex Klein076841b2019-08-29 15:19:39 -0600347@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600348@validate.require('output_dir', 'sysroot.build_target.name', 'sysroot.path')
349@validate.exists('output_dir')
350@validate.validation_complete
351def BundleSimpleChromeArtifacts(input_proto, output_proto, _config):
Alex Klein2275d692019-04-23 16:04:12 -0600352 """Create the simple chrome artifacts."""
353 # Required args.
354 sysroot_path = input_proto.sysroot.path
355 build_target_name = input_proto.sysroot.build_target.name
356 output_dir = input_proto.output_dir
357
Alex Klein2275d692019-04-23 16:04:12 -0600358 # Optional args.
359 chroot_path = input_proto.chroot.path or constants.DEFAULT_CHROOT_PATH
360 cache_dir = input_proto.chroot.cache_dir
361
362 # Build out the argument instances.
363 build_target = build_target_util.BuildTarget(build_target_name)
364 chroot = chroot_lib.Chroot(path=chroot_path, cache_dir=cache_dir)
365 # Sysroot.path needs to be the fully qualified path, including the chroot.
366 full_sysroot_path = os.path.join(chroot.path, sysroot_path.lstrip(os.sep))
367 sysroot = sysroot_lib.Sysroot(full_sysroot_path)
368
369 # Quick sanity check that the sysroot exists before we go on.
370 if not sysroot.Exists():
371 cros_build_lib.Die('The sysroot does not exist.')
372
373 try:
374 results = artifacts.BundleSimpleChromeArtifacts(chroot, sysroot,
375 build_target, output_dir)
376 except artifacts.Error as e:
377 cros_build_lib.Die('Error %s raised in BundleSimpleChromeArtifacts: %s',
378 type(e), e)
379
380 for file_name in results:
381 output_proto.artifacts.add().path = file_name
382
383
Alex Klein076841b2019-08-29 15:19:39 -0600384@faux.all_empty
Michael Mortensen51f06722019-07-18 09:55:50 -0600385@validate.require('chroot.path', 'test_results_dir', 'output_dir')
Alex Klein231d2da2019-07-22 16:44:45 -0600386@validate.exists('output_dir')
387@validate.validation_complete
388def BundleVmFiles(input_proto, output_proto, _config):
Alex Klein6504eca2019-04-18 15:37:56 -0600389 """Tar VM disk and memory files.
390
391 Args:
392 input_proto (SysrootBundleRequest): The input proto.
393 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600394 _config (api_config.ApiConfig): The API call config.
Alex Klein6504eca2019-04-18 15:37:56 -0600395 """
Michael Mortensen51f06722019-07-18 09:55:50 -0600396 chroot = controller_util.ParseChroot(input_proto.chroot)
397 test_results_dir = input_proto.test_results_dir
Alex Klein6504eca2019-04-18 15:37:56 -0600398 output_dir = input_proto.output_dir
399
Michael Mortensen51f06722019-07-18 09:55:50 -0600400 archives = artifacts.BundleVmFiles(
401 chroot, test_results_dir, output_dir)
Alex Klein6504eca2019-04-18 15:37:56 -0600402 for archive in archives:
403 output_proto.artifacts.add().path = archive
Tiancong Wangc4805b72019-06-11 12:12:03 -0700404
Alex Klein231d2da2019-07-22 16:44:45 -0600405
Tiancong Wang24a3df72019-08-20 15:48:51 -0700406_VALID_ARTIFACT_TYPES = [toolchain_pb2.BENCHMARK_AFDO,
407 toolchain_pb2.ORDERFILE]
Alex Klein076841b2019-08-29 15:19:39 -0600408@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600409@validate.require('build_target.name', 'output_dir')
Tiancong Wang50b80a92019-08-01 14:46:15 -0700410@validate.is_in('artifact_type', _VALID_ARTIFACT_TYPES)
Alex Klein231d2da2019-07-22 16:44:45 -0600411@validate.exists('output_dir')
412@validate.validation_complete
Tiancong Wang50b80a92019-08-01 14:46:15 -0700413def BundleAFDOGenerationArtifacts(input_proto, output_proto, _config):
414 """Generic function for creating tarballs of both AFDO and orerfile.
Tiancong Wangc4805b72019-06-11 12:12:03 -0700415
416 Args:
Tiancong Wang50b80a92019-08-01 14:46:15 -0700417 input_proto (BundleChromeAFDORequest): The input proto.
Tiancong Wangc4805b72019-06-11 12:12:03 -0700418 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600419 _config (api_config.ApiConfig): The API call config.
Tiancong Wangc4805b72019-06-11 12:12:03 -0700420 """
Tiancong Wang50b80a92019-08-01 14:46:15 -0700421
Tiancong Wangc4805b72019-06-11 12:12:03 -0700422 # Required args.
Alex Klein231d2da2019-07-22 16:44:45 -0600423 build_target = build_target_util.BuildTarget(input_proto.build_target.name)
Tiancong Wangc4805b72019-06-11 12:12:03 -0700424 output_dir = input_proto.output_dir
Tiancong Wang50b80a92019-08-01 14:46:15 -0700425 artifact_type = input_proto.artifact_type
Tiancong Wangc4805b72019-06-11 12:12:03 -0700426
Tiancong Wangc4805b72019-06-11 12:12:03 -0700427 chroot = controller_util.ParseChroot(input_proto.chroot)
428
429 try:
Tiancong Wang24a3df72019-08-20 15:48:51 -0700430 is_orderfile = bool(artifact_type is toolchain_pb2.ORDERFILE)
Tiancong Wang50b80a92019-08-01 14:46:15 -0700431 results = artifacts.BundleAFDOGenerationArtifacts(
432 is_orderfile, chroot,
433 build_target, output_dir)
Tiancong Wangc4805b72019-06-11 12:12:03 -0700434 except artifacts.Error as e:
435 cros_build_lib.Die('Error %s raised in BundleSimpleChromeArtifacts: %s',
436 type(e), e)
437
438 for file_name in results:
439 output_proto.artifacts.add().path = file_name
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600440
441
Alex Klein076841b2019-08-29 15:19:39 -0600442@faux.all_empty
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600443@validate.exists('output_dir')
444def ExportCpeReport(input_proto, output_proto, config):
445 """Export a CPE report.
446
447 Args:
448 input_proto (BundleRequest): The input proto.
449 output_proto (BundleResponse): The output proto.
450 config (api_config.ApiConfig): The API call config.
451 """
452 chroot = controller_util.ParseChroot(input_proto.chroot)
453 output_dir = input_proto.output_dir
454
455 if input_proto.build_target.name:
456 # Legacy handling - use the default sysroot path for the build target.
457 build_target = controller_util.ParseBuildTarget(input_proto.build_target)
458 sysroot = sysroot_lib.Sysroot(build_target.root)
459 elif input_proto.sysroot.path:
460 sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path)
461 else:
462 # TODO(saklein): Switch to validate decorators once legacy handling can be
463 # cleaned up.
464 cros_build_lib.Die('sysroot.path is required.')
465
466 if config.validate_only:
467 return controller.RETURN_CODE_VALID_INPUT
468
469 cpe_result = artifacts.GenerateCpeReport(chroot, sysroot, output_dir)
470
471 output_proto.artifacts.add().path = cpe_result.report
472 output_proto.artifacts.add().path = cpe_result.warnings