mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-04 23:16:57 +08:00
refactor: EnsurePython3Venv
This commit is contained in:
@@ -13,13 +13,11 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/httprunner/funplugin/fungo"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/json"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/version"
|
||||
)
|
||||
|
||||
func Dump2JSON(data interface{}, path string) error {
|
||||
@@ -89,43 +87,31 @@ func FormatResponse(raw interface{}) interface{} {
|
||||
return formattedResponse
|
||||
}
|
||||
|
||||
var Python3Executable string = "python3" // system default python3
|
||||
var python3Executable string = "python3" // system default python3
|
||||
|
||||
func PrepareVenv(venv string) error {
|
||||
// specify python3 venv
|
||||
if venv != "" {
|
||||
python3 := getPython3Executable(venv)
|
||||
if !IsFilePathExists(python3) {
|
||||
return errors.New("specified python3 venv is invalid")
|
||||
// EnsurePython3Venv ensures python3 venv with specified packages
|
||||
// venv should be directory path of target venv
|
||||
func EnsurePython3Venv(venv string, packages ...string) (python3 string, err error) {
|
||||
// priority: specified > $HOME/.hrp/venv
|
||||
if venv == "" {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "get user home dir failed")
|
||||
}
|
||||
Python3Executable = python3
|
||||
log.Info().Str("Python3Executable", Python3Executable).Msg("set python3 executable path")
|
||||
return nil
|
||||
venv = filepath.Join(home, ".hrp", "venv")
|
||||
}
|
||||
|
||||
// not specify python3 venv, create default venv
|
||||
home, err := os.UserHomeDir()
|
||||
python3, err = ensurePython3Venv(venv, packages...)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "get user home dir failed")
|
||||
return "", errors.Wrap(err, "prepare python3 venv failed")
|
||||
}
|
||||
venv = filepath.Join(home, ".hrp", "venv")
|
||||
log.Info().Str("venv", venv).Msg("create default python3 venv")
|
||||
packages := []string{
|
||||
fmt.Sprintf("funppy==%s", fungo.Version),
|
||||
fmt.Sprintf("httprunner==%s", version.VERSION),
|
||||
}
|
||||
python3, err := EnsurePython3Venv(venv, packages...)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create default python3 venv failed")
|
||||
}
|
||||
Python3Executable = python3
|
||||
log.Info().Str("Python3Executable", Python3Executable).Msg("set python3 executable path")
|
||||
return nil
|
||||
python3Executable = python3
|
||||
log.Info().Str("Python3Executable", python3Executable).Msg("set python3 executable path")
|
||||
return python3, nil
|
||||
}
|
||||
|
||||
func ExecPython3Command(cmdName string, args ...string) error {
|
||||
args = append([]string{"-m", cmdName}, args...)
|
||||
return ExecCommand(Python3Executable, args...)
|
||||
return ExecCommand(python3Executable, args...)
|
||||
}
|
||||
|
||||
func AssertPythonPackage(python3 string, pkgName, pkgVersion string) error {
|
||||
@@ -144,7 +130,7 @@ func AssertPythonPackage(python3 string, pkgName, pkgVersion string) error {
|
||||
|
||||
// check package version equality
|
||||
version := strings.TrimSpace(string(out))
|
||||
if fmt.Sprintf("v%s", version) != pkgVersion {
|
||||
if strings.TrimLeft(version, "v") != strings.TrimLeft(pkgVersion, "v") {
|
||||
return fmt.Errorf("python package %s version %s not matched, please upgrade to %s",
|
||||
pkgName, version, pkgVersion)
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//go:build darwin || linux
|
||||
// +build darwin linux
|
||||
|
||||
package builtin
|
||||
@@ -16,10 +17,8 @@ func getPython3Executable(venvDir string) string {
|
||||
return filepath.Join(venvDir, "bin", "python3")
|
||||
}
|
||||
|
||||
// EnsurePython3Venv ensures python3 venv for hashicorp python plugin
|
||||
// venvDir should be directory path of target venv
|
||||
func EnsurePython3Venv(venvDir string, packages ...string) (python3 string, err error) {
|
||||
python3 = getPython3Executable(venvDir)
|
||||
func ensurePython3Venv(venv string, packages ...string) (python3 string, err error) {
|
||||
python3 = getPython3Executable(venv)
|
||||
|
||||
log.Info().
|
||||
Str("python3", python3).
|
||||
@@ -35,15 +34,15 @@ func EnsurePython3Venv(venvDir string, packages ...string) (python3 string, err
|
||||
}
|
||||
|
||||
// check if .venv exists
|
||||
if _, err := os.Stat(venvDir); err == nil {
|
||||
if _, err := os.Stat(venv); err == nil {
|
||||
// .venv exists, remove first
|
||||
if err := ExecCommand("rm", "-rf", venvDir); err != nil {
|
||||
if err := ExecCommand("rm", "-rf", venv); err != nil {
|
||||
return "", errors.Wrap(err, "remove existed venv failed")
|
||||
}
|
||||
}
|
||||
|
||||
// create python3 .venv
|
||||
if err := ExecCommand("python3", "-m", "venv", venvDir); err != nil {
|
||||
if err := ExecCommand("python3", "-m", "venv", venv); err != nil {
|
||||
return "", errors.Wrap(err, "create python3 venv failed")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package builtin
|
||||
|
||||
@@ -1,10 +1 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
||||
)
|
||||
|
||||
func convert2PyTestScripts(paths ...string) error {
|
||||
args := append([]string{"make"}, paths...)
|
||||
return builtin.ExecPython3Command("httprunner", args...)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package scaffold
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -12,7 +13,8 @@ func TestGenDemoExamples(t *testing.T) {
|
||||
}
|
||||
|
||||
dir = "../../../examples/demo-with-py-plugin"
|
||||
err = CreateScaffold(dir, Py, "", true)
|
||||
venv := filepath.Join(dir, ".venv")
|
||||
err = CreateScaffold(dir, Py, venv, true)
|
||||
if err != nil {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
@@ -205,14 +205,6 @@ func createPythonPlugin(projectName, venv string) error {
|
||||
return errors.Wrap(err, "copy file failed")
|
||||
}
|
||||
|
||||
if venv == "" {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "get user home dir failed")
|
||||
}
|
||||
venv = filepath.Join(home, ".hrp", "venv")
|
||||
}
|
||||
log.Info().Str("venv", venv).Msg("create python3 venv")
|
||||
packages := []string{
|
||||
fmt.Sprintf("funppy==%s", fungo.Version),
|
||||
fmt.Sprintf("httprunner==%s", version.VERSION),
|
||||
|
||||
Reference in New Issue
Block a user