diff --git a/go.mod b/go.mod index 198dcf26..633c73b7 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/go-openapi/spec v0.20.6 github.com/google/uuid v1.3.0 github.com/gorilla/websocket v1.4.1 - github.com/httprunner/funplugin v0.4.8 + github.com/httprunner/funplugin v0.4.9 github.com/jinzhu/copier v0.3.2 github.com/jmespath/go-jmespath v0.4.0 github.com/json-iterator/go v1.1.12 diff --git a/go.sum b/go.sum index b3891bad..c10a45e2 100644 --- a/go.sum +++ b/go.sum @@ -255,6 +255,8 @@ github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb h1:b5rjCoWHc7eqmAS github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= github.com/httprunner/funplugin v0.4.8 h1:G785jrEn6EAEg2nwuPcCQUHBTgwgoaSz5qdQU4X3JpI= github.com/httprunner/funplugin v0.4.8/go.mod h1:vPyeJIfbpGe0epZZtAV0wCn16gLY9+imSw/zfxq0Lcc= +github.com/httprunner/funplugin v0.4.9 h1:gmF1sP5D4/nvdocqgOAyT3GpVDz3fL4ErZ17WHo8x9U= +github.com/httprunner/funplugin v0.4.9/go.mod h1:vPyeJIfbpGe0epZZtAV0wCn16gLY9+imSw/zfxq0Lcc= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/imkira/go-interpol v1.1.0/go.mod h1:z0h2/2T3XF8kyEPpRgJ3kmNv+C43p+I/CoI+jC3w2iA= diff --git a/hrp/build.go b/hrp/build.go index 4c5cb940..3184fa3c 100644 --- a/hrp/build.go +++ b/hrp/build.go @@ -44,6 +44,7 @@ type TemplateContent struct { FromImports []string // python from...import... Functions []string // python/go function FunctionNames []string // function name set by user + Packages []string // python packages } type Regexps struct { @@ -127,8 +128,15 @@ func (t *TemplateContent) parsePyContent(path string) error { if strings.HasPrefix(line, "import") { t.Imports = append(t.Imports, strings.Trim(line, " ")) + // e.g. import module as md + // import package.module + t.Packages = append(t.Packages, strings.Split(strings.Split(line, " ")[1], ".")[0]) } else if strings.HasPrefix(line, "from") { t.FromImports = append(t.FromImports, strings.Trim(line, " ")) + // e.g. from package.module import function + // from module import function + // from package import module + t.Packages = append(t.Packages, strings.Split(strings.Split(line, " ")[1], ".")[0]) } else { // no parse content at under of `if __name__ == "__main__"` if strings.HasPrefix(line, "if __name__") { @@ -257,11 +265,24 @@ func buildPy(path string, output string) error { FunctionName: regexp.MustCompile(regexPythonFunctionName), }, } + err := templateContent.parsePyContent(path) if err != nil { return err } + // ensure installation of packages + _, err = builtin.EnsurePython3Venv(templateContent.Packages...) + if err != nil { + return err + } + + // check the syntax of debugtalk.py + err = builtin.CheckPythonScriptSyntax(path) + if err != nil { + return err + } + // generate .debugtalk_gen.py if output == "" { dir, _ := os.Getwd() diff --git a/hrp/internal/builtin/utils.go b/hrp/internal/builtin/utils.go index 8f3ca391..93a4b64b 100644 --- a/hrp/internal/builtin/utils.go +++ b/hrp/internal/builtin/utils.go @@ -103,6 +103,11 @@ func EnsurePython3Venv(packages ...string) (string, error) { return python3, nil } +func CheckPythonScriptSyntax(path string) error { + err := ExecCommand("python3", "-m", "py_compile", path) + return err +} + func ExecCommandInDir(cmd *exec.Cmd, dir string) error { log.Info().Str("cmd", cmd.String()).Str("dir", dir).Msg("exec command") cmd.Dir = dir diff --git a/hrp/parameters.go b/hrp/parameters.go index 13d1b679..ae8e1b9f 100644 --- a/hrp/parameters.go +++ b/hrp/parameters.go @@ -163,7 +163,7 @@ func (iter *ParametersIterator) Next() map[string]interface{} { // merge with random parameters for _, paramName := range iter.randomParameterNames { - randSource := rand.New(rand.NewSource(time.Now().Unix())) + randSource := rand.New(rand.NewSource(time.Now().UnixNano())) randIndex := randSource.Intn(len(iter.data[paramName])) for k, v := range iter.data[paramName][randIndex] { selectedParameters[k] = v