Skip to content

Commit

Permalink
Improved the Python filter errors. Close #144
Browse files Browse the repository at this point in the history
  • Loading branch information
Nusiq committed Mar 4, 2022
1 parent 715ab31 commit 7142c02
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
67 changes: 33 additions & 34 deletions regolith/filter_python.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,19 @@ func (f *PythonFilter) Run(absoluteLocation string) error {
defer Logger.Debugf("Executed in %s", time.Since(start))

// Run filter
// command is a list of strings that can possibly run python (it's python3
// on some OSs)
command := []string{"python", "python3"}
pythonCommand, err := findPython()
if err != nil {
return PassError(err)
}
scriptPath := filepath.Join(absoluteLocation, f.Definition.Script)
if needsVenv(filepath.Dir(scriptPath)) {
venvPath, err := f.Definition.resolveVenvPath()
if err != nil {
return WrapError(err, "Failed to resolve venv path.")
}
Logger.Debug("Running Python filter using venv: ", venvPath)
command = []string{
filepath.Join(venvPath, venvScriptsPath, "python"+exeSuffix)}
pythonCommand = filepath.Join(
venvPath, venvScriptsPath, "python"+exeSuffix)
}
var args []string
if len(f.Settings) == 0 {
Expand All @@ -67,14 +68,8 @@ func (f *PythonFilter) Run(absoluteLocation string) error {
[]string{"-u", scriptPath, string(jsonSettings)},
f.Arguments...)
}
var err error
for _, c := range command {
err = RunSubProcess(
c, args, absoluteLocation, GetAbsoluteWorkingDirectory())
if err == nil {
return nil
}
}
err = RunSubProcess(
pythonCommand, args, absoluteLocation, GetAbsoluteWorkingDirectory())
if err != nil {
return WrapError(err, "Failed to run Python script.")
}
Expand Down Expand Up @@ -113,13 +108,15 @@ func (f *PythonFilterDefinition) InstallDependencies(parent *RemoteFilterDefinit
return WrapError(err, "Failed to resolve venv path.")
}
Logger.Info("Creating venv...")
// it's sometimes python3 on some OSs
for _, c := range []string{"python", "python3"} {
err = RunSubProcess(
c, []string{"-m", "venv", venvPath}, filterPath, "")
if err == nil {
break
}
pythonCommand, err := findPython()
if err != nil {
return PassError(err)
}
// Create the "venv"
err = RunSubProcess(
pythonCommand, []string{"-m", "venv", venvPath}, filterPath, "")
if err != nil {
return WrapError(err, "Failed to create venv.")
}
Logger.Info("Installing pip dependencies...")
err = RunSubProcess(
Expand All @@ -137,22 +134,11 @@ func (f *PythonFilterDefinition) InstallDependencies(parent *RemoteFilterDefinit
}

func (f *PythonFilterDefinition) Check() error {
python := ""
var err error
for _, c := range []string{"python", "python3"} {
_, err = exec.LookPath(c)
if err == nil {
python = c
break
}
}
pythonCommand, err := findPython()
if err != nil {
return WrapError(
nil, // the error woud say something like "python3" doesn't exist but we don't care
"Python not found, download and install it from "+
"https://www.python.org/downloads/")
return PassError(err)
}
cmd, err := exec.Command(python, "--version").Output()
cmd, err := exec.Command(pythonCommand, "--version").Output()
if err != nil {
return WrapError(err, "Python version check failed.")
}
Expand Down Expand Up @@ -189,3 +175,16 @@ func needsVenv(filterPath string) bool {
}
return false
}

func findPython() (string, error) {
var err error
for _, c := range []string{"python", "python3"} {
_, err = exec.LookPath(c)
if err == nil {
return c, nil
}
}
return "", WrappedError(
"Python not found, download and install it from " +
"https://www.python.org/downloads/")
}
3 changes: 1 addition & 2 deletions regolith/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ func RunProfile(profileName string) error {
path, _ := filepath.Abs(".")
err := filter.Run(path)
if err != nil {
// TODO: This needs to print which filter failed
return WrapErrorf(err, " failed")
return WrapError(err, "Failed to run filter.")
}
}

Expand Down

0 comments on commit 7142c02

Please sign in to comment.