From 96ebad6083672a92cb7e3553beacdc8848267b3d Mon Sep 17 00:00:00 2001 From: xucong053 Date: Wed, 15 Jun 2022 11:27:35 +0800 Subject: [PATCH] fix: use python instead of python3 if python3 is not available on windows --- docs/cmd/hrp.md | 2 +- docs/cmd/hrp_boom.md | 2 +- docs/cmd/hrp_build.md | 2 +- docs/cmd/hrp_convert.md | 2 +- docs/cmd/hrp_pytest.md | 2 +- docs/cmd/hrp_run.md | 2 +- docs/cmd/hrp_startproject.md | 2 +- docs/cmd/hrp_wiki.md | 2 +- examples/demo-empty-project/proj.json | 4 ++-- examples/demo-with-go-plugin/proj.json | 2 +- examples/demo-with-py-plugin/proj.json | 2 +- examples/demo-without-plugin/proj.json | 4 ++-- hrp/build.go | 2 +- hrp/internal/builtin/utils_unix.go | 14 +++++++++++- hrp/internal/builtin/utils_windows.go | 31 ++++++++++++++++++++++---- 15 files changed, 55 insertions(+), 20 deletions(-) diff --git a/docs/cmd/hrp.md b/docs/cmd/hrp.md index 70279830..d68d3495 100644 --- a/docs/cmd/hrp.md +++ b/docs/cmd/hrp.md @@ -37,4 +37,4 @@ Copyright 2017 debugtalk * [hrp startproject](hrp_startproject.md) - create a scaffold project * [hrp wiki](hrp_wiki.md) - visit https://httprunner.com -###### Auto generated by spf13/cobra on 14-Jun-2022 +###### Auto generated by spf13/cobra on 15-Jun-2022 diff --git a/docs/cmd/hrp_boom.md b/docs/cmd/hrp_boom.md index 802d8f82..ef420992 100644 --- a/docs/cmd/hrp_boom.md +++ b/docs/cmd/hrp_boom.md @@ -42,4 +42,4 @@ hrp boom [flags] * [hrp](hrp.md) - Next-Generation API Testing Solution. -###### Auto generated by spf13/cobra on 14-Jun-2022 +###### Auto generated by spf13/cobra on 15-Jun-2022 diff --git a/docs/cmd/hrp_build.md b/docs/cmd/hrp_build.md index 3202ee3e..4018be2b 100644 --- a/docs/cmd/hrp_build.md +++ b/docs/cmd/hrp_build.md @@ -28,4 +28,4 @@ hrp build $path ... [flags] * [hrp](hrp.md) - Next-Generation API Testing Solution. -###### Auto generated by spf13/cobra on 14-Jun-2022 +###### Auto generated by spf13/cobra on 15-Jun-2022 diff --git a/docs/cmd/hrp_convert.md b/docs/cmd/hrp_convert.md index a1cf7236..93ce27ba 100644 --- a/docs/cmd/hrp_convert.md +++ b/docs/cmd/hrp_convert.md @@ -22,4 +22,4 @@ hrp convert $path... [flags] * [hrp](hrp.md) - Next-Generation API Testing Solution. -###### Auto generated by spf13/cobra on 14-Jun-2022 +###### Auto generated by spf13/cobra on 15-Jun-2022 diff --git a/docs/cmd/hrp_pytest.md b/docs/cmd/hrp_pytest.md index 3ceb2d62..a74c6621 100644 --- a/docs/cmd/hrp_pytest.md +++ b/docs/cmd/hrp_pytest.md @@ -16,4 +16,4 @@ hrp pytest $path ... [flags] * [hrp](hrp.md) - Next-Generation API Testing Solution. -###### Auto generated by spf13/cobra on 14-Jun-2022 +###### Auto generated by spf13/cobra on 15-Jun-2022 diff --git a/docs/cmd/hrp_run.md b/docs/cmd/hrp_run.md index ffbe28fc..9186a93a 100644 --- a/docs/cmd/hrp_run.md +++ b/docs/cmd/hrp_run.md @@ -35,4 +35,4 @@ hrp run $path... [flags] * [hrp](hrp.md) - Next-Generation API Testing Solution. -###### Auto generated by spf13/cobra on 14-Jun-2022 +###### Auto generated by spf13/cobra on 15-Jun-2022 diff --git a/docs/cmd/hrp_startproject.md b/docs/cmd/hrp_startproject.md index e608327d..7b4a3455 100644 --- a/docs/cmd/hrp_startproject.md +++ b/docs/cmd/hrp_startproject.md @@ -21,4 +21,4 @@ hrp startproject $project_name [flags] * [hrp](hrp.md) - Next-Generation API Testing Solution. -###### Auto generated by spf13/cobra on 14-Jun-2022 +###### Auto generated by spf13/cobra on 15-Jun-2022 diff --git a/docs/cmd/hrp_wiki.md b/docs/cmd/hrp_wiki.md index 196d0bba..0c9ed307 100644 --- a/docs/cmd/hrp_wiki.md +++ b/docs/cmd/hrp_wiki.md @@ -16,4 +16,4 @@ hrp wiki [flags] * [hrp](hrp.md) - Next-Generation API Testing Solution. -###### Auto generated by spf13/cobra on 14-Jun-2022 +###### Auto generated by spf13/cobra on 15-Jun-2022 diff --git a/examples/demo-empty-project/proj.json b/examples/demo-empty-project/proj.json index 2c83dda1..aaa9cdd5 100644 --- a/examples/demo-empty-project/proj.json +++ b/examples/demo-empty-project/proj.json @@ -1,5 +1,5 @@ { "project_name": "demo-empty-project", - "create_time": "2022-06-09T23:39:15.678957+08:00", - "hrp_version": "v4.1.2" + "create_time": "2022-06-15T11:16:58.586353+08:00", + "hrp_version": "v4.1.3" } diff --git a/examples/demo-with-go-plugin/proj.json b/examples/demo-with-go-plugin/proj.json index fac542f7..a26fc2be 100644 --- a/examples/demo-with-go-plugin/proj.json +++ b/examples/demo-with-go-plugin/proj.json @@ -1,5 +1,5 @@ { "project_name": "demo-with-go-plugin", - "create_time": "2022-06-14T13:19:05.262039+08:00", + "create_time": "2022-06-15T11:16:22.591+08:00", "hrp_version": "v4.1.3" } diff --git a/examples/demo-with-py-plugin/proj.json b/examples/demo-with-py-plugin/proj.json index e0ae1816..41946a97 100644 --- a/examples/demo-with-py-plugin/proj.json +++ b/examples/demo-with-py-plugin/proj.json @@ -1,5 +1,5 @@ { "project_name": "demo-with-py-plugin", - "create_time": "2022-06-14T13:19:05.680344+08:00", + "create_time": "2022-06-15T11:16:22.751458+08:00", "hrp_version": "v4.1.3" } diff --git a/examples/demo-without-plugin/proj.json b/examples/demo-without-plugin/proj.json index 8153f569..bf51df3f 100644 --- a/examples/demo-without-plugin/proj.json +++ b/examples/demo-without-plugin/proj.json @@ -1,5 +1,5 @@ { "project_name": "demo-without-plugin", - "create_time": "2022-06-09T23:39:15.552117+08:00", - "hrp_version": "v4.1.2" + "create_time": "2022-06-15T11:16:58.422853+08:00", + "hrp_version": "v4.1.3" } diff --git a/hrp/build.go b/hrp/build.go index 43cdaf82..9cecccfb 100644 --- a/hrp/build.go +++ b/hrp/build.go @@ -194,7 +194,7 @@ func buildGo(path string, output string) error { func buildPy(path string, output string) error { log.Info().Str("path", path).Str("output", output).Msg("start to prepare python plugin") // check the syntax of debugtalk.py - err := builtin.ExecCommand("python3", "-m", "py_compile", path) + err := builtin.ExecPython3Command("py_compile", path) if err != nil { return errors.Wrap(err, "python plugin syntax invalid") } diff --git a/hrp/internal/builtin/utils_unix.go b/hrp/internal/builtin/utils_unix.go index f375fc8e..4be33001 100644 --- a/hrp/internal/builtin/utils_unix.go +++ b/hrp/internal/builtin/utils_unix.go @@ -8,11 +8,23 @@ import ( "os" "os/exec" "path/filepath" + "strings" "github.com/pkg/errors" "github.com/rs/zerolog/log" ) +func isPython3(python string) bool { + out, err := exec.Command(python, "--version").Output() + if err != nil { + return false + } + if strings.HasPrefix(string(out), "Python 3") { + return true + } + return false +} + func getPython3Executable(venvDir string) string { return filepath.Join(venvDir, "bin", "python3") } @@ -26,7 +38,7 @@ func ensurePython3Venv(venv string, packages ...string) (python3 string, err err Msg("ensure python3 venv") // check if python3 venv is available - if err := exec.Command(python3, "--version").Run(); err != nil { + if isPython3(python3) { // python3 venv not available, create one // check if system python3 is available if err := ExecCommand("python3", "--version"); err != nil { diff --git a/hrp/internal/builtin/utils_windows.go b/hrp/internal/builtin/utils_windows.go index 1946ba0c..6c8ae13e 100644 --- a/hrp/internal/builtin/utils_windows.go +++ b/hrp/internal/builtin/utils_windows.go @@ -14,8 +14,30 @@ import ( "github.com/rs/zerolog/log" ) +func init() { + // use python instead of python3 if python3 is not available + if !isPython3(python3Executable) { + python3Executable = "python" + } +} + +func isPython3(python string) bool { + out, err := exec.Command("cmd", "/c", python, "--version").Output() + if err != nil { + return false + } + if strings.HasPrefix(string(out), "Python 3") { + return true + } + return false +} + func getPython3Executable(venvDir string) string { - return filepath.Join(venvDir, "Scripts", "python3.exe") + python := filepath.Join(venvDir, "Scripts", "python3.exe") + if isPython3(python) { + return python + } + return filepath.Join(venvDir, "Scripts", "python.exe") } func ensurePython3Venv(venvDir string, packages ...string) (python3 string, err error) { @@ -28,11 +50,12 @@ func ensurePython3Venv(venvDir string, packages ...string) (python3 string, err systemPython := "python3" // check if python3 venv is available - if err := exec.Command("cmd", "/c", python3, "--version").Run(); err != nil { + if !isPython3(python3) { // python3 venv not available, create one // check if system python3 is available - if err := ExecCommand(systemPython, "--version"); err != nil { - if err := ExecCommand("python", "--version"); err != nil { + log.Warn().Str("pythonPath", python3).Msg("python3 venv is not available, try to check system python3") + if !isPython3(systemPython) { + if !isPython3("python") { return "", errors.Wrap(err, "python3 not found") } systemPython = "python"