forked from paketo-buildpacks/pipenv-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite_process.go
45 lines (36 loc) · 1.09 KB
/
site_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
package pipenvinstall
import (
"bytes"
"fmt"
"os"
"strings"
"github.com/paketo-buildpacks/packit/v2/pexec"
)
// SiteProcess implements the Executable interface.
type SiteProcess struct {
executable Executable
}
// NewSiteProcess creates an instance of the SiteProcess given an Executable that runs `python`
func NewSiteProcess(executable Executable) SiteProcess {
return SiteProcess{
executable: executable,
}
}
// Execute runs a python command to locate the site packages within the pip targetLayerPath.
func (p SiteProcess) Execute(layerPath string) (string, error) {
buffer := bytes.NewBuffer(nil)
err := p.executable.Execute(pexec.Execution{
Args: []string{"-m", "site", "--user-site"},
Env: append(os.Environ(), fmt.Sprintf("PYTHONUSERBASE=%s", layerPath)),
Stdout: buffer,
Stderr: buffer,
})
if err != nil {
return "", fmt.Errorf("failed to locate site packages:\n%s\nerror: %w", buffer.String(), err)
}
path := strings.TrimSpace(buffer.String())
if len(path) == 0 {
return "", fmt.Errorf("failed to locate site packages: output is empty")
}
return path, nil
}