-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute.go
90 lines (80 loc) · 2.4 KB
/
execute.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
86
87
88
89
90
package pyenv
import (
"fmt"
"log"
"os/exec"
"path/filepath"
)
// Darwin Executor
func (env *DarwinPyEnv) AddDependencies(requirementsPath string) error {
fp := filepath.Join(env.EnvOptions.ParentPath, "dist/python/install/bin/pip")
err := dependencyHelper(env.EnvOptions, fp, requirementsPath)
if err != nil {
return err
}
return nil
}
func (env *DarwinPyEnv) ExecutePython(args ...string) (*exec.Cmd, error) {
fp := filepath.Join(env.EnvOptions.ParentPath, "dist/python/install/bin/python")
cmd, err := executeHelper(env.EnvOptions, fp, args...)
if err != nil {
return nil, err
}
return cmd, nil
}
// Linux Executor
func (env *LinuxPyEnv) AddDependencies(requirementsPath string) error {
fp := filepath.Join(env.EnvOptions.ParentPath, "dist/python/install/bin/pip")
err := dependencyHelper(env.EnvOptions, fp, requirementsPath)
if err != nil {
return err
}
return nil
}
func (env *LinuxPyEnv) ExecutePython(args ...string) (*exec.Cmd, error) {
fp := filepath.Join(env.EnvOptions.ParentPath, "dist/python/install/bin/python")
cmd, err := executeHelper(env.EnvOptions, fp, args...)
if err != nil {
return nil, err
}
return cmd, nil
}
// Windows Executor
func (env *WindowsPyEnv) AddDependencies(requirementsPath string) error {
fp := filepath.Join(env.EnvOptions.ParentPath, "dist/python/install/Scripts/pip3.exe")
err := dependencyHelper(env.EnvOptions, fp, requirementsPath)
if err != nil {
return err
}
return nil
}
func (env *WindowsPyEnv) ExecutePython(args ...string) (*exec.Cmd, error) {
fp := filepath.Join(env.EnvOptions.ParentPath, "dist/python/install/python.exe")
cmd, err := executeHelper(env.EnvOptions, fp, args...)
if err != nil {
return nil, err
}
return cmd, nil
}
// helper functions
func dependencyHelper(env *PyEnvOptions, fp string, requirementsPath string) error {
if env.Compressed {
if err := env.DecompressDist(); err != nil {
return err
}
}
log.Println("installing python dependencies")
cmd := exec.Command(fp, "install", "-r", requirementsPath)
if err := cmd.Run(); err != nil {
return fmt.Errorf("error installing python dependencies: %v", err)
}
log.Println("installing python dependencies complete")
return nil
}
func executeHelper(env *PyEnvOptions, fp string, args ...string) (*exec.Cmd, error) {
if env.Compressed {
return nil, fmt.Errorf("cannot execute python with a compressed dist")
}
cmd := exec.Command(fp, args...)
return cmd, nil
}