mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 23:51:25 +08:00
refactor: move exec to myexec
This commit is contained in:
@@ -12,7 +12,6 @@ import (
|
||||
"math"
|
||||
"math/rand"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strconv"
|
||||
@@ -23,7 +22,6 @@ import (
|
||||
"github.com/rs/zerolog/log"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/env"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/json"
|
||||
)
|
||||
|
||||
@@ -94,117 +92,6 @@ func FormatResponse(raw interface{}) interface{} {
|
||||
return formattedResponse
|
||||
}
|
||||
|
||||
var python3Executable string = "python3" // system default python3
|
||||
|
||||
// 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")
|
||||
}
|
||||
venv = filepath.Join(home, ".hrp", "venv")
|
||||
}
|
||||
python3, err = ensurePython3Venv(venv, packages...)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "prepare python3 venv failed")
|
||||
}
|
||||
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...)
|
||||
}
|
||||
|
||||
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 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)
|
||||
}
|
||||
|
||||
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, pkgVersion string
|
||||
if strings.Contains(pkg, "==") {
|
||||
// funppy==0.5.0
|
||||
pkgInfo := strings.Split(pkg, "==")
|
||||
pkgName = pkgInfo[0]
|
||||
pkgVersion = pkgInfo[1]
|
||||
} else {
|
||||
// funppy
|
||||
pkgName = pkg
|
||||
}
|
||||
|
||||
// check if package installed and version matched
|
||||
err = AssertPythonPackage(python3, pkgName, pkgVersion)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// check if pip available
|
||||
err = ExecCommand(python3, "-m", "pip", "--version")
|
||||
if err != nil {
|
||||
log.Warn().Msg("pip is not available")
|
||||
return errors.Wrap(err, "pip is not available")
|
||||
}
|
||||
|
||||
log.Info().Str("pkgName", pkgName).Str("pkgVersion", pkgVersion).Msg("installing python package")
|
||||
|
||||
// install package
|
||||
pypiIndexURL := env.PYPI_INDEX_URL
|
||||
if pypiIndexURL == "" {
|
||||
pypiIndexURL = "https://pypi.org/simple" // default
|
||||
}
|
||||
err = ExecCommand(python3, "-m", "pip", "install", "--upgrade", pkg,
|
||||
"--index-url", pypiIndexURL,
|
||||
"--quiet", "--disable-pip-version-check")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "pip install package failed")
|
||||
}
|
||||
|
||||
return AssertPythonPackage(python3, pkgName, pkgVersion)
|
||||
}
|
||||
|
||||
func ExecCommandInDir(cmd *exec.Cmd, dir string) error {
|
||||
log.Info().Str("cmd", cmd.String()).Str("dir", dir).Msg("exec command")
|
||||
cmd.Dir = dir
|
||||
|
||||
// print output with colors
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("exec command failed")
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateFolder(folderPath string) error {
|
||||
log.Info().Str("path", folderPath).Msg("create folder")
|
||||
err := os.MkdirAll(folderPath, os.ModePerm)
|
||||
@@ -289,19 +176,19 @@ func GetRandomNumber(min, max int) int {
|
||||
}
|
||||
|
||||
func Interface2Float64(i interface{}) (float64, error) {
|
||||
switch i.(type) {
|
||||
switch v := i.(type) {
|
||||
case int:
|
||||
return float64(i.(int)), nil
|
||||
return float64(v), nil
|
||||
case int32:
|
||||
return float64(i.(int32)), nil
|
||||
return float64(v), nil
|
||||
case int64:
|
||||
return float64(i.(int64)), nil
|
||||
return float64(v), nil
|
||||
case float32:
|
||||
return float64(i.(float32)), nil
|
||||
return float64(v), nil
|
||||
case float64:
|
||||
return i.(float64), nil
|
||||
return v, nil
|
||||
case string:
|
||||
intVar, err := strconv.Atoi(i.(string))
|
||||
intVar, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
@@ -1,103 +0,0 @@
|
||||
//go:build darwin || linux
|
||||
|
||||
package builtin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/env"
|
||||
)
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
func ensurePython3Venv(venv string, packages ...string) (python3 string, err error) {
|
||||
python3 = getPython3Executable(venv)
|
||||
|
||||
log.Info().
|
||||
Str("python3", python3).
|
||||
Interface("packages", packages).
|
||||
Msg("ensure python3 venv")
|
||||
|
||||
// check if python3 venv is available
|
||||
if !isPython3(python3) {
|
||||
// python3 venv not available, create one
|
||||
// check if system python3 is available
|
||||
if err := ExecCommand("python3", "--version"); err != nil {
|
||||
return "", errors.Wrap(err, "python3 not found")
|
||||
}
|
||||
|
||||
// check if .venv exists
|
||||
if _, err := os.Stat(venv); err == nil {
|
||||
// .venv exists, remove first
|
||||
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", venv); err != nil {
|
||||
return "", errors.Wrap(err, "create python3 venv failed")
|
||||
}
|
||||
}
|
||||
|
||||
// install default python packages
|
||||
for _, pkg := range packages {
|
||||
err := InstallPythonPackage(python3, pkg)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, fmt.Sprintf("pip install %s failed", pkg))
|
||||
}
|
||||
}
|
||||
|
||||
return python3, nil
|
||||
}
|
||||
|
||||
func Command(name string, arg ...string) *exec.Cmd {
|
||||
args := strings.Join(arg, " ")
|
||||
return exec.Command("bash", "-c", name, args)
|
||||
}
|
||||
|
||||
func ExecCommand(cmdName string, args ...string) error {
|
||||
cmd := exec.Command(cmdName, args...)
|
||||
log.Info().Str("cmd", cmd.String()).Msg("exec command")
|
||||
|
||||
// add cmd dir path to $PATH
|
||||
if cmdDir := filepath.Dir(cmdName); cmdDir != "" {
|
||||
PATH := fmt.Sprintf("%s:%s", cmdDir, env.PATH)
|
||||
if err := os.Setenv("PATH", PATH); err != nil {
|
||||
log.Error().Err(err).Msg("set env $PATH failed")
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// print output with colors
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("exec command failed")
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
//go:build windows
|
||||
|
||||
package builtin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"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 {
|
||||
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) {
|
||||
python3 = getPython3Executable(venvDir)
|
||||
log.Info().
|
||||
Str("python3", python3).
|
||||
Interface("packages", packages).
|
||||
Msg("ensure python3 venv")
|
||||
|
||||
systemPython := "python3"
|
||||
|
||||
// check if python3 venv is available
|
||||
if !isPython3(python3) {
|
||||
// python3 venv not available, create one
|
||||
// check if system python3 is available
|
||||
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"
|
||||
}
|
||||
|
||||
// check if .venv exists
|
||||
if _, err := os.Stat(venvDir); err == nil {
|
||||
// .venv exists, remove first
|
||||
if err := ExecCommand("del", "/q", venvDir); err != nil {
|
||||
return "", errors.Wrap(err, "remove existed venv failed")
|
||||
}
|
||||
}
|
||||
|
||||
// create python3 .venv
|
||||
// notice: --symlinks should be specified for windows
|
||||
// https://github.com/actions/virtual-environments/issues/2690
|
||||
if err := ExecCommand(systemPython, "-m", "venv", "--symlinks", venvDir); err != nil {
|
||||
// fix: failed to symlink on Windows
|
||||
log.Warn().Msg("failed to create python3 .venv by using --symlinks, try to use --copies")
|
||||
if err := ExecCommand(systemPython, "-m", "venv", "--copies", venvDir); err != nil {
|
||||
return "", errors.Wrap(err, "create python3 venv failed")
|
||||
}
|
||||
}
|
||||
|
||||
// fix: python3 doesn't exist in .venv on Windows
|
||||
if _, err := os.Stat(python3); err != nil {
|
||||
log.Warn().Msg("python3 doesn't exist, try to link python")
|
||||
err := os.Link(filepath.Join(venvDir, "Scripts", "python.exe"), python3)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "python3 doesn't exist in .venv")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// install default python packages
|
||||
for _, pkg := range packages {
|
||||
err := InstallPythonPackage(python3, pkg)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, fmt.Sprintf("pip install %s failed", pkg))
|
||||
}
|
||||
}
|
||||
|
||||
return python3, nil
|
||||
}
|
||||
|
||||
func Command(name string, arg ...string) *exec.Cmd {
|
||||
args := strings.Join(arg, " ")
|
||||
return exec.Command("cmd", "/c", name, args)
|
||||
}
|
||||
|
||||
func ExecCommand(cmdName string, args ...string) error {
|
||||
// "cmd /c" carries out the command specified by string and then stops
|
||||
// refer: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/cmd
|
||||
cmdStr := fmt.Sprintf("%s %s", cmdName, strings.Join(args, " "))
|
||||
cmd := exec.Command("cmd", "/c", cmdStr)
|
||||
log.Info().Str("cmd", cmd.String()).Msg("exec command")
|
||||
|
||||
// print output with colors
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("exec command failed")
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user