forked from kluctl/go-embed-python
/
embedded_python.go
91 lines (75 loc) · 2.17 KB
/
embedded_python.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
package python
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/joe-p/go-embed-python/embed_util"
"github.com/joe-p/go-embed-python/python/internal/data"
)
type EmbeddedPython struct {
e *embed_util.EmbeddedFiles
pythonPath []string
}
// NewEmbeddedPython creates a new EmbeddedPython instance. The embedded source code and python binaries are
// extracted on demand using the given name as the base for the temporary directory. You should ensure that the chosen
// name does collide with other consumers of this library.
func NewEmbeddedPython(name string) (*EmbeddedPython, error) {
e, err := embed_util.NewEmbeddedFiles(data.Data, fmt.Sprintf("python-%s", name))
if err != nil {
return nil, err
}
return &EmbeddedPython{
e: e,
}, nil
}
func NewEmbeddedPythonWithTmpDir(tmpDir string, withHashInDir bool) (*EmbeddedPython, error) {
e, err := embed_util.NewEmbeddedFilesWithTmpDir(data.Data, tmpDir, withHashInDir)
if err != nil {
return nil, err
}
return &EmbeddedPython{
e: e,
}, nil
}
func (ep *EmbeddedPython) Cleanup() error {
return ep.e.Cleanup()
}
func (ep *EmbeddedPython) GetExtractedPath() string {
return ep.e.GetExtractedPath()
}
func (ep *EmbeddedPython) GetBinPath() string {
if runtime.GOOS == "windows" {
return ep.GetExtractedPath()
} else {
return filepath.Join(ep.GetExtractedPath(), "bin")
}
}
func (ep *EmbeddedPython) GetExePath() string {
suffix := ""
if runtime.GOOS == "windows" {
suffix = ".exe"
} else {
suffix = "3"
}
return filepath.Join(ep.GetBinPath(), "python"+suffix)
}
func (ep *EmbeddedPython) AddPythonPath(p string) {
ep.pythonPath = append(ep.pythonPath, p)
}
func (ep *EmbeddedPython) PythonCmd(args ...string) *exec.Cmd {
return ep.PythonCmd2(args)
}
func (ep *EmbeddedPython) PythonCmd2(args []string) *exec.Cmd {
exePath := ep.GetExePath()
cmd := exec.Command(exePath, args...)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintf("PYTHONHOME=%s", ep.GetExtractedPath()))
if len(ep.pythonPath) != 0 {
pythonPathEnv := fmt.Sprintf("PYTHONPATH=%s", strings.Join(ep.pythonPath, string(os.PathListSeparator)))
cmd.Env = append(cmd.Env, pythonPathEnv)
}
return cmd
}