Skip to content

Commit

Permalink
Log more when resolving Python (#4185)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Maista6969 authored Nov 21, 2023
1 parent 987fa80 commit 0dcd587
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
18 changes: 9 additions & 9 deletions pkg/plugin/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"os/exec"
"strings"
"sync"

stashExec "github.com/stashapp/stash/pkg/exec"
Expand Down Expand Up @@ -45,21 +46,17 @@ func (t *rawPluginTask) Start() error {
var cmd *exec.Cmd
if python.IsPythonCommand(command[0]) {
pythonPath := t.serverConfig.GetPythonPath()
var p *python.Python
if pythonPath != "" {
p = python.New(pythonPath)
} else {
p, _ = python.Resolve()
}
p, err := python.Resolve(pythonPath)

if p != nil {
if err != nil {
logger.Warnf("%s", err)
} else {
cmd = p.Command(context.TODO(), command[1:])
}

// if could not find python, just use the command args as-is
}

if cmd == nil {
// if could not find python, just use the command args as-is
cmd = stashExec.Command(command[0], command[1:]...)
}

Expand Down Expand Up @@ -99,6 +96,8 @@ func (t *rawPluginTask) Start() error {
go t.handlePluginStderr(t.plugin.Name, stderr)
t.cmd = cmd

logger.Debugf("Plugin %s started: %s", t.plugin.Name, strings.Join(cmd.Args, " "))

// send the stdout to the plugin output
go func() {
defer t.waitGroup.Done()
Expand All @@ -113,6 +112,7 @@ func (t *rawPluginTask) Start() error {
errStr := err.Error()
output.Error = &errStr
}
logger.Debugf("Plugin %s finished", t.plugin.Name)

t.result = &output
}()
Expand Down
28 changes: 22 additions & 6 deletions pkg/python/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package python

import (
"context"
"fmt"
"os/exec"

stashExec "github.com/stashapp/stash/pkg/exec"
"github.com/stashapp/stash/pkg/fsutil"
"github.com/stashapp/stash/pkg/logger"
)

type Python string
Expand All @@ -22,19 +25,32 @@ func New(path string) *Python {
// Resolve tries to find the python executable in the system.
// It first checks for python3, then python.
// Returns nil and an exec.ErrNotFound error if not found.
func Resolve() (*Python, error) {
_, err := exec.LookPath("python3")
func Resolve(configuredPythonPath string) (*Python, error) {
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 {
_, err = exec.LookPath("python")
python, err := exec.LookPath("python")
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
}

ret := Python("python3")
ret := Python(python3)
return &ret, nil
}

Expand Down
16 changes: 6 additions & 10 deletions pkg/scraper/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,17 @@ func (s *scriptScraper) runScraperScript(ctx context.Context, inString string, o
var cmd *exec.Cmd
if python.IsPythonCommand(command[0]) {
pythonPath := s.globalConfig.GetPythonPath()
var p *python.Python
if pythonPath != "" {
p = python.New(pythonPath)
} else {
p, _ = python.Resolve()
}
p, err := python.Resolve(pythonPath)

if p != nil {
cmd = p.Command(ctx, command[1:])
if err != nil {
logger.Warnf("%s", err)
} else {
cmd = p.Command(context.TODO(), command[1:])
}

// if could not find python, just use the command args as-is
}

if cmd == nil {
// if could not find python, just use the command args as-is
cmd = stashExec.Command(command[0], command[1:]...)
}

Expand Down

0 comments on commit 0dcd587

Please sign in to comment.