-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonvenv.go
More file actions
64 lines (54 loc) · 1.33 KB
/
pythonvenv.go
File metadata and controls
64 lines (54 loc) · 1.33 KB
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
package gopythonvenv
import (
"errors"
"os"
"os/exec"
"github.com/CREDOProject/go-pythonvenv/finder"
"github.com/CREDOProject/go-pythonvenv/utils"
"github.com/CREDOProject/sharedutils/files"
)
var (
ErrAlreadyPresent = errors.New("Virtual environment already exists.")
)
// Structure representing a PythonVenv
type PythonVenv struct {
Path string
}
// Creates a virtual environment in the specified path using the latest
// Python version available in the system.
func Create(path string) (*PythonVenv, error) {
err := createVenv(path)
if err != nil && err != ErrAlreadyPresent {
return nil, err
}
return &PythonVenv{
Path: path,
}, nil
}
func createVenv(path string) error {
if files.IsDir(path) {
return ErrAlreadyPresent
}
v, err := finder.New().Find()
if err != nil {
return err
}
cmd := exec.Command(v.Path, "-m", "venv", path)
err = cmd.Run()
return err
}
// Activates the virtual environment in the path.
func (g *PythonVenv) Activate() []string {
environment := utils.Env()
environment["VIRTUAL_ENV"] = g.Path
environment["PATH"] = g.Path + "/bin:" + environment["PATH"]
delete(environment, "PYTHONHOME")
return utils.Roll(environment)
}
// Removes the virtual environment in the specified path.
func (g *PythonVenv) Destroy() error {
if err := os.RemoveAll(g.Path); err != nil {
return err
}
return nil
}