-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathjava_deps.go
More file actions
377 lines (353 loc) · 10.8 KB
/
Copy pathjava_deps.go
File metadata and controls
377 lines (353 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// SPDX-FileCopyrightText: Copyright (c) NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strings"
)
const javaDescriptorName = "bazel-java-ci.json"
type javaComponent struct {
ID string
Path string
InventoryPath string
Target string
}
type javaRuntimeInventory struct {
Dependencies []javaRuntimeDependency `json:"dependencies"`
}
type javaRuntimeDependency struct {
Coordinate string `json:"coordinate"`
Licenses []string `json:"licenses"`
Name string `json:"name"`
URL string `json:"url"`
}
type mergedJavaDependency struct {
Coordinate string
Licenses map[string]struct{}
Names map[string]struct{}
URLs map[string]struct{}
}
func discoverJavaComponents(root string) ([]javaComponent, error) {
srcRoot := filepath.Join(root, "src")
if st, err := os.Stat(srcRoot); err != nil || !st.IsDir() {
if err == nil {
err = fmt.Errorf("not a directory")
}
return nil, fmt.Errorf("discover Java components under %s: %w", srcRoot, err)
}
var components []javaComponent
ids := map[string]string{}
err := walkImportRoot(srcRoot, func(path string, d fs.DirEntry) error {
if d.IsDir() || d.Name() != javaDescriptorName {
return nil
}
component, err := parseJavaComponentDescriptor(root, path)
if err != nil {
return err
}
if previous, ok := ids[component.ID]; ok {
return fmt.Errorf(
"duplicate Java component id %q in %s and %s",
component.ID,
previous,
path,
)
}
ids[component.ID] = path
components = append(components, component)
return nil
})
if err != nil {
return nil, err
}
sort.Slice(components, func(i, j int) bool {
return components[i].Path < components[j].Path
})
if len(components) == 0 {
return nil, fmt.Errorf(
"no %s descriptors found under %s; Java dependency collection cannot continue",
javaDescriptorName,
srcRoot,
)
}
return components, nil
}
func parseJavaComponentDescriptor(root, descriptorPath string) (javaComponent, error) {
raw, err := os.ReadFile(descriptorPath)
if err != nil {
return javaComponent{}, fmt.Errorf("read Java descriptor %s: %w", descriptorPath, err)
}
var document map[string]json.RawMessage
if err := json.Unmarshal(raw, &document); err != nil {
return javaComponent{}, fmt.Errorf("parse Java descriptor %s: %w", descriptorPath, err)
}
id, err := requiredDescriptorString(document, "id")
if err != nil {
return javaComponent{}, fmt.Errorf("%s: %w", descriptorPath, err)
}
if !regexp.MustCompile(`^[a-z0-9][a-z0-9-]*$`).MatchString(id) {
return javaComponent{}, fmt.Errorf(
"%s: id must contain lowercase letters, digits, or hyphens, got %q",
descriptorPath,
id,
)
}
componentKind, err := requiredDescriptorString(document, "component_kind")
if err != nil {
return javaComponent{}, fmt.Errorf("%s: %w", descriptorPath, err)
}
if componentKind != "java-framework" && componentKind != "java-service" {
return javaComponent{}, fmt.Errorf(
"%s: component_kind must be java-framework or java-service, got %q",
descriptorPath,
componentKind,
)
}
ciLane, err := requiredDescriptorString(document, "ci_lane")
if err != nil {
return javaComponent{}, fmt.Errorf("%s: %w", descriptorPath, err)
}
if ciLane != "build-container" && ciLane != "docker-host" {
return javaComponent{}, fmt.Errorf(
"%s: ci_lane must be build-container or docker-host, got %q",
descriptorPath,
ciLane,
)
}
if rawTestsSkip, ok := document["tests_skip"]; !ok {
return javaComponent{}, fmt.Errorf("%s: missing required field tests_skip", descriptorPath)
} else {
var testsSkip bool
if err := json.Unmarshal(rawTestsSkip, &testsSkip); err != nil {
return javaComponent{}, fmt.Errorf("%s: tests_skip must be boolean", descriptorPath)
}
}
componentDir := filepath.Dir(descriptorPath)
relativePath, err := filepath.Rel(root, componentDir)
if err != nil {
return javaComponent{}, fmt.Errorf("resolve Java component path for %s: %w", descriptorPath, err)
}
relativePath = filepath.ToSlash(relativePath)
if relativePath == "." || strings.HasPrefix(relativePath, "../") {
return javaComponent{}, fmt.Errorf(
"Java descriptor %s is outside repository root %s",
descriptorPath,
root,
)
}
return javaComponent{
ID: id,
Path: relativePath,
InventoryPath: filepath.FromSlash(relativePath + "/runtime_inventory.json"),
Target: "//" + relativePath + ":runtime_inventory.json",
}, nil
}
func requiredDescriptorString(document map[string]json.RawMessage, field string) (string, error) {
raw, ok := document[field]
if !ok {
return "", fmt.Errorf("missing required field %s", field)
}
var value string
if err := json.Unmarshal(raw, &value); err != nil || strings.TrimSpace(value) == "" {
return "", fmt.Errorf("%s must be a non-empty string", field)
}
return strings.TrimSpace(value), nil
}
func collectJavaInventoryDependencies(root string) (map[string]mergedJavaDependency, int, error) {
components, err := discoverJavaComponents(root)
if err != nil {
return nil, 0, err
}
bazelBin, err := materializeJavaInventories(root, components)
if err != nil {
return nil, 0, err
}
dependencies := map[string]mergedJavaDependency{}
for _, component := range components {
inventoryPath := filepath.Join(bazelBin, component.InventoryPath)
inventory, err := readJavaRuntimeInventory(inventoryPath)
if err != nil {
return nil, 0, fmt.Errorf(
"read Java runtime inventory for component %s from %s: %w",
component.ID,
inventoryPath,
err,
)
}
for _, dependency := range inventory.Dependencies {
mergeJavaDependency(dependencies, dependency)
}
}
return dependencies, len(components), nil
}
func materializeJavaInventories(root string, components []javaComponent) (string, error) {
if override := strings.TrimSpace(os.Getenv("COLLECT_DEPS_JAVA_INVENTORY_ROOT")); override != "" {
return override, nil
}
bazel := strings.TrimSpace(os.Getenv("COLLECT_DEPS_BAZEL"))
if bazel == "" {
bazel = "bazel"
}
startupArgs := javaBazelStartupArgs()
targets := make([]string, 0, len(components))
for _, component := range components {
targets = append(targets, component.Target)
}
buildArgs := append(append([]string{}, startupArgs...), "build")
buildArgs = append(buildArgs, targets...)
fmt.Fprintf(
os.Stderr,
"collect-dependencies: building %d Java runtime inventories with Bazel\n",
len(targets),
)
stdout, stderr, err := runCommand(root, nil, javaBazelTimeout, bazel, buildArgs...)
if err != nil {
var execErr *exec.Error
if errors.As(err, &execErr) {
return "", fmt.Errorf(
"build Java runtime inventories: %q is not available; install Bazelisk as bazel",
bazel,
)
}
return "", fmt.Errorf(
"build Java runtime inventories with %s: %w\n%s",
bazel,
err,
commandFailureOutput(stdout, stderr),
)
}
infoArgs := append(append([]string{}, startupArgs...), "info", "bazel-bin")
stdout, stderr, err = runCommand(root, nil, javaBazelTimeout, bazel, infoArgs...)
if err != nil {
return "", fmt.Errorf(
"locate bazel-bin with %s: %w\n%s",
bazel,
err,
commandFailureOutput(stdout, stderr),
)
}
bazelBin := strings.TrimSpace(stdout)
if bazelBin == "" {
return "", fmt.Errorf("%s info bazel-bin returned an empty path", bazel)
}
return bazelBin, nil
}
func javaBazelStartupArgs() []string {
outputRoot := strings.TrimSpace(os.Getenv("BAZEL_OUTPUT_USER_ROOT"))
if outputRoot == "" {
return nil
}
return []string{"--output_user_root=" + outputRoot}
}
func commandFailureOutput(stdout, stderr string) string {
message := strings.TrimSpace(stderr)
if message == "" {
message = strings.TrimSpace(stdout)
}
if len(message) > 4000 {
message = message[len(message)-4000:]
}
return message
}
func readJavaRuntimeInventory(path string) (javaRuntimeInventory, error) {
raw, err := os.ReadFile(path)
if err != nil {
return javaRuntimeInventory{}, err
}
var inventory javaRuntimeInventory
if err := json.Unmarshal(raw, &inventory); err != nil {
return javaRuntimeInventory{}, fmt.Errorf("parse JSON: %w", err)
}
if inventory.Dependencies == nil {
return javaRuntimeInventory{}, fmt.Errorf("missing dependencies array")
}
for i, dependency := range inventory.Dependencies {
if strings.TrimSpace(dependency.Coordinate) == "" {
return javaRuntimeInventory{}, fmt.Errorf("dependency %d has an empty coordinate", i)
}
}
return inventory, nil
}
func mergeJavaDependency(
dependencies map[string]mergedJavaDependency,
dependency javaRuntimeDependency,
) {
coordinate := strings.TrimSpace(dependency.Coordinate)
merged, ok := dependencies[coordinate]
if !ok {
merged = mergedJavaDependency{
Coordinate: coordinate,
Licenses: map[string]struct{}{},
Names: map[string]struct{}{},
URLs: map[string]struct{}{},
}
}
for _, license := range dependency.Licenses {
if license = strings.TrimSpace(license); license != "" {
merged.Licenses[license] = struct{}{}
}
}
if name := strings.TrimSpace(dependency.Name); name != "" {
merged.Names[name] = struct{}{}
}
if rawURL := strings.TrimSpace(dependency.URL); rawURL != "" {
merged.URLs[rawURL] = struct{}{}
}
dependencies[coordinate] = merged
}
func buildJavaInventoryRows(
dependencies map[string]mergedJavaDependency,
) []dependencyRow {
rows := make([]dependencyRow, 0, len(dependencies))
for _, coordinate := range sortedMapKeys(dependencies) {
dependency := dependencies[coordinate]
licenses := sortedKeys(dependency.Licenses)
license := "_(runtime inventory has no license metadata)_"
if len(licenses) > 0 {
license = strings.Join(licenses, " / ")
}
rows = append(rows, dependencyRow{
Language: "Java",
SortKey: strings.ToLower(coordinate),
Spec: javaDependencySpec(dependency),
License: license,
})
}
return rows
}
func javaDependencySpec(dependency mergedJavaDependency) string {
spec := "`" + dependency.Coordinate + "`"
names := sortedKeys(dependency.Names)
urls := sortedKeys(dependency.URLs)
details := []string{}
if len(names) > 0 {
details = append(details, strings.Join(names, " / "))
}
if len(urls) > 0 {
details = append(details, strings.Join(urls, " / "))
}
if len(details) > 0 {
spec += " (" + strings.Join(details, "; ") + ")"
}
return spec
}