-
Notifications
You must be signed in to change notification settings - Fork 35
/
venv.go
261 lines (213 loc) · 6.22 KB
/
venv.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Functions and types for interacting with virtual environments
package main
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
var (
venvCommandTimeout = 2 * time.Hour // 2 hours timeout
)
// VenvConfig defines a Python Virtual Environment.
type VenvConfig struct {
Path string // path to the virtualenv root
Python string // path to the desired Python installation
}
// Takes a VenvConfig and will create a new virtual environment.
func makeVenv(cfg VenvConfig) error {
pythonVersion, err := getPythonVersion(cfg.Python)
if err != nil {
return errors.Wrap(err, "unable to determine Python version")
}
logrus.Debugln("Detected Python version:", pythonVersion)
// venv was introduced in python version 3.3
useVenv := pythonVersionAtLeast(pythonVersion, 3, 3)
logrus.Debugln("Use venv:", useVenv)
var cmd *exec.Cmd
if useVenv {
cmd = exec.Command(cfg.Python, "-m", "venv", cfg.Path)
} else {
venvExecutable, err := exec.LookPath("virtualenv")
if err != nil {
return errors.Wrap(err, "virtualenv not found in path")
}
cmd = exec.Command(venvExecutable, "--python", cfg.Python, cfg.Path)
}
err = cmd.Run()
if err != nil {
failedCommandLogger(cmd)
return errors.Wrap(err, "unable to create virtual environment")
}
return nil
}
// pythonVersionAtLeast checks if the Python version is at least major.minor.
func pythonVersionAtLeast(version string, major int, minor int) bool {
parts := strings.Split(version, ".")
if len(parts) < 2 {
return false
}
majorVersion, err := strconv.Atoi(parts[0])
if err != nil {
return false
}
minorVersion, err := strconv.Atoi(parts[1])
if err != nil {
return false
}
if majorVersion > major {
return true
}
if majorVersion == major && minorVersion >= minor {
return true
}
return false
}
// getPythonVersion returns the Python version as a string.
func getPythonVersion(python string) (string, error) {
cmd := exec.Command(python, "--version")
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out // Some versions output version info to stderr
err := cmd.Run()
if err != nil {
return "", errors.Wrap(err, "unable to execute Python version command")
}
versionOutput := strings.TrimSpace(out.String())
parts := strings.Fields(versionOutput)
if len(parts) != 2 {
return "", errors.New("unexpected output from Python version command")
}
return parts[1], nil
}
// Ensure ensures that a virtual environment exists, if not, it attempts to create it
func (c VenvConfig) Ensure() error {
_, err := os.Stat(c.Path)
if os.IsNotExist(err) {
err := makeVenv(c)
if err != nil {
return err
}
}
return nil
}
// Update updates the virtualenv for the given config with the specified requirements file
func (c VenvConfig) Update(requirementsFile string) error {
vCmd := VenvCommand{
Config: c,
Binary: "pip",
Args: []string{"install", "-r", requirementsFile},
}
venvCommandOutput := vCmd.Run()
if venvCommandOutput.Error != nil {
return errors.Wrap(venvCommandOutput.Error, "unable to update virtualenv")
}
return nil
}
// VenvCommand enables you to run a system command in a virtualenv.
type VenvCommand struct {
Config VenvConfig
Binary string // path to the binary under $venv/bin
Args []string // args to pass to the command that is called
Cwd string // Directory to change to, if needed
Env []string // Additions to the runtime environment
StreamOutput bool // Whether or not the application should stream output stdout/stderr
}
type VenvCommandRunOutput struct {
Stdout string
Stderr string
Error error
Exitcode int
}
func streamOutput(stream io.ReadCloser, outString *string) error {
var buff bytes.Buffer
scanner := bufio.NewScanner(io.TeeReader(stream, &buff))
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
m := scanner.Text()
fmt.Println(m)
}
*outString = buff.String()
return nil
}
// Run will execute the command described in VenvCommand.
//
// The strings returned are Stdout/Stderr.
func (c VenvCommand) Run() VenvCommandRunOutput {
ctx, cancel := context.WithTimeout(context.Background(), venvCommandTimeout)
CommandOutput := VenvCommandRunOutput{
Stdout: "",
Stderr: "",
Error: nil,
Exitcode: -1,
}
defer cancel() // The cancel should be deferred so resources are cleaned up
path, ok := os.LookupEnv("PATH")
if !ok {
CommandOutput.Error = errors.New("Unable to lookup the $PATH env variable")
return CommandOutput
}
// Updating $PATH variable to include the venv path
venvPath := filepath.Join(c.Config.Path, "bin")
if !strings.Contains(path, venvPath) {
newVenvPath := fmt.Sprintf("%s:%s", filepath.Join(c.Config.Path, "bin"), path)
logrus.Debugln("PATH: ", newVenvPath)
os.Setenv("PATH", newVenvPath)
}
cmd := exec.CommandContext(
ctx,
filepath.Join(c.Config.Path, "bin", c.Binary),
c.Args...,
)
if c.Cwd != "" {
cmd.Dir = c.Cwd
}
cmd.Env = append(os.Environ(), c.Env...)
if c.StreamOutput {
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
if err := cmd.Start(); err != nil {
CommandOutput.Error = errors.Wrap(err, "unable to start command")
return CommandOutput
}
go streamOutput(stdout, &CommandOutput.Stdout)
go streamOutput(stderr, &CommandOutput.Stderr)
if err := cmd.Wait(); err != nil {
exitError, _ := err.(*exec.ExitError)
CommandOutput.Error = errors.Wrap(err, "unable to complete command")
CommandOutput.Exitcode = exitError.ExitCode()
return CommandOutput
}
CommandOutput.Exitcode = 0
return CommandOutput
}
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
logrus.Debugln("Running venv command: ", cmd.Args)
err := cmd.Run()
CommandOutput.Stderr = stderr.String()
CommandOutput.Stdout = stdout.String()
if ctx.Err() == context.DeadlineExceeded {
CommandOutput.Error = errors.Wrap(err, "Execution timed out")
return CommandOutput
} else if err != nil {
failedCommandLogger(cmd)
if exitError, ok := err.(*exec.ExitError); ok {
CommandOutput.Exitcode = exitError.ExitCode()
}
CommandOutput.Error = errors.Wrap(err, "unable to complete command")
return CommandOutput
}
CommandOutput.Exitcode = 0
return CommandOutput
}