-
Notifications
You must be signed in to change notification settings - Fork 11
/
detect.go
72 lines (65 loc) · 1.89 KB
/
detect.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
package pipinstall
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/fs"
)
// BuildPlanMetadata is the buildpack specific data included in build plan
// requirements.
type BuildPlanMetadata struct {
// Build denotes the dependency is needed at build-time.
Build bool `toml:"build"`
}
// Detect will return a packit.DetectFunc that will be invoked during the
// detect phase of the buildpack lifecycle.
//
// Detection will contribute a Build Plan that provides site-packages,
// and requires cpython and pip at build.
func Detect() packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
requirementsFile := "requirements.txt"
envRequirement, requirementEnvExists := os.LookupEnv("BP_PIP_REQUIREMENT")
if requirementEnvExists {
requirementsFile = envRequirement
}
missingRequirementFiles := []string{}
allRequirementsFilesExist := true
for _, filename := range strings.Split(requirementsFile, " ") {
found, err := fs.Exists(filepath.Join(context.WorkingDir, filename))
if err != nil {
return packit.DetectResult{}, err
}
if !found {
missingRequirementFiles = append(missingRequirementFiles, filename)
}
allRequirementsFilesExist = allRequirementsFilesExist && found
}
if !allRequirementsFilesExist {
return packit.DetectResult{}, packit.Fail.WithMessage(fmt.Sprintf("requirements file not found at: '%s'", strings.Join(missingRequirementFiles, "', '")))
}
return packit.DetectResult{
Plan: packit.BuildPlan{
Provides: []packit.BuildPlanProvision{
{Name: SitePackages},
},
Requires: []packit.BuildPlanRequirement{
{
Name: CPython,
Metadata: BuildPlanMetadata{
Build: true,
},
},
{
Name: Pip,
Metadata: BuildPlanMetadata{
Build: true,
},
},
},
},
}, nil
}
}