-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyproc.go
476 lines (405 loc) · 12.7 KB
/
pyproc.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package jumpboot
import (
"bytes"
"embed"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
"path"
"strconv"
"syscall"
"text/template"
"time"
)
// The file descriptor is passed as an extra file, so it will be after stderr
//
//go:embed scripts/bootstrap.py
var primaryBootstrapScriptTemplate string
//go:embed scripts/secondaryBootstrapScript.py
var secondaryBootstrapScriptTemplate string
//go:embed packages/jumpboot/*.py
var jumpboot_package embed.FS
// PythonProcess represents a running Python process with its I/O pipes
type PythonProcess struct {
Cmd *exec.Cmd
Stdin io.WriteCloser
Stdout io.ReadCloser
Stderr io.ReadCloser
PipeIn *os.File
PipeOut *os.File
script io.WriteCloser // For writing the secondary bootstrap script
}
// Module represents a Python module
type Module struct {
// Name of the module
Name string
// Path to the module
Path string
// Base64 encoded source code of the module
Source string
}
// Package represents a Python package
type Package struct {
// Name of the package
Name string
// Path to the package
Path string
// Modules in the package
Modules []Module
// Subpackages in the package
Packages []Package
}
// PythonProgram represents a Python program with its main module and supporting packages and modules
type PythonProgram struct {
Name string
Path string
Program Module
Packages []Package
Modules []Module
PipeIn int
PipeOut int
// DebugPort - setting this to a non-zero value will start the debugpy server on the specified port
// and wait for the debugger to attach before running the program in the bootstrap script
DebugPort int
BreakOnStart bool
KVPairs map[string]interface{}
}
// Data struct to hold the pipe number
type TemplateData struct {
PipeNumber int
}
// NewModuleFromPath creates a new module from a file path
func NewModuleFromPath(name, path string) (*Module, error) {
// load the source file from the path
source, err := os.ReadFile(path)
if err != nil {
return nil, err
}
// base64 encode the source
encoded := base64.StdEncoding.EncodeToString(source)
return &Module{
Name: name,
Path: path,
Source: encoded,
}, nil
}
// NewModuleFromString creates a new module from a string
func NewModuleFromString(name, original_path string, source string) *Module {
// Trim the "packages/" prefix if it exists
path := original_path
// if filepath.HasPrefix(path, "packages/") {
// path = filepath.Join(filepath.Base(filepath.Dir(path)), filepath.Base(path))
// }
// base64 encode the source
encoded := base64.StdEncoding.EncodeToString([]byte(source))
return &Module{
Name: name,
Source: encoded,
Path: path,
}
}
// NewPackage creates a new package from a collection of modules
func NewPackage(name, path string, modules []Module) *Package {
return &Package{
Name: name,
Path: path,
Modules: modules,
}
}
// fsDirHasInitPy returns true if the fs directory contains a __init__.py file
func fsDirHasInitPy(fs embed.FS, path string) bool {
// read the directory. If the directory contains a __init__.py file, then it is a package
entries, err := fs.ReadDir(path)
if err != nil {
return false
}
for _, entry := range entries {
if entry.Name() == "__init__.py" {
return true
}
}
return false
}
func newPackageFromFS(name string, sourcepath string, rootpath string, fs embed.FS) (*Package, error) {
retv := &Package{
Name: name,
Path: rootpath,
}
entries, err := fs.ReadDir(rootpath)
if err != nil {
return nil, err
}
for _, entry := range entries {
fpath := path.Join(rootpath, entry.Name())
if entry.IsDir() {
subpackage, err := newPackageFromFS(entry.Name(), sourcepath, fpath, fs)
if err != nil {
continue
}
retv.Packages = append(retv.Packages, *subpackage)
} else {
// Use the fpath directly, which now uses forward slashes
file, err := fs.Open(fpath)
if err != nil {
return nil, err
}
defer file.Close()
source, err := io.ReadAll(file)
if err != nil {
return nil, err
}
if path.Ext(entry.Name()) != ".py" {
continue
} else {
module := NewModuleFromString(entry.Name(), fpath, string(source))
retv.Modules = append(retv.Modules, *module)
}
}
}
return retv, nil
}
// New Package from an embed.FS containing the package structure and source files
func NewPackageFromFS(name string, sourcepath string, rootpath string, fs embed.FS) (*Package, error) {
// the embedded filesystem should be a directory
return newPackageFromFS(name, sourcepath, rootpath, fs)
}
func procTemplate(templateStr string, data interface{}) string {
// Parse the template
tmpl, err := template.New("pythonTemplate").Parse(templateStr)
if err != nil {
log.Fatalf("Error parsing template: %v", err)
}
// Execute the template with the data
var result bytes.Buffer
err = tmpl.Execute(&result, data)
if err != nil {
log.Fatalf("Error executing template: %v", err)
}
return result.String()
}
func (env *Environment) NewPythonProcessFromProgram(program *PythonProgram, environment_vars map[string]string, extrafiles []*os.File, debug bool, args ...string) (*PythonProcess, []byte, error) {
// create the jumpboot package
jumpboot_package, err := newPackageFromFS("jumpboot", "jumpboot", "packages/jumpboot", jumpboot_package)
if err != nil {
return nil, nil, err
}
// prepend the jumpboot package to the list of packages
program.Packages = append([]Package{*jumpboot_package}, program.Packages...)
// Create two pipes for the bootstrap and the program data
// these are closed after the data is written
reader_bootstrap, writer_bootstrap, err := os.Pipe()
if err != nil {
return nil, nil, err
}
reader_program, writer_program, err := os.Pipe()
if err != nil {
return nil, nil, err
}
// Create two pipes for the primary input and output of the script
// these are used to communicate with the primary bootstrap script
pipein_reader_primary, pipein_writer_primary, err := os.Pipe()
if err != nil {
return nil, nil, err
}
pipeout_reader_primary, pipeout_writer_primary, err := os.Pipe()
if err != nil {
return nil, nil, err
}
// get the file descriptor for the bootstrap script
reader_bootstrap_fd := reader_bootstrap.Fd()
primaryBootstrapScript := procTemplate(primaryBootstrapScriptTemplate, TemplateData{PipeNumber: int(reader_bootstrap_fd)})
// Create the command with the primary bootstrap script
cmd := exec.Command(env.PythonPath)
// Pass both file descriptors using ExtraFiles
// this will return a list of strings with the file descriptors
extradescriptors := setExtraFiles(cmd, append([]*os.File{pipein_writer_primary, pipeout_reader_primary, reader_bootstrap, reader_program}, extrafiles...))
// truncate pipein_writer_primary, pipeout_reader_primary from extradescriptors
// these are available as PipeIn and PipeOut in the PythonProgram struct
program.PipeOut, _ = strconv.Atoi(extradescriptors[0])
program.PipeIn, _ = strconv.Atoi(extradescriptors[1])
extradescriptors = extradescriptors[2:]
// At this point, cmd.Args will contain just the python path. We can now append the "-c" flag and the primary bootstrap script
cmd.Args = append(cmd.Args, "-u", "-c", primaryBootstrapScript)
// append the count of extra files to the command arguments as a string
cmd.Args = append(cmd.Args, fmt.Sprintf("%d", len(extradescriptors)))
// append the extra file descriptors to the command arguments
cmd.Args = append(cmd.Args, extradescriptors...)
// append the program arguments to the command arguments
cmd.Args = append(cmd.Args, args...)
// Set environment variables
cmd.Env = os.Environ()
for key, value := range environment_vars {
cmd.Env = append(cmd.Env, key+"="+value)
}
// Create pipes for the input, output, and error of the script
stdinPipe, err := cmd.StdinPipe()
if err != nil {
return nil, nil, err
}
stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, err
}
stderrPipe, err := cmd.StderrPipe()
if err != nil {
return nil, nil, err
}
// Prepare the program data
programData, err := json.Marshal(program)
if err != nil {
return nil, nil, err
}
// Start the command
if err := cmd.Start(); err != nil {
return nil, nil, err
}
// Write the secondary bootstrap script and program data to separate pipes
go func() {
defer writer_bootstrap.Close()
secondaryBootstrapScript := procTemplate(secondaryBootstrapScriptTemplate, TemplateData{PipeNumber: int(reader_program.Fd())})
io.WriteString(writer_bootstrap, secondaryBootstrapScript)
}()
go func() {
defer writer_program.Close()
writer_program.Write(programData)
}()
pyProcess := &PythonProcess{
Cmd: cmd,
Stdin: stdinPipe,
Stdout: stdoutPipe,
Stderr: stderrPipe,
PipeIn: pipein_reader_primary,
PipeOut: pipeout_writer_primary,
}
// Set up signal handling
setupSignalHandler(pyProcess)
return pyProcess, programData, nil
}
// NewPythonProcessFromString starts a Python script from a string with the given arguments.
// It returns a PythonProcess struct containing the command and I/O pipes.
// It ensures that the child process is killed if the parent process is killed.
func (env *Environment) NewPythonProcessFromString(script string, environment_vars map[string]string, extrafiles []*os.File, debug bool, args ...string) (*PythonProcess, error) {
// Create a pipe for the secondary bootstrap script
// we'll write the script to the writer
reader, writer, err := os.Pipe()
if err != nil {
return nil, err
}
// Create two pipes for the primary input and output of the script
pipein_reader_primary, pipein_writer_primary, err := os.Pipe()
if err != nil {
return nil, err
}
pipeout_reader_primary, pipeout_writer_primary, err := os.Pipe()
if err != nil {
return nil, err
}
// Create the command with the bootstrap script
// We want stdin/stdout to unbuffered (-u) and to run the bootstrap script
// The "-c" flag is used to pass the script as an argument and terminates the python option list.
bootloader := procTemplate(primaryBootstrapScriptTemplate, TemplateData{PipeNumber: int(reader.Fd())})
fullArgs := append([]string{"-u", "-c", bootloader}, args...)
cmd := exec.Command(env.PythonPath, fullArgs...)
// Pass the file descriptor using ExtraFiles
// prepend our reader to the list of extra files so it is always the first file descriptor
extrafiles = append([]*os.File{reader, pipein_writer_primary, pipeout_reader_primary}, extrafiles...)
setExtraFiles(cmd, extrafiles)
// set it's environment variables as our environment variables
cmd.Env = os.Environ()
// set the environment variables if they are provided
for key, value := range environment_vars {
cmd.Env = append(cmd.Env, key+"="+value)
}
// Create pipes for the input, output, and error of the script
stdinPipe, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
stderrPipe, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
// Start the command
if err := cmd.Start(); err != nil {
return nil, err
}
// Write the main script to the pipe
go func() {
// Close the writer when the function returns
// Python will not run the bootstrap script until the writer is closed
defer writer.Close()
io.WriteString(writer, script)
}()
pyProcess := &PythonProcess{
Cmd: cmd,
Stdin: stdinPipe,
Stdout: stdoutPipe,
Stderr: stderrPipe,
PipeIn: pipein_reader_primary,
PipeOut: pipeout_writer_primary,
}
// Set up signal handling
setupSignalHandler(pyProcess)
return pyProcess, nil
}
// Wait waits for the Python process to exit and returns an error if it was killed
func (pp *PythonProcess) Wait() error {
err := pp.Cmd.Wait()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
if exitErr.ExitCode() == -1 {
// The child process was killed
return errors.New("child process was killed")
}
}
return err
}
return nil
}
// Terminate gracefully stops the Python process
func (pp *PythonProcess) Terminate() error {
if pp.Cmd.Process == nil {
return nil // Process hasn't started or has already finished
}
// Try to terminate gracefully first
err := pp.Cmd.Process.Signal(syscall.SIGTERM)
if err != nil {
return err
}
// Wait for the process to exit
done := make(chan error, 1)
go func() {
done <- pp.Cmd.Wait()
}()
// Wait for the process to exit or force kill after timeout
select {
case <-time.After(5 * time.Second):
// Force kill if it doesn't exit within 5 seconds
err = pp.Cmd.Process.Kill()
if err != nil {
return err
}
<-done // Wait for the process to be killed
case err = <-done:
// Process exited before timeout
}
return err
}
func setupSignalHandler(pp *PythonProcess) {
signalChan := make(chan os.Signal, 1)
setSignalsForChannel(signalChan)
go func() {
<-signalChan
// Terminate the child process when a signal is received
pp.Terminate()
}()
}