forked from paketo-buildpacks/pipenv-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvenv_locator.go
50 lines (41 loc) · 1.13 KB
/
venv_locator.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
package pipenvinstall
import (
"errors"
"os"
"path/filepath"
"github.com/paketo-buildpacks/packit/v2"
)
type VenvLocator struct {
}
func NewVenvLocator() VenvLocator {
return VenvLocator{}
}
// It would have been cleaner to run "pipenv --venv"
// and extract out the exact virtual env dir,
// but it doesn't seem to work.
// So we look for the dir with pyvenv.cfg in $WORKON_HOME
func (v VenvLocator) LocateVenvDir(path string) (string, error) {
entries, err := os.ReadDir(path)
if err != nil {
return "", packit.Fail.WithMessage("reading target directory %s failed:\nerror: %w", path, err)
}
venvDir := ""
for _, entry := range entries {
if !entry.IsDir() {
continue
}
_, err := os.Stat(filepath.Join(path, entry.Name(), "pyvenv.cfg"))
if err != nil {
if errors.Is(err, os.ErrNotExist) {
continue
}
return "", packit.Fail.WithMessage("pipenv virtual env dir lookup failed in target %s: %w", path, err)
}
venvDir = entry.Name()
break
}
if venvDir == "" {
return "", packit.Fail.WithMessage("pipenv virtual env directory not found in target %s", path)
}
return filepath.Join(path, venvDir), nil
}