This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
forked from paketo-buildpacks/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
153 lines (121 loc) · 4.92 KB
/
build.go
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
package cpython
import (
"path/filepath"
"time"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/draft"
"github.com/paketo-buildpacks/packit/v2/postal"
"github.com/paketo-buildpacks/packit/v2/sbom"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
//go:generate faux --interface DependencyManager --output fakes/dependency_manager.go
//go:generate faux --interface SBOMGenerator --output fakes/sbom_generator.go
// DependencyManager defines the interface for picking the best matching
// dependency installing it, and generating a BOM.
type DependencyManager interface {
Resolve(path, id, version, stack string) (postal.Dependency, error)
Deliver(dependency postal.Dependency, cnbPath, destinationPath, platformPath string) error
GenerateBillOfMaterials(dependencies ...postal.Dependency) []packit.BOMEntry
}
type SBOMGenerator interface {
GenerateFromDependency(dependency postal.Dependency, dir string) (sbom.SBOM, error)
}
// Build will return a packit.BuildFunc that will be invoked during the build
// phase of the buildpack lifecycle.
//
// Build will find the right cpython dependency to install, install it in a
// layer, and generate Bill-of-Materials. It also makes use of the checksum of
// the dependency to reuse the layer when possible.
func Build(dependencies DependencyManager, sbomGenerator SBOMGenerator, logger scribe.Emitter, clock chronos.Clock) packit.BuildFunc {
return func(context packit.BuildContext) (packit.BuildResult, error) {
logger.Title("%s %s", context.BuildpackInfo.Name, context.BuildpackInfo.Version)
logger.Process("Resolving CPython version")
planner := draft.NewPlanner()
entry, sortedEntries := planner.Resolve(Cpython, context.Plan.Entries, Priorities)
logger.Candidates(sortedEntries)
entryVersion, _ := entry.Metadata["version"].(string)
// This is done because the core dependencies pipeline provides the cpython
// dependency under the name python.
entry.Name = "python"
dependency, err := dependencies.Resolve(filepath.Join(context.CNBPath, "buildpack.toml"), entry.Name, entryVersion, context.Stack)
if err != nil {
return packit.BuildResult{}, err
}
// This is done because the core dependencies pipeline provides the cpython
// dependency under the name python.
dependency.ID = "cpython"
dependency.Name = "CPython"
logger.SelectedDependency(entry, dependency, clock.Now())
legacySBOM := dependencies.GenerateBillOfMaterials(dependency)
launch, build := planner.MergeLayerTypes(Cpython, context.Plan.Entries)
var launchMetadata packit.LaunchMetadata
if launch {
launchMetadata.BOM = legacySBOM
}
var buildMetadata packit.BuildMetadata
if build {
buildMetadata.BOM = legacySBOM
}
cpythonLayer, err := context.Layers.Get(Cpython)
if err != nil {
return packit.BuildResult{}, err
}
cpythonLayer.Launch, cpythonLayer.Build, cpythonLayer.Cache = launch, build, build
cachedSHA, ok := cpythonLayer.Metadata[DepKey].(string)
if ok && cachedSHA == dependency.SHA256 {
logger.Process("Reusing cached layer %s", cpythonLayer.Path)
logger.Break()
return packit.BuildResult{
Layers: []packit.Layer{cpythonLayer},
Build: buildMetadata,
Launch: launchMetadata,
}, nil
}
logger.Process("Executing build process")
cpythonLayer, err = cpythonLayer.Reset()
if err != nil {
return packit.BuildResult{}, err
}
cpythonLayer.Launch, cpythonLayer.Build, cpythonLayer.Cache = launch, build, build
logger.Subprocess("Installing CPython %s", dependency.Version)
duration, err := clock.Measure(func() error {
return dependencies.Deliver(dependency, context.CNBPath, cpythonLayer.Path, context.Platform.Path)
})
if err != nil {
return packit.BuildResult{}, err
}
logger.Action("Completed in %s", duration.Round(time.Millisecond))
logger.Break()
logger.GeneratingSBOM(cpythonLayer.Path)
var sbomContent sbom.SBOM
duration, err = clock.Measure(func() error {
sbomContent, err = sbomGenerator.GenerateFromDependency(dependency, cpythonLayer.Path)
return err
})
if err != nil {
return packit.BuildResult{}, err
}
logger.Action("Completed in %s", duration.Round(time.Millisecond))
logger.Break()
logger.FormattingSBOM(context.BuildpackInfo.SBOMFormats...)
cpythonLayer.SBOM, err = sbomContent.InFormats(context.BuildpackInfo.SBOMFormats...)
if err != nil {
return packit.BuildResult{}, err
}
cpythonLayer.Metadata = map[string]interface{}{
DepKey: dependency.SHA256,
}
cpythonLayer.SharedEnv.Default("PYTHONPATH", cpythonLayer.Path)
cpythonLayer.SharedEnv.Default("PYTHONPYCACHEPREFIX", "$HOME/.pycache")
logger.Break()
logger.Process("Configuring environment")
logger.Subprocess("%s", scribe.NewFormattedMapFromEnvironment(cpythonLayer.SharedEnv))
logger.Break()
return packit.BuildResult{
Layers: []packit.Layer{cpythonLayer},
Build: buildMetadata,
Launch: launchMetadata,
}, nil
}
}