-
Notifications
You must be signed in to change notification settings - Fork 97
De-deduplicate code #704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
De-deduplicate code #704
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package backends | ||
|
|
||
| import ( | ||
| "errors" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| ) | ||
|
Comment on lines
+3
to
+8
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| // ErrPythonNotFound is returned when python3 cannot be found. | ||
| var ErrPythonNotFound = errors.New("python3 not found in PATH") | ||
|
|
||
| // FindPythonPath returns the path to a python3 binary. | ||
| // If customPath is non-empty, it is returned directly. | ||
| // If envDir is non-empty and envDir/bin/python3 exists, that is returned. | ||
| // Otherwise, python3 is looked up in PATH. Returns ErrPythonNotFound if none found. | ||
| func FindPythonPath(customPath, envDir string) (string, error) { | ||
| if customPath != "" { | ||
| return customPath, nil | ||
| } | ||
| if envDir != "" { | ||
| venvPython := filepath.Join(envDir, "bin", "python3") | ||
| if _, err := os.Stat(venvPython); err == nil { | ||
| return venvPython, nil | ||
| } | ||
| } | ||
| systemPython, err := exec.LookPath("python3") | ||
| if err != nil { | ||
| return "", ErrPythonNotFound | ||
| } | ||
| return systemPython, nil | ||
| } | ||
|
|
||
| // NewPythonCmd creates an exec.Cmd that runs python3 with the given arguments. | ||
| // If pythonPath is empty, "python3" is used. | ||
| func NewPythonCmd(pythonPath string, args ...string) *exec.Cmd { | ||
| binary := "python3" | ||
| if pythonPath != "" { | ||
| binary = pythonPath | ||
| } | ||
| return exec.Command(binary, args...) | ||
| } | ||
|
Comment on lines
+36
to
+42
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding a context-aware version of this helper (e.g., |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Preserve the underlying Python resolution error instead of always mapping to ErrStatusNotFound
Now that
FindPythonPathcan return a more specificErrPythonNotFound, consider either propagatingerrdirectly (as in the diffusers/sglang backends) or settingm.status = err.Error()instead ofErrStatusNotFound.Error(). This would preserve the more detailed failure information (e.g., “MLX not available” vs. “python3 not found”), improving diagnosis of misconfigured environments.