mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-26 01:51:29 +08:00
refactor: move internal myexec to funplugin/myexec
This commit is contained in:
@@ -11,13 +11,13 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/httprunner/funplugin/fungo"
|
||||
"github.com/httprunner/funplugin/myexec"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/code"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/env"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/myexec"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/sdk"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/version"
|
||||
)
|
||||
|
||||
@@ -5,11 +5,11 @@ import (
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/httprunner/funplugin/myexec"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/myexec"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/version"
|
||||
"github.com/httprunner/httprunner/v4/hrp/pkg/convert"
|
||||
)
|
||||
|
||||
@@ -5,10 +5,10 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/httprunner/funplugin/myexec"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/myexec"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/pytest"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/sdk"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/version"
|
||||
|
||||
@@ -1,164 +0,0 @@
|
||||
package myexec
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/code"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/env"
|
||||
)
|
||||
|
||||
var python3Executable string = "python3" // system default python3
|
||||
|
||||
func isPython3(python string) bool {
|
||||
out, err := Command(python, "--version").Output()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if strings.HasPrefix(string(out), "Python 3") {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 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(code.InvalidPython3Venv, err.Error())
|
||||
}
|
||||
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 RunCommand(python3Executable, args...)
|
||||
}
|
||||
|
||||
func AssertPythonPackage(python3 string, pkgName, pkgVersion string) error {
|
||||
out, err := 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 = RunCommand(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 = RunCommand(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 RunCommand(cmdName string, args ...string) error {
|
||||
cmd := 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 stderr output
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("stderr", stderr.String()).Msg("exec command failed")
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ExecCommandInDir(cmd *exec.Cmd, dir string) error {
|
||||
log.Info().Str("cmd", cmd.String()).Str("dir", dir).Msg("exec command")
|
||||
cmd.Dir = dir
|
||||
|
||||
// print stderr output
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("stderr", stderr.String()).Msg("exec command failed")
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
//go:build darwin || linux
|
||||
|
||||
package myexec
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
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 := RunCommand("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 := RunCommand("rm", "-rf", venv); err != nil {
|
||||
return "", errors.Wrap(err, "remove existed venv failed")
|
||||
}
|
||||
}
|
||||
|
||||
// create python3 .venv
|
||||
if err := RunCommand("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 {
|
||||
cmd := exec.Command(name, arg...)
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{
|
||||
Setpgid: true,
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
func KillProcessesByGpid(cmd *exec.Cmd) error {
|
||||
return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
//go:build windows
|
||||
|
||||
package myexec
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"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 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 := RunCommand("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 := RunCommand(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 := RunCommand(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 {
|
||||
// "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
|
||||
cmd := exec.Command("cmd.exe")
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{
|
||||
CmdLine: strings.Join(append([]string{"/c", name}, arg...), " "),
|
||||
HideWindow: true,
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
func KillProcessesByGpid(cmd *exec.Cmd) error {
|
||||
killCmd := Command("taskkill", "/T", "/F", "/PID ", strconv.Itoa(cmd.Process.Pid))
|
||||
return killCmd.Run()
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package pytest
|
||||
|
||||
import (
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/myexec"
|
||||
"github.com/httprunner/funplugin/myexec"
|
||||
)
|
||||
|
||||
func RunPytest(args []string) error {
|
||||
|
||||
@@ -8,13 +8,13 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/httprunner/funplugin/fungo"
|
||||
"github.com/httprunner/funplugin/myexec"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/env"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/myexec"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/sdk"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/version"
|
||||
)
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package wiki
|
||||
|
||||
import (
|
||||
"github.com/httprunner/funplugin/myexec"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/myexec"
|
||||
)
|
||||
|
||||
func OpenWiki() error {
|
||||
|
||||
@@ -4,10 +4,10 @@ import (
|
||||
_ "embed"
|
||||
"os"
|
||||
|
||||
"github.com/httprunner/funplugin/myexec"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/myexec"
|
||||
)
|
||||
|
||||
func convert2GoTestScripts(paths ...string) error {
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"github.com/httprunner/funplugin/myexec"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/myexec"
|
||||
)
|
||||
|
||||
// convert TCase to pytest case
|
||||
|
||||
@@ -8,12 +8,12 @@ import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/httprunner/funplugin/myexec"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/code"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/json"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/myexec"
|
||||
"github.com/httprunner/httprunner/v4/hrp/pkg/gadb"
|
||||
)
|
||||
|
||||
|
||||
@@ -9,12 +9,12 @@ import (
|
||||
|
||||
"github.com/httprunner/funplugin"
|
||||
"github.com/httprunner/funplugin/fungo"
|
||||
"github.com/httprunner/funplugin/myexec"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/code"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/env"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/myexec"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/sdk"
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user