diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index a84beb29..60611491 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -6,12 +6,13 @@ - feat #1342: support specify custom python3 venv - feat: support python3 venv priority, specified > projectDir/.venv > $HOME/.hrp/venv -- refactor: build plugin mechanism +- feat: assert python3 package is ready with specified version +- refactor: build plugin mechanism, cancel automatic installation of dependencies - fix: pip upgrade httprunner when installing hrp **python version** -fix: unexpected changes in step variables +- fix: unexpected changes in step variables ## v4.1.2 (2022-06-09) diff --git a/examples/demo-with-go-plugin/proj.json b/examples/demo-with-go-plugin/proj.json index 48b9a7c2..bebbd982 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-13T23:28:24.920066+08:00", - "hrp_version": "v4.1.2" + "create_time": "2022-06-13T23:42:06.381463+08:00", + "hrp_version": "v4.1.3" } diff --git a/examples/demo-with-py-plugin/.debugtalk_gen.py b/examples/demo-with-py-plugin/.debugtalk_gen.py index 653fed67..7199a757 100644 --- a/examples/demo-with-py-plugin/.debugtalk_gen.py +++ b/examples/demo-with-py-plugin/.debugtalk_gen.py @@ -1,4 +1,4 @@ -# NOTE: Generated By hrp v4.1.2, DO NOT EDIT! +# NOTE: Generated By hrp v4.1.3, DO NOT EDIT! import sys import os diff --git a/examples/demo-with-py-plugin/proj.json b/examples/demo-with-py-plugin/proj.json index 457cef97..1a5c6c7b 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-13T23:28:25.254595+08:00", - "hrp_version": "v4.1.2" + "create_time": "2022-06-13T23:42:06.751991+08:00", + "hrp_version": "v4.1.3" } diff --git a/hrp/internal/builtin/utils.go b/hrp/internal/builtin/utils.go index 9f6d2b92..c8d8fcc0 100644 --- a/hrp/internal/builtin/utils.go +++ b/hrp/internal/builtin/utils.go @@ -104,7 +104,17 @@ func PrepareVenv(venv string) error { } // not specify python3 venv, create default venv - python3, err := createDefaultPython3Venv() + 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 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") } @@ -113,56 +123,50 @@ func PrepareVenv(venv string) error { return nil } -// create default python3 venv located in $HOME/.hrp/venv -func createDefaultPython3Venv() (string, error) { - log.Info().Msg("create default python3 venv at $HOME/.hrp/venv") - home, err := os.UserHomeDir() - if err != nil { - return "", errors.Wrap(err, "get user home dir failed") - } - venv := filepath.Join(home, ".hrp", "venv") - packages := []string{ - fmt.Sprintf("funppy==%s", fungo.Version), - fmt.Sprintf("httprunner==%s", version.VERSION), - } - return EnsurePython3Venv(venv, packages...) -} - func ExecPython3Command(cmdName string, args ...string) error { args = append([]string{"-m", cmdName}, args...) return ExecCommand(Python3Executable, args...) } +func AssertPythonPackage(python3 string, pkgName, pkgVersion string) error { + out, err := exec.Command( + python3, "-c", fmt.Sprintf("import %s; print(%s.__version__)", pkgName, pkgName), + ).Output() + if err != nil { + return fmt.Errorf("python package %s not found", pkgName) + } + + // do not check version if pkgVersion is empty + if pkgVersion == "" { + log.Info().Str("name", pkgName).Msg("python package is ready") + return nil + } + + // check package version equality + version := strings.TrimSpace(string(out)) + if fmt.Sprintf("v%s", version) != pkgVersion { + return fmt.Errorf("python package %s version %s not matched, please upgrade to %s", + pkgName, version, pkgVersion) + } + + log.Info().Str("name", pkgName).Str("version", pkgVersion).Msg("python package is ready") + return nil +} + func InstallPythonPackage(python3 string, pkg string) (err error) { - var pkgName string + var pkgName, pkgVersion string if strings.Contains(pkg, "==") { - // funppy==0.4.2 + // funppy==0.5.0 pkgInfo := strings.Split(pkg, "==") pkgName = pkgInfo[0] - } else if strings.Contains(pkg, ">=") { - // httprunner>=4.0.0-beta - pkgInfo := strings.Split(pkg, ">=") - pkgName = pkgInfo[0] + pkgVersion = pkgInfo[1] } else { + // funppy pkgName = pkg } - defer func() { - if err == nil { - // check package version - if out, err := exec.Command( - python3, "-c", fmt.Sprintf("import %s; print(%s.__version__)", pkgName, pkgName), - ).Output(); err == nil { - log.Info(). - Str("name", pkgName). - Str("version", strings.TrimSpace(string(out))). - Msg("python package is ready") - } - } - }() - - // check if package installed - err = exec.Command(python3, "-c", fmt.Sprintf("import %s", pkgName)).Run() + // check if package installed and version matched + err = AssertPythonPackage(python3, pkgName, pkgVersion) if err == nil { return nil } @@ -183,7 +187,7 @@ func InstallPythonPackage(python3 string, pkg string) (err error) { return errors.Wrap(err, "pip install package failed") } - return nil + return AssertPythonPackage(python3, pkgName, pkgVersion) } func ExecCommandInDir(cmd *exec.Cmd, dir string) error { diff --git a/hrp/internal/scaffold/main.go b/hrp/internal/scaffold/main.go index ed9405a6..46e21b76 100644 --- a/hrp/internal/scaffold/main.go +++ b/hrp/internal/scaffold/main.go @@ -8,10 +8,10 @@ import ( "path/filepath" "time" - "github.com/httprunner/funplugin/fungo" "github.com/pkg/errors" "github.com/rs/zerolog/log" + "github.com/httprunner/funplugin/fungo" "github.com/httprunner/httprunner/v4/hrp" "github.com/httprunner/httprunner/v4/hrp/internal/builtin" "github.com/httprunner/httprunner/v4/hrp/internal/sdk" @@ -208,6 +208,7 @@ func createPythonPlugin(projectName, venv string) error { if venv == "" { venv = filepath.Join(projectName, ".venv") } + log.Info().Str("venv", venv).Msg("create python3 venv") packages := []string{ fmt.Sprintf("funppy==%s", fungo.Version), fmt.Sprintf("httprunner==%s", version.VERSION), diff --git a/hrp/internal/scaffold/templates/plugin/.debugtalk_gen.py b/hrp/internal/scaffold/templates/plugin/.debugtalk_gen.py index 653fed67..7199a757 100644 --- a/hrp/internal/scaffold/templates/plugin/.debugtalk_gen.py +++ b/hrp/internal/scaffold/templates/plugin/.debugtalk_gen.py @@ -1,4 +1,4 @@ -# NOTE: Generated By hrp v4.1.2, DO NOT EDIT! +# NOTE: Generated By hrp v4.1.3, DO NOT EDIT! import sys import os diff --git a/hrp/internal/scaffold/templates/plugin/debugtalk_gen.go b/hrp/internal/scaffold/templates/plugin/debugtalk_gen.go index 1e6ceac9..df7727c3 100644 --- a/hrp/internal/scaffold/templates/plugin/debugtalk_gen.go +++ b/hrp/internal/scaffold/templates/plugin/debugtalk_gen.go @@ -1,4 +1,4 @@ -// NOTE: Generated By hrp v4.1.2, DO NOT EDIT! +// NOTE: Generated By hrp v4.1.3, DO NOT EDIT! package main import ( diff --git a/hrp/plugin.go b/hrp/plugin.go index 91bdedd8..2af298c3 100644 --- a/hrp/plugin.go +++ b/hrp/plugin.go @@ -9,6 +9,7 @@ import ( "syscall" "github.com/httprunner/funplugin" + "github.com/httprunner/funplugin/fungo" "github.com/rs/zerolog/log" "github.com/httprunner/httprunner/v4/hrp/internal/builtin" @@ -57,6 +58,13 @@ func initPlugin(path, venv string, logOn bool) (plugin funplugin.IPlugin, err er log.Error().Err(err).Msg("prepare python3 venv failed") return } + // check if funppy is ready in python3 venv + err = builtin.AssertPythonPackage(builtin.Python3Executable, "funppy", fungo.Version) + if err != nil { + log.Error().Err(err).Str("venv", venv).Msg("python package funppy is not ready") + os.Exit(1) + } + pluginOptions = append(pluginOptions, funplugin.WithPython3(builtin.Python3Executable)) }