blob: 476f59319180b85ac9934120d928ca6f03265619 [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 Hernandezf388cbf2019-04-01 11:15:23 -060018def 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.
Evan Hernandez59690b72019-04-08 16:24:45 -060030 img_root = os.path.join(build_root, 'src/build/images', target, 'latest')
Evan Hernandezf388cbf2019-04-01 11:15:23 -060031 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)
Evan Hernandezb04e2aa2019-04-08 16:55:54 -060059 osutils.CopyDirContents(temp, output_dir, allow_nonempty=True)
Evan Hernandezf388cbf2019-04-01 11:15:23 -060060
61
62def 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 Hernandez36589c62019-04-05 18:14:42 -060072 cwd = os.path.join(build_root, 'chroot/build', target, 'usr/local/build')
Evan Hernandezf388cbf2019-04-01 11:15:23 -060073
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
84def 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 Hernandez36589c62019-04-05 18:14:42 -060094 cwd = os.path.join(build_root, 'chroot/build', target, 'build')
Evan Hernandezf388cbf2019-04-01 11:15:23 -060095
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
104def 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
122def 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
139def 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
Evan Hernandeza478d802019-04-08 15:08:24 -0600148
149 # commands.BuildEbuildLogsTarball conflates "buildroot" with sysroot.
150 # Adjust accordingly...
151 build_root = os.path.join(constants.SOURCE_ROOT, 'chroot', 'build')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600152
153 # TODO(saklein): Replace with a chromite/service implementation.
154 archive = commands.BuildEbuildLogsTarball(build_root, target, output_dir)
155
156 output_proto.artifacts.add().path = os.path.join(output_dir, archive)