-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinstall_process.go
85 lines (70 loc) · 2.32 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
package poetryinstall
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"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
}
// PoetryInstallProcess implements the InstallProcess interface.
type PoetryInstallProcess struct {
executable Executable
logger scribe.Emitter
}
// NewPoetryInstallProcess creates an instance of the PoetryInstallProcess given an Executable.
func NewPoetryInstallProcess(executable Executable, logger scribe.Emitter) PoetryInstallProcess {
return PoetryInstallProcess{
executable: executable,
logger: logger,
}
}
// Execute installs the poetry dependencies from workingDir/pyproject.toml into
// a virtual env in the targetPath.
func (p PoetryInstallProcess) Execute(workingDir, targetPath, cachePath string) (string, error) {
args := []string{"install"}
env := append(
os.Environ(),
fmt.Sprintf("POETRY_CACHE_DIR=%s", cachePath),
fmt.Sprintf("POETRY_VIRTUALENVS_PATH=%s", targetPath),
)
p.logger.Subprocess(fmt.Sprintf("Running 'POETRY_CACHE_DIR=%s POETRY_VIRTUALENVS_PATH=%s poetry %s'", cachePath, targetPath, strings.Join(args, " ")))
err := p.executable.Execute(pexec.Execution{
Args: args,
Env: env,
Dir: workingDir,
Stdout: p.logger.ActionWriter,
Stderr: p.logger.ActionWriter,
})
if err != nil {
return "", fmt.Errorf("poetry install failed:\nerror: %w", err)
}
return p.findVenvDir(workingDir, targetPath, cachePath)
}
func (p PoetryInstallProcess) findVenvDir(workingDir, targetPath, cachePath string) (string, error) {
env := append(
os.Environ(),
fmt.Sprintf("POETRY_CACHE_DIR=%s", cachePath),
fmt.Sprintf("POETRY_VIRTUALENVS_PATH=%s", targetPath),
)
args := []string{"env", "info", "--path"}
outBuffer := bytes.NewBuffer(nil)
errBuffer := bytes.NewBuffer(nil)
err := p.executable.Execute(pexec.Execution{
Args: args,
Env: env,
Dir: workingDir,
Stdout: outBuffer,
Stderr: errBuffer,
})
if err != nil {
return "", fmt.Errorf("failed to find virtual env directory:\n%s\n%s\nerror: %w", outBuffer, errBuffer, err)
}
return filepath.Clean(strings.TrimSpace(outBuffer.String())), nil
}