forked from paketo-buildpacks/pipenv-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_process.go
106 lines (93 loc) · 2.88 KB
/
install_process.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
package pipenvinstall
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/pexec"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
//go:generate faux --interface Executable --output fakes/executable.go
// Executable defines the interface for invoking an executable.
type Executable interface {
Execute(pexec.Execution) error
}
// PipenvInstallProcess implements the InstallProcess interface.
type PipenvInstallProcess struct {
executable Executable
logger scribe.Emitter
}
// NewPipenvInstallProcess creates an instance of the PipenvInstallProcess given an Executable.
func NewPipenvInstallProcess(executable Executable, logger scribe.Emitter) PipenvInstallProcess {
return PipenvInstallProcess{
executable: executable,
logger: logger,
}
}
// Execute installs the pipenv dependencies from workingDir/Pipfile into the
// targetLayer. The cacheLayer is used for the pipenv cache directory.
func (p PipenvInstallProcess) Execute(workingDir string, targetLayer, cacheLayer packit.Layer) error {
targetPath := targetLayer.Path
cachePath := cacheLayer.Path
lockExists := true
args := []string{
"install",
// --deploy is for checking Pipefile and lock are in sync
"--deploy",
// --system is not used because it does not let us write to a specific path
}
_, err := os.Stat(filepath.Join(workingDir, "Pipfile.lock"))
if err != nil {
if errors.Is(err, os.ErrNotExist) {
lockExists = false
args = []string{
"install",
// Do not write out a Pipfile.lock. It's not useful and is expensive.
"--skip-lock",
}
} else {
return fmt.Errorf("failed to stat Pipfile.lock: %w", err)
}
}
p.logger.Subprocess("Running 'pipenv %s'", strings.Join(args, " "))
buffer := bytes.NewBuffer(nil)
err = p.executable.Execute(pexec.Execution{
Args: args,
Env: append(os.Environ(),
// Pipenv seems to disregard PYTHONUSERBASE.
// Target dir set using WORKON_HOME which is a virtualenv setting.
"PIP_USER=1",
"PIP_IGNORE_INSTALLED=1",
fmt.Sprintf("WORKON_HOME=%s", targetPath),
fmt.Sprintf("PIPENV_CACHE_DIR=%s", cachePath)),
Dir: workingDir,
Stdout: buffer,
Stderr: buffer,
})
if err != nil {
return fmt.Errorf("pipenv install failed:\n%s\nerror: %w", buffer.String(), err)
}
// if clean is run when no lock file exists, it will generate
// one, which is an expensive operation.
if lockExists {
p.logger.Subprocess("Running 'pipenv clean'")
buffer.Reset()
err = p.executable.Execute(pexec.Execution{
Args: []string{"clean"},
Env: append(os.Environ(),
"PIP_USER=1",
fmt.Sprintf("WORKON_HOME=%s", targetPath),
fmt.Sprintf("PIPENV_CACHE_DIR=%s", cachePath)),
Dir: workingDir,
Stdout: buffer,
Stderr: buffer,
})
if err != nil {
return fmt.Errorf("pipenv clean failed:\n%s\nerror: %w", buffer.String(), err)
}
}
return nil
}