-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from kluctl/feat-external-python
feat: Support non-embedded python via the same interface
- Loading branch information
Showing
12 changed files
with
181 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package python | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
type Python struct { | ||
pythonHome string | ||
pythonPath []string | ||
} | ||
|
||
type PythonOpt func(o *Python) | ||
|
||
func WithPythonHome(home string) PythonOpt { | ||
return func(o *Python) { | ||
o.pythonHome = home | ||
} | ||
} | ||
|
||
func NewPython(opts ...PythonOpt) *Python { | ||
ep := &Python{} | ||
|
||
for _, o := range opts { | ||
o(ep) | ||
} | ||
|
||
return ep | ||
} | ||
|
||
func (ep *Python) GetExeName() string { | ||
suffix := "" | ||
if runtime.GOOS == "windows" { | ||
suffix = ".exe" | ||
} else { | ||
suffix = "3" | ||
} | ||
return "python" + suffix | ||
} | ||
|
||
func (ep *Python) GetExePath() (string, error) { | ||
if ep.pythonHome == "" { | ||
p, err := exec.LookPath(ep.GetExeName()) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to determine %s path: %w", ep.GetExeName(), err) | ||
} | ||
return p, nil | ||
} else { | ||
var p string | ||
if runtime.GOOS == "windows" { | ||
p = filepath.Join(ep.pythonHome, ep.GetExeName()) | ||
} else { | ||
p = filepath.Join(ep.pythonHome, "bin", ep.GetExeName()) | ||
} | ||
if _, err := os.Stat(p); err != nil { | ||
return "", fmt.Errorf("failed to determine %s path: %w", ep.GetExeName(), err) | ||
} | ||
return p, nil | ||
} | ||
} | ||
|
||
func (ep *Python) AddPythonPath(p string) { | ||
ep.pythonPath = append(ep.pythonPath, p) | ||
} | ||
|
||
func (ep *Python) PythonCmd(args ...string) (*exec.Cmd, error) { | ||
return ep.PythonCmd2(args) | ||
} | ||
|
||
func (ep *Python) PythonCmd2(args []string) (*exec.Cmd, error) { | ||
exePath, err := ep.GetExePath() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cmd := exec.Command(exePath, args...) | ||
cmd.Env = os.Environ() | ||
|
||
if ep.pythonHome != "" { | ||
cmd.Env = append(cmd.Env, fmt.Sprintf("PYTHONHOME=%s", ep.pythonHome)) | ||
} | ||
|
||
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, nil | ||
} |
Oops, something went wrong.