From 00396d4b7589a5fee72f101e6f1abd995c03d737 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Sun, 20 Aug 2023 11:28:33 +0800 Subject: [PATCH] refactor: move internal myexec to funplugin/myexec --- hrp/build.go | 2 +- hrp/cmd/convert.go | 2 +- hrp/cmd/pytest.go | 2 +- hrp/internal/myexec/cmd.go | 164 ----------------------------- hrp/internal/myexec/cmd_uixt.go | 71 ------------- hrp/internal/myexec/cmd_windows.go | 108 ------------------- hrp/internal/pytest/main.go | 2 +- hrp/internal/scaffold/main.go | 2 +- hrp/internal/wiki/main.go | 3 +- hrp/pkg/convert/from_gotest.go | 2 +- hrp/pkg/convert/to_pytest.go | 3 +- hrp/pkg/uixt/android_device.go | 2 +- hrp/plugin.go | 2 +- 13 files changed, 10 insertions(+), 355 deletions(-) delete mode 100644 hrp/internal/myexec/cmd.go delete mode 100644 hrp/internal/myexec/cmd_uixt.go delete mode 100644 hrp/internal/myexec/cmd_windows.go diff --git a/hrp/build.go b/hrp/build.go index 0eaa2922..a55c5396 100644 --- a/hrp/build.go +++ b/hrp/build.go @@ -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" ) diff --git a/hrp/cmd/convert.go b/hrp/cmd/convert.go index bc7752d8..cbfafdb2 100644 --- a/hrp/cmd/convert.go +++ b/hrp/cmd/convert.go @@ -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" ) diff --git a/hrp/cmd/pytest.go b/hrp/cmd/pytest.go index 9946f0ec..239329b0 100644 --- a/hrp/cmd/pytest.go +++ b/hrp/cmd/pytest.go @@ -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" diff --git a/hrp/internal/myexec/cmd.go b/hrp/internal/myexec/cmd.go deleted file mode 100644 index e40df5af..00000000 --- a/hrp/internal/myexec/cmd.go +++ /dev/null @@ -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 -} diff --git a/hrp/internal/myexec/cmd_uixt.go b/hrp/internal/myexec/cmd_uixt.go deleted file mode 100644 index 39c64085..00000000 --- a/hrp/internal/myexec/cmd_uixt.go +++ /dev/null @@ -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) -} diff --git a/hrp/internal/myexec/cmd_windows.go b/hrp/internal/myexec/cmd_windows.go deleted file mode 100644 index 3e202002..00000000 --- a/hrp/internal/myexec/cmd_windows.go +++ /dev/null @@ -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() -} diff --git a/hrp/internal/pytest/main.go b/hrp/internal/pytest/main.go index 258a2171..bdd6166b 100644 --- a/hrp/internal/pytest/main.go +++ b/hrp/internal/pytest/main.go @@ -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 { diff --git a/hrp/internal/scaffold/main.go b/hrp/internal/scaffold/main.go index 102b02d8..339f5b52 100644 --- a/hrp/internal/scaffold/main.go +++ b/hrp/internal/scaffold/main.go @@ -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" ) diff --git a/hrp/internal/wiki/main.go b/hrp/internal/wiki/main.go index c5364826..216d930b 100644 --- a/hrp/internal/wiki/main.go +++ b/hrp/internal/wiki/main.go @@ -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 { diff --git a/hrp/pkg/convert/from_gotest.go b/hrp/pkg/convert/from_gotest.go index d25dff85..7fa6a235 100644 --- a/hrp/pkg/convert/from_gotest.go +++ b/hrp/pkg/convert/from_gotest.go @@ -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 { diff --git a/hrp/pkg/convert/to_pytest.go b/hrp/pkg/convert/to_pytest.go index 224974c3..d41fe500 100644 --- a/hrp/pkg/convert/to_pytest.go +++ b/hrp/pkg/convert/to_pytest.go @@ -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 diff --git a/hrp/pkg/uixt/android_device.go b/hrp/pkg/uixt/android_device.go index 3832200b..f2b25839 100644 --- a/hrp/pkg/uixt/android_device.go +++ b/hrp/pkg/uixt/android_device.go @@ -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" ) diff --git a/hrp/plugin.go b/hrp/plugin.go index be494fed..07f7b80b 100644 --- a/hrp/plugin.go +++ b/hrp/plugin.go @@ -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" )