Log more when resolving Python (#4185)

* Log more when resolving Python

Users often have problems configuring their Python installations

* Convert if-else ladder to switch statement
* Consolidate Python resolution

Adds additional logging to plugin tasks to
align with the logging that scrapers output.
This commit is contained in:
Maista
2023-11-22 00:04:15 +01:00
committed by GitHub
parent 987fa80786
commit 0dcd58763f
3 changed files with 37 additions and 25 deletions

View File

@@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"io" "io"
"os/exec" "os/exec"
"strings"
"sync" "sync"
stashExec "github.com/stashapp/stash/pkg/exec" stashExec "github.com/stashapp/stash/pkg/exec"
@@ -45,21 +46,17 @@ func (t *rawPluginTask) Start() error {
var cmd *exec.Cmd var cmd *exec.Cmd
if python.IsPythonCommand(command[0]) { if python.IsPythonCommand(command[0]) {
pythonPath := t.serverConfig.GetPythonPath() pythonPath := t.serverConfig.GetPythonPath()
var p *python.Python p, err := python.Resolve(pythonPath)
if pythonPath != "" {
p = python.New(pythonPath)
} else {
p, _ = python.Resolve()
}
if p != nil { if err != nil {
logger.Warnf("%s", err)
} else {
cmd = p.Command(context.TODO(), command[1:]) cmd = p.Command(context.TODO(), command[1:])
} }
// if could not find python, just use the command args as-is
} }
if cmd == nil { if cmd == nil {
// if could not find python, just use the command args as-is
cmd = stashExec.Command(command[0], command[1:]...) cmd = stashExec.Command(command[0], command[1:]...)
} }
@@ -99,6 +96,8 @@ func (t *rawPluginTask) Start() error {
go t.handlePluginStderr(t.plugin.Name, stderr) go t.handlePluginStderr(t.plugin.Name, stderr)
t.cmd = cmd t.cmd = cmd
logger.Debugf("Plugin %s started: %s", t.plugin.Name, strings.Join(cmd.Args, " "))
// send the stdout to the plugin output // send the stdout to the plugin output
go func() { go func() {
defer t.waitGroup.Done() defer t.waitGroup.Done()
@@ -113,6 +112,7 @@ func (t *rawPluginTask) Start() error {
errStr := err.Error() errStr := err.Error()
output.Error = &errStr output.Error = &errStr
} }
logger.Debugf("Plugin %s finished", t.plugin.Name)
t.result = &output t.result = &output
}() }()

View File

@@ -2,9 +2,12 @@ package python
import ( import (
"context" "context"
"fmt"
"os/exec" "os/exec"
stashExec "github.com/stashapp/stash/pkg/exec" stashExec "github.com/stashapp/stash/pkg/exec"
"github.com/stashapp/stash/pkg/fsutil"
"github.com/stashapp/stash/pkg/logger"
) )
type Python string type Python string
@@ -22,19 +25,32 @@ func New(path string) *Python {
// Resolve tries to find the python executable in the system. // Resolve tries to find the python executable in the system.
// It first checks for python3, then python. // It first checks for python3, then python.
// Returns nil and an exec.ErrNotFound error if not found. // Returns nil and an exec.ErrNotFound error if not found.
func Resolve() (*Python, error) { func Resolve(configuredPythonPath string) (*Python, error) {
_, err := exec.LookPath("python3") if configuredPythonPath != "" {
isFile, err := fsutil.FileExists(configuredPythonPath)
switch {
case err == nil && isFile:
logger.Tracef("using configured python path: %s", configuredPythonPath)
return New(configuredPythonPath), nil
case err == nil && !isFile:
logger.Warnf("configured python path is not a file: %s", configuredPythonPath)
case err != nil:
logger.Warnf("unable to use configured python path: %s", err)
}
}
python3, err := exec.LookPath("python3")
if err != nil { if err != nil {
_, err = exec.LookPath("python") python, err := exec.LookPath("python")
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("python executable not in PATH: %s", err)
} }
ret := Python("python") ret := Python(python)
return &ret, nil return &ret, nil
} }
ret := Python("python3") ret := Python(python3)
return &ret, nil return &ret, nil
} }

View File

@@ -38,21 +38,17 @@ func (s *scriptScraper) runScraperScript(ctx context.Context, inString string, o
var cmd *exec.Cmd var cmd *exec.Cmd
if python.IsPythonCommand(command[0]) { if python.IsPythonCommand(command[0]) {
pythonPath := s.globalConfig.GetPythonPath() pythonPath := s.globalConfig.GetPythonPath()
var p *python.Python p, err := python.Resolve(pythonPath)
if pythonPath != "" {
p = python.New(pythonPath) if err != nil {
logger.Warnf("%s", err)
} else { } else {
p, _ = python.Resolve() cmd = p.Command(context.TODO(), command[1:])
} }
if p != nil {
cmd = p.Command(ctx, command[1:])
}
// if could not find python, just use the command args as-is
} }
if cmd == nil { if cmd == nil {
// if could not find python, just use the command args as-is
cmd = stashExec.Command(command[0], command[1:]...) cmd = stashExec.Command(command[0], command[1:]...)
} }