-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpip_cleanup.go
74 lines (64 loc) · 2.21 KB
/
pip_cleanup.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
package cpython
import (
"os"
"path/filepath"
"github.com/paketo-buildpacks/packit/v2/pexec"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
// This function serves as a constant for packages to be uninstalled
func pipPackagesToBeUninstalled() []string {
return []string{"setuptools"}
}
// PipCleanup implements the PythonPipCleanup interface.
type PipCleanup struct {
pythonProcess Executable
logger scribe.Emitter
}
// NewPipCleanup creates an instance of PipCleanup given a python Executable and a scribe.Emitter.
func NewPipCleanup(pythonProcess Executable, logger scribe.Emitter) PipCleanup {
return PipCleanup{
pythonProcess: pythonProcess,
logger: logger,
}
}
func (i PipCleanup) Cleanup(packages []string, targetLayer string) error {
env := environWithUpdatedPath(os.Environ(), "PATH", filepath.Join(targetLayer, "bin"))
env = environWithUpdatedPath(env, "LD_LIBRARY_PATH", filepath.Join(targetLayer, "lib"))
if len(packages) > 0 {
// Verify pip --version works to ensure subsequent pip commands will work
err := i.pythonProcess.Execute(pexec.Execution{
Args: []string{"-m", "pip", "--version"},
Env: env,
Stdout: i.logger.Debug.ActionWriter,
Stderr: i.logger.Debug.ActionWriter,
})
if err != nil {
i.logger.Subprocess("pip --version failed. Run with --env BP_LOG_LEVEL=DEBUG to see more information")
return err
}
// Remove packages from site-packages in the targetLayer
for _, name := range packages {
i.logger.Debug.Subprocess("Checking if '%s' package is installed", name)
err := i.pythonProcess.Execute(pexec.Execution{
Args: []string{"-m", "pip", "show", "-q", name},
Env: env,
Stdout: i.logger.Debug.ActionWriter,
Stderr: i.logger.Debug.ActionWriter,
})
if err == nil {
i.logger.Debug.Subprocess("Uninstalling '%s' package", name)
err = i.pythonProcess.Execute(pexec.Execution{
Args: []string{"-m", "pip", "uninstall", "-y", name},
Env: env,
Stdout: i.logger.Debug.ActionWriter,
Stderr: i.logger.Debug.ActionWriter,
})
if err != nil {
i.logger.Subprocess("pip uninstall failed. Run with --env BP_LOG_LEVEL=DEBUG to see more information")
return err
}
}
}
}
return nil
}