-
Notifications
You must be signed in to change notification settings - Fork 7
/
detect.go
177 lines (157 loc) · 4.65 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
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
package pythonstart
import (
"fmt"
"os"
"path/filepath"
"strconv"
"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 {
// Launch flag requests the given requirement be made available during the
// launch phase of the buildpack lifecycle.
Launch bool `toml:"launch"`
}
// Detect will return a packit.DetectFunc that will be invoked during the
// detect phase of the buildpack lifecycle.
//
// If this buildpack detects files that indicate your app is a Python project,
// it will pass detection. It will require "cpython" OR "cpython" and
// "site-packages" OR "conda-environment" as launch-time build plan
// requirements, depending on whether it detects files indicating the use of
// different package managers.
//
// If BP_LIVE_RELOAD_ENABLED=true in the build environment, it will
// additionally require "watchexec" at launch-time
func Detect() packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
envFile, err := fs.Exists(filepath.Join(context.WorkingDir, "environment.yml"))
if err != nil {
return packit.DetectResult{}, packit.Fail.WithMessage("failed trying to stat environment.yml: %w", err)
}
requirementsFile, err := fs.Exists(filepath.Join(context.WorkingDir, "requirements.txt"))
if err != nil {
return packit.DetectResult{}, packit.Fail.WithMessage("failed trying to stat requirements.txt: %w", err)
}
lockFile, err := fs.Exists(filepath.Join(context.WorkingDir, "package-list.txt"))
if err != nil {
return packit.DetectResult{}, packit.Fail.WithMessage("failed trying to stat package-list.txt: %w", err)
}
pyprojectTOMLFile, err := fs.Exists(filepath.Join(context.WorkingDir, "pyproject.toml"))
if err != nil {
return packit.DetectResult{}, packit.Fail.WithMessage("failed trying to stat pyproject.toml: %w", err)
}
pythonFiles, err := filepath.Glob(filepath.Join(context.WorkingDir, "*.py"))
if err != nil {
return packit.DetectResult{}, packit.Fail.WithMessage("failed trying to find *.py files: %w", err)
}
if !envFile && !requirementsFile && !lockFile && !pyprojectTOMLFile && len(pythonFiles) < 1 {
return packit.DetectResult{}, packit.Fail.WithMessage("No *.py, environment.yml, requirements.txt, pyproject.toml, or package-list.txt found")
}
simplePlan := packit.BuildPlan{
Provides: []packit.BuildPlanProvision{},
Requires: []packit.BuildPlanRequirement{
{
Name: "cpython",
Metadata: BuildPlanMetadata{
Launch: true,
},
},
},
}
pipPlan := packit.BuildPlan{
Provides: []packit.BuildPlanProvision{},
Requires: []packit.BuildPlanRequirement{
{
Name: "cpython",
Metadata: BuildPlanMetadata{
Launch: true,
},
},
{
Name: "site-packages",
Metadata: BuildPlanMetadata{
Launch: true,
},
},
},
}
condaPlan := packit.BuildPlan{
Provides: []packit.BuildPlanProvision{},
Requires: []packit.BuildPlanRequirement{
{
Name: "conda-environment",
Metadata: BuildPlanMetadata{
Launch: true,
},
},
},
}
poetryInstallPlan := packit.BuildPlan{
Provides: []packit.BuildPlanProvision{},
Requires: []packit.BuildPlanRequirement{
{
Name: "cpython",
Metadata: BuildPlanMetadata{
Launch: true,
},
},
{
Name: "poetry",
Metadata: BuildPlanMetadata{
Launch: true,
},
},
{
Name: "poetry-venv",
Metadata: BuildPlanMetadata{
Launch: true,
},
},
},
}
plans := []packit.BuildPlan{pipPlan, condaPlan, poetryInstallPlan, simplePlan}
shouldReload, err := checkLiveReloadEnabled()
if err != nil {
return packit.DetectResult{}, err
}
if shouldReload {
for i := range plans {
plans[i].Requires = append(plans[i].Requires, packit.BuildPlanRequirement{
Name: "watchexec",
Metadata: BuildPlanMetadata{
Launch: true,
},
})
}
}
return packit.DetectResult{
Plan: or(plans...),
}, nil
}
}
func checkLiveReloadEnabled() (bool, error) {
if reload, ok := os.LookupEnv("BP_LIVE_RELOAD_ENABLED"); ok {
shouldEnableReload, err := strconv.ParseBool(reload)
if err != nil {
return false, fmt.Errorf("failed to parse BP_LIVE_RELOAD_ENABLED value %s: %w", reload, err)
}
return shouldEnableReload, nil
}
return false, nil
}
func or(plans ...packit.BuildPlan) packit.BuildPlan {
if len(plans) < 1 {
return packit.BuildPlan{}
}
combinedPlan := plans[0]
for i := range plans {
if i == 0 {
continue
}
combinedPlan.Or = append(combinedPlan.Or, plans[i])
}
return combinedPlan
}