-
Notifications
You must be signed in to change notification settings - Fork 0
/
runenv-python-stub.go
97 lines (77 loc) · 1.88 KB
/
runenv-python-stub.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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
func main() {
cmdName := os.Args[0]
if len(os.Args) == 1 {
fmt.Fprintf(os.Stdout, "%q of python run environment was started with no command and arguments", cmdName)
}
const columnWidth = 8
argsReport := strings.Builder{}
fmt.Fprintf(&argsReport, "%*s = %q\n", columnWidth, "cmd", cmdName)
for i, a := range os.Args[1:] {
argStr := fmt.Sprintf("arg[%d]", i)
fmt.Fprintf(&argsReport, "%*s = %q\n", columnWidth, argStr, a)
}
fmt.Fprint(os.Stdout, argsReport.String()+"\n")
if isVenvCreation(os.Args) {
createFakeVenv(os.Args)
fmt.Fprintf(os.Stdout, "venv directory created\n")
return
}
}
func isVenvCreation(args []string) bool {
if len(args) < 3 {
return false
}
return strings.HasSuffix(args[0], "python") &&
args[1] == "-m" && args[2] == "venv"
}
func createFakeVenv(args []string) {
venvDir := args[len(args)-1]
binDir := filepath.Join(venvDir, "bin")
must(
os.MkdirAll(binDir, 0o750),
"python stub: failed to create %q dir", binDir,
)
exec, err := os.Executable()
must(err, "failed to get current executable path")
must(copyFile(exec, filepath.Join(binDir, "python")), "failed to put 'python' into %q", binDir)
must(copyFile(exec, filepath.Join(binDir, "pip")), "failed to put 'pip' into %q", binDir)
}
func copyFile(src, dst string) error {
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()
destFile, err := os.Create(dst)
if err != nil {
return err
}
defer destFile.Close()
_, err = io.Copy(destFile, sourceFile)
if err != nil {
return err
}
sourceInfo, err := os.Stat(src)
if err != nil {
return err
}
err = os.Chmod(dst, sourceInfo.Mode())
if err != nil {
return err
}
return nil
}
func must(err error, msg string, args ...any) {
if err != nil {
args = append(args, err)
panic(fmt.Errorf(msg+": %w", args...))
}
}