-
Notifications
You must be signed in to change notification settings - Fork 1
/
local.go
154 lines (126 loc) · 3.92 KB
/
local.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
package python
import (
"bufio"
"context"
"fmt"
"io"
"os"
"os/exec"
"runtime"
"strings"
"github.com/bruin-data/bruin/pkg/executor"
"github.com/bruin-data/bruin/pkg/git"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)
const WINDOWS = "windows"
type cmd interface {
Run(ctx context.Context, repo *git.Repo, command *command) error
}
type requirementsInstaller interface {
EnsureVirtualEnvExists(ctx context.Context, repo *git.Repo, requirementsTxt string) (string, error)
}
type localPythonRunner struct {
cmd cmd
requirementsInstaller requirementsInstaller
pathToPython string
}
func log(ctx context.Context, message string) {
if ctx.Value(executor.KeyPrinter) == nil {
return
}
if !strings.HasSuffix(message, "\n") {
message += "\n"
}
writer := ctx.Value(executor.KeyPrinter).(io.Writer)
_, _ = writer.Write([]byte(message))
}
func (l *localPythonRunner) Run(ctx context.Context, execCtx *executionContext) error {
pythonCommandForScript := fmt.Sprintf("%s -u -m %s", l.pathToPython, execCtx.module)
noDependencyCommand := &command{
Name: Shell,
Args: []string{ShellSubcommandFlag, pythonCommandForScript},
EnvVars: execCtx.envVariables,
}
if execCtx.requirementsTxt == "" {
return l.cmd.Run(ctx, execCtx.repo, noDependencyCommand)
}
log(ctx, "requirements.txt found, setting up the isolated environment...")
log(ctx, "requirements.txt path: "+execCtx.requirementsTxt)
depsPath, err := l.requirementsInstaller.EnsureVirtualEnvExists(ctx, execCtx.repo, execCtx.requirementsTxt)
if err != nil {
return err
}
if depsPath == "" {
log(ctx, "requirements.txt is empty, executing the script right away...")
return l.cmd.Run(ctx, execCtx.repo, noDependencyCommand)
}
// if there's a virtualenv, use the Python there explicitly, otherwise aliases change the runtime used
pythonCommandForScript = fmt.Sprintf("%s/%s/%s -u -m %s", depsPath, VirtualEnvBinaryFolder, DefaultPythonExecutable, execCtx.module)
fullCommand := fmt.Sprintf("%s/%s/activate && echo 'activated virtualenv' && %s", depsPath, VirtualEnvBinaryFolder, pythonCommandForScript)
if runtime.GOOS != WINDOWS {
fullCommand = ". " + fullCommand
}
return l.cmd.Run(ctx, execCtx.repo, &command{
Name: Shell,
Args: []string{ShellSubcommandFlag, fullCommand},
EnvVars: execCtx.envVariables,
})
}
type commandRunner struct{}
type command struct {
Name string
Args []string
EnvVars map[string]string
}
func (l *commandRunner) Run(ctx context.Context, repo *git.Repo, command *command) error {
cmd := exec.Command(command.Name, command.Args...) //nolint:gosec
cmd.Dir = repo.Path
cmd.Env = make([]string, len(command.EnvVars))
for k, v := range command.EnvVars {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
}
var output io.Writer = os.Stdout
if ctx.Value(executor.KeyPrinter) != nil {
output = ctx.Value(executor.KeyPrinter).(io.Writer)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return errors.Wrap(err, "failed to get stdout")
}
stderr, err := cmd.StderrPipe()
if err != nil {
return errors.Wrap(err, "failed to get stdout")
}
wg := new(errgroup.Group)
wg.Go(func() error { return consumePipe(stdout, output) })
wg.Go(func() error { return consumePipe(stderr, output) })
err = cmd.Start()
if err != nil {
return errors.Wrap(err, "failed to start command")
}
res := cmd.Wait()
if res != nil {
return res
}
err = wg.Wait()
if err != nil {
return errors.Wrap(err, "failed to consume pipe")
}
return nil
}
func consumePipe(pipe io.Reader, output io.Writer) error {
scanner := bufio.NewScanner(pipe)
for scanner.Scan() {
// the size of the slice here is important, the added 4 at the end includes the 3 bytes for the prefix and the 1 byte for the newline
msg := make([]byte, len(scanner.Bytes())+4)
copy(msg, ">> ")
copy(msg[3:], scanner.Bytes())
msg[len(msg)-1] = '\n'
_, err := output.Write(msg)
if err != nil {
return err
}
}
return nil
}