diff --git a/python/pep518.go b/python/pep518.go index b9d4154..e7fd083 100644 --- a/python/pep518.go +++ b/python/pep518.go @@ -1,13 +1,18 @@ package python import ( - "io/ioutil" + "os" "path/filepath" "strings" + + log "github.com/sirupsen/logrus" ) func pep518Proc(path string) { tomlContent := readPyProjectToml(path) + if tomlContent == "" { + return + } if isUv(path) { uvProc(path) @@ -17,12 +22,11 @@ func pep518Proc(path string) { } func readPyProjectToml(path string) string { - // read the whole file at once - b, err := ioutil.ReadFile(filepath.Join(path, "pyproject.toml")) + b, err := os.ReadFile(filepath.Join(path, "pyproject.toml")) if err != nil { - panic(err) + log.Error(err.Error()) + return "" } - return string(b) } diff --git a/python/pip.go b/python/pip.go index 76648af..f9e6242 100644 --- a/python/pip.go +++ b/python/pip.go @@ -1,7 +1,6 @@ package python import ( - "fmt" "os/exec" "path/filepath" @@ -11,14 +10,12 @@ import ( func pipProc(path string) { log.Info("Found 'requirements.txt'. Creating virtual environment using 'pip' & 'venv' module.") - var cmd *exec.Cmd - // create virtual env - cmd = exec.Command("python3", "-m", "venv", ".venv") + cmd := exec.Command("python3", "-m", "venv", ".venv") cmd.Dir = path out, err := cmd.CombinedOutput() if err != nil { - log.Error(fmt.Sprintf("%s", err.Error())) + log.Error(err.Error()) } log.Debug(string(out)) @@ -27,7 +24,7 @@ func pipProc(path string) { cmd.Dir = path out, err = cmd.CombinedOutput() if err != nil { - log.Error(fmt.Sprintf("%s", err.Error())) + log.Error(err.Error()) } log.Debug(string(out)) } diff --git a/python/pipenv.go b/python/pipenv.go index 7e7ef47..fd737e5 100644 --- a/python/pipenv.go +++ b/python/pipenv.go @@ -1,7 +1,6 @@ package python import ( - "fmt" "os" "os/exec" @@ -19,7 +18,7 @@ func pipenvProc(path string) { cmd.Dir = path out, err := cmd.CombinedOutput() if err != nil { - log.Error(fmt.Sprintf("%s", err.Error())) + log.Error(err.Error()) } log.Debug(string(out)) } diff --git a/python/poetry.go b/python/poetry.go index 2eea9ca..ae94b35 100644 --- a/python/poetry.go +++ b/python/poetry.go @@ -1,8 +1,8 @@ package python import ( - "fmt" "os/exec" + "path/filepath" log "github.com/sirupsen/logrus" ) @@ -11,12 +11,12 @@ func poetryProc(path string) { log.Info("Found 'pyproject.toml'. Creating virtual environment using 'Poetry'.") // Make sure .venv will be in project - if !fileExists("poetry.toml") { + if !fileExists(filepath.Join(path, "poetry.toml")) { cmd := exec.Command("poetry", "config", "--local", "virtualenvs.in-project", "true") cmd.Dir = path out, err := cmd.CombinedOutput() if err != nil { - log.Error(fmt.Sprintf("%s", err.Error())) + log.Error(err.Error()) } log.Debug(string(out)) } @@ -26,7 +26,7 @@ func poetryProc(path string) { cmd.Dir = path out, err := cmd.CombinedOutput() if err != nil { - log.Error(fmt.Sprintf("%s", err.Error())) + log.Error(err.Error()) } log.Debug(string(out)) } diff --git a/python/run.go b/python/run.go index 1a8bb2c..1569d1c 100644 --- a/python/run.go +++ b/python/run.go @@ -8,21 +8,16 @@ import ( func getPythonCommand(path string) string { if dirExists(filepath.Join(path, "./.venv")) { return filepath.Join(path, ".venv/bin/python") - } else { - return "python" } + return "python" } // GetPythonRunCommand Returns an *exec.Cmd to be handled with the provided "scriptName.py" using the python binary from virtual environment func GetPythonRunCommand(path string, scriptName string) *exec.Cmd { - pythonPath := getPythonCommand(path) - return exec.Command(pythonPath, scriptName) + return exec.Command(getPythonCommand(path), scriptName) } // ExecutePython Executes the provided "scriptName.py" using the python binary from virtual environment func ExecutePython(path string, scriptName string) ([]byte, error) { - // run python job - pythonPath := getPythonCommand(path) - cmd := exec.Command(pythonPath, scriptName) - return cmd.CombinedOutput() + return GetPythonRunCommand(path, scriptName).CombinedOutput() } diff --git a/python/uv.go b/python/uv.go index 8ffbe23..a40b4fb 100644 --- a/python/uv.go +++ b/python/uv.go @@ -1,7 +1,6 @@ package python import ( - "fmt" "os/exec" log "github.com/sirupsen/logrus" @@ -15,7 +14,7 @@ func uvProc(path string) { cmd.Dir = path out, err := cmd.CombinedOutput() if err != nil { - log.Error(fmt.Sprintf("%s", err.Error())) + log.Error(err.Error()) } log.Debug(string(out)) }