fix: use python instead of python3 if python3 is not available on windows

This commit is contained in:
xucong053
2022-06-15 11:27:35 +08:00
parent 4fdb24a8a8
commit 94366b0ecc
15 changed files with 55 additions and 20 deletions

View File

@@ -37,4 +37,4 @@ Copyright 2017 debugtalk
* [hrp startproject](hrp_startproject.md) - create a scaffold project * [hrp startproject](hrp_startproject.md) - create a scaffold project
* [hrp wiki](hrp_wiki.md) - visit https://httprunner.com * [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

View File

@@ -42,4 +42,4 @@ hrp boom [flags]
* [hrp](hrp.md) - Next-Generation API Testing Solution. * [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

View File

@@ -28,4 +28,4 @@ hrp build $path ... [flags]
* [hrp](hrp.md) - Next-Generation API Testing Solution. * [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

View File

@@ -22,4 +22,4 @@ hrp convert $path... [flags]
* [hrp](hrp.md) - Next-Generation API Testing Solution. * [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

View File

@@ -16,4 +16,4 @@ hrp pytest $path ... [flags]
* [hrp](hrp.md) - Next-Generation API Testing Solution. * [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

View File

@@ -35,4 +35,4 @@ hrp run $path... [flags]
* [hrp](hrp.md) - Next-Generation API Testing Solution. * [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

View File

@@ -21,4 +21,4 @@ hrp startproject $project_name [flags]
* [hrp](hrp.md) - Next-Generation API Testing Solution. * [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

View File

@@ -16,4 +16,4 @@ hrp wiki [flags]
* [hrp](hrp.md) - Next-Generation API Testing Solution. * [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

View File

@@ -1,5 +1,5 @@
{ {
"project_name": "demo-empty-project", "project_name": "demo-empty-project",
"create_time": "2022-06-09T23:39:15.678957+08:00", "create_time": "2022-06-15T11:16:58.586353+08:00",
"hrp_version": "v4.1.2" "hrp_version": "v4.1.3"
} }

View File

@@ -1,5 +1,5 @@
{ {
"project_name": "demo-with-go-plugin", "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" "hrp_version": "v4.1.3"
} }

View File

@@ -1,5 +1,5 @@
{ {
"project_name": "demo-with-py-plugin", "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" "hrp_version": "v4.1.3"
} }

View File

@@ -1,5 +1,5 @@
{ {
"project_name": "demo-without-plugin", "project_name": "demo-without-plugin",
"create_time": "2022-06-09T23:39:15.552117+08:00", "create_time": "2022-06-15T11:16:58.422853+08:00",
"hrp_version": "v4.1.2" "hrp_version": "v4.1.3"
} }

View File

@@ -194,7 +194,7 @@ func buildGo(path string, output string) error {
func buildPy(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") log.Info().Str("path", path).Str("output", output).Msg("start to prepare python plugin")
// check the syntax of debugtalk.py // check the syntax of debugtalk.py
err := builtin.ExecCommand("python3", "-m", "py_compile", path) err := builtin.ExecPython3Command("py_compile", path)
if err != nil { if err != nil {
return errors.Wrap(err, "python plugin syntax invalid") return errors.Wrap(err, "python plugin syntax invalid")
} }

View File

@@ -8,11 +8,23 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rs/zerolog/log" "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 { func getPython3Executable(venvDir string) string {
return filepath.Join(venvDir, "bin", "python3") return filepath.Join(venvDir, "bin", "python3")
} }
@@ -26,7 +38,7 @@ func ensurePython3Venv(venv string, packages ...string) (python3 string, err err
Msg("ensure python3 venv") Msg("ensure python3 venv")
// check if python3 venv is available // check if python3 venv is available
if err := exec.Command(python3, "--version").Run(); err != nil { if isPython3(python3) {
// python3 venv not available, create one // python3 venv not available, create one
// check if system python3 is available // check if system python3 is available
if err := ExecCommand("python3", "--version"); err != nil { if err := ExecCommand("python3", "--version"); err != nil {

View File

@@ -14,8 +14,30 @@ import (
"github.com/rs/zerolog/log" "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 { 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) { 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" systemPython := "python3"
// check if python3 venv is available // 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 // python3 venv not available, create one
// check if system python3 is available // check if system python3 is available
if err := ExecCommand(systemPython, "--version"); err != nil { log.Warn().Str("pythonPath", python3).Msg("python3 venv is not available, try to check system python3")
if err := ExecCommand("python", "--version"); err != nil { if !isPython3(systemPython) {
if !isPython3("python") {
return "", errors.Wrap(err, "python3 not found") return "", errors.Wrap(err, "python3 not found")
} }
systemPython = "python" systemPython = "python"