forked from cloudfoundry/python-buildpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinalize.go
More file actions
160 lines (129 loc) · 4.17 KB
/
finalize.go
File metadata and controls
160 lines (129 loc) · 4.17 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
package finalize
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/kr/text"
"github.com/cloudfoundry/libbuildpack"
)
type Manifest interface {
RootDir() string
}
type Stager interface {
BuildDir() string
DepDir() string
DepsIdx() string
WriteProfileD(string, string) error
}
type Command interface {
Execute(string, io.Writer, io.Writer, string, ...string) error
Output(dir string, program string, args ...string) (string, error)
}
type ManagePyFinder interface {
FindManagePy(dir string) (string, error)
}
type Reqs interface {
FindAnyPackage(buildDir string, searchedPackages ...string) (bool, error)
FindStalePackages(oldRequirementsPath, newRequirementsPath string, excludedPackages ...string) ([]string, error)
}
type Finalizer struct {
Stager Stager
Log *libbuildpack.Logger
Logfile *os.File
Manifest Manifest
Command Command
ManagePyFinder ManagePyFinder
Requirements Reqs
}
func Run(f *Finalizer) error {
if err := f.HandleCollectstatic(); err != nil {
f.Log.Error("Error handling collectstatic: %v", err)
return err
}
if err := f.ReplaceDepsDirWithLiteral(); err != nil {
f.Log.Error("Error replacing depsDir with literal: %v", err)
return err
}
if err := f.ReplaceLiteralWithDepsDirAtRuntime(); err != nil {
f.Log.Error("Error replacing literal with depsDir: %v", err)
return err
}
return nil
}
func (f *Finalizer) HandleCollectstatic() error {
if len(os.Getenv("DISABLE_COLLECTSTATIC")) > 0 {
f.Log.Debug("DISABLE_COLLECTSTATIC > 0, skipping collectstatic")
return nil
}
exists, err := f.Requirements.FindAnyPackage(f.Stager.BuildDir(), "django", "Django")
if err != nil {
f.Log.Debug("Error during FindAnyPackage, skipping collectstatic")
return err
}
if !exists {
f.Log.Debug("Django not in requirements, skipping collectstatic")
return nil
}
managePyPath, err := f.ManagePyFinder.FindManagePy(f.Stager.BuildDir())
if err != nil {
f.Log.Debug("Error finding manage.py, skipping collectstatic")
return err
}
f.Log.Info("Running python %s collectstatic --noinput --traceback", managePyPath)
output := new(bytes.Buffer)
if err = f.Command.Execute(f.Stager.BuildDir(), output, text.NewIndentWriter(os.Stderr, []byte(" ")), "python", managePyPath, "collectstatic", "--noinput", "--traceback"); err != nil {
f.Log.Error(` ! Error while running '$ python %s collectstatic --noinput'.
See traceback above for details.
You may need to update application code to resolve this error.
Or, you can disable collectstatic for this application:
$ cf set-env <app> DISABLE_COLLECTSTATIC 1
https://devcenter.heroku.com/articles/django-assets`, managePyPath)
return err
}
writeFilteredCollectstaticOutput(output)
return nil
}
func writeFilteredCollectstaticOutput(output *bytes.Buffer) {
buffer := new(bytes.Buffer)
r := regexp.MustCompile("^(Copying |Post-processed ).*$")
lines := strings.Split(output.String(), "\n")
for _, line := range lines {
if len(line) > 0 && !r.MatchString(line) {
buffer.WriteString(line)
buffer.WriteString("\n")
}
}
os.Stdout.Write(buffer.Bytes())
}
func (f *Finalizer) ReplaceDepsDirWithLiteral() error {
dirs, err := filepath.Glob(filepath.Join(f.Stager.DepDir(), "python", "lib", "python*"))
if err != nil {
return err
}
for _, dir := range dirs {
if err := filepath.Walk(dir, func(path string, _ os.FileInfo, _ error) error {
if strings.HasSuffix(path, ".pth") {
fileContents, err := os.ReadFile(path)
if err != nil {
return err
}
fileContents = []byte(strings.Replace(string(fileContents), f.Stager.DepDir(), "DOLLAR_DEPS_DIR/"+f.Stager.DepsIdx(), -1))
if err := os.WriteFile(path, fileContents, 0644); err != nil {
return err
}
}
return nil
}); err != nil {
return err
}
}
return nil
}
func (f *Finalizer) ReplaceLiteralWithDepsDirAtRuntime() error {
scriptContents := `find $DEPS_DIR/%s/python/lib/python*/ -name "*.pth" -print0 2> /dev/null | xargs -r -0 -n 1 sed -i -e "s#DOLLAR_DEPS_DIR#$DEPS_DIR#" &> /dev/null` + "\n"
return f.Stager.WriteProfileD("python.fixeggs.sh", fmt.Sprintf(scriptContents, f.Stager.DepsIdx()))
}