mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-14 15:17:35 +08:00
feat: assert python3 package is ready with specified version
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 (
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user