Skip to content

Commit 115bafb

Browse files
committed
refactor: python3 must be specified for python path
1 parent 05564d4 commit 115bafb

9 files changed

Lines changed: 31 additions & 251 deletions

File tree

funppy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = '0.5.0'
1+
__version__ = 'v0.5.0'
22

33
from funppy.plugin import register, serve
44

funppy/examples/debugtalk.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import logging
22
from typing import List
33

4-
import funppy
5-
64

75
def sum(*args):
86
result = 0
@@ -44,6 +42,7 @@ def teardown_hook_example(name):
4442

4543

4644
if __name__ == '__main__':
45+
import funppy
4746
funppy.register("sum", sum)
4847
funppy.register("sum_ints", sum_ints)
4948
funppy.register("concatenate", concatenate)

go_plugin_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
//go:build linux || freebsd || darwin
12
// +build linux freebsd darwin
3+
24
// go plugin doesn't support windows
35

46
package funplugin

hashicorp_plugin_test.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package funplugin
22

33
import (
4+
"io/ioutil"
45
"os"
56
"os/exec"
67
"path/filepath"
8+
"runtime"
79
"testing"
810

9-
"github.com/httprunner/funplugin/shared"
1011
"github.com/rs/zerolog/log"
1112
"github.com/stretchr/testify/assert"
1213
)
@@ -39,23 +40,36 @@ func TestHashicorpGoPlugin(t *testing.T) {
3940
assertPlugin(t, plugin)
4041
}
4142

42-
func TestHashicorpPythonPlugin(t *testing.T) {
43-
plugin, err := Init("funppy/examples/debugtalk.py")
43+
func TestHashicorpPythonPluginWithoutVenv(t *testing.T) {
44+
_, err := Init("funppy/examples/debugtalk.py")
45+
assert.EqualError(t, err, "python3 not specified")
46+
}
47+
48+
func TestHashicorpPythonPluginWithVenv(t *testing.T) {
49+
dir, err := ioutil.TempDir(os.TempDir(), "prefix")
4450
if err != nil {
4551
t.Fatal(err)
4652
}
47-
defer plugin.Quit()
53+
defer os.RemoveAll(dir)
4854

49-
assertPlugin(t, plugin)
50-
}
55+
venvDir := filepath.Join(dir, ".hrp", "venv")
56+
err = exec.Command("python3", "-m", "venv", venvDir).Run()
57+
if err != nil {
58+
t.Fatal(err)
59+
}
5160

52-
func TestHashicorpPythonPluginWithVenv(t *testing.T) {
53-
home, _ := os.UserHomeDir()
54-
venvDir := filepath.Join(home, ".hrp", "venv")
55-
python3, err := shared.EnsurePython3Venv(venvDir)
61+
var python3 string
62+
if runtime.GOOS == "windows" {
63+
python3 = filepath.Join(venvDir, "Scripts", "python3.exe")
64+
} else {
65+
python3 = filepath.Join(venvDir, "bin", "python3")
66+
}
67+
68+
err = exec.Command(python3, "-m", "pip", "install", "funppy").Run()
5669
if err != nil {
5770
t.Fatal(err)
5871
}
72+
5973
plugin, err := Init("funppy/examples/debugtalk.py", WithPython3(python3))
6074
if err != nil {
6175
t.Fatal(err)

init.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package funplugin
22

33
import (
4+
"errors"
45
"fmt"
5-
"os"
66
"path/filepath"
77

8-
"github.com/pkg/errors"
98
"github.com/rs/zerolog/log"
10-
11-
"github.com/httprunner/funplugin/shared"
129
)
1310

1411
type IPlugin interface {
@@ -64,17 +61,7 @@ func Init(path string, options ...Option) (plugin IPlugin, err error) {
6461
case ".py":
6562
// found hashicorp python plugin file
6663
if option.python3 == "" {
67-
// default python3 venv path in $HOME/.hrp/venv
68-
home, err := os.UserHomeDir()
69-
if err != nil {
70-
return nil, errors.Wrap(err, "get user home dir failed")
71-
}
72-
venvDir := filepath.Join(home, ".hrp", "venv")
73-
python3, err := shared.EnsurePython3Venv(venvDir, "funppy")
74-
if err != nil {
75-
return nil, errors.Wrap(err, "ensure python venv failed")
76-
}
77-
option.python3 = python3
64+
return nil, errors.New("python3 not specified")
7865
}
7966
option.langType = langTypePython
8067
return newHashicorpPlugin(path, option)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "funppy"
3-
version = "0.5.0"
3+
version = "v0.5.0"
44
description = "Python plugin over gRPC for funplugin"
55
license = "Apache-2.0"
66
authors = ["debugtalk <mail@debugtalk.com>"]

shared/utils.go

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ package shared
22

33
import (
44
"fmt"
5-
"os/exec"
65
"reflect"
76
"strings"
87

9-
"github.com/pkg/errors"
108
"github.com/rs/zerolog/log"
119
)
1210

@@ -105,59 +103,6 @@ func call(fn reflect.Value, args []reflect.Value) (interface{}, error) {
105103
}
106104
}
107105

108-
func InstallPythonPackage(python3 string, pkg string) (err error) {
109-
var pkgName string
110-
if strings.Contains(pkg, "==") {
111-
// funppy==0.4.2
112-
pkgInfo := strings.Split(pkg, "==")
113-
pkgName = pkgInfo[0]
114-
} else if strings.Contains(pkg, ">=") {
115-
// httprunner>=4.0.0-beta
116-
pkgInfo := strings.Split(pkg, ">=")
117-
pkgName = pkgInfo[0]
118-
} else {
119-
pkgName = pkg
120-
}
121-
122-
defer func() {
123-
if err == nil {
124-
// check package version
125-
if out, err := exec.Command(
126-
python3, "-c", fmt.Sprintf("import %s; print(%s.__version__)", pkgName, pkgName),
127-
).Output(); err == nil {
128-
log.Info().
129-
Str("name", pkgName).
130-
Str("version", strings.TrimSpace(string(out))).
131-
Msg("python package is ready")
132-
}
133-
}
134-
}()
135-
136-
// check if package installed
137-
err = exec.Command(python3, "-c", fmt.Sprintf("import %s", pkgName)).Run()
138-
if err == nil {
139-
return nil
140-
}
141-
142-
// check if pip available
143-
err = execCommand(python3, "-m", "pip", "--version")
144-
if err != nil {
145-
log.Warn().Msg("pip is not available")
146-
return errors.Wrap(err, "pip is not available")
147-
}
148-
149-
log.Info().Str("package", pkg).Msg("installing python package")
150-
151-
// install package
152-
err = execCommand(python3, "-m", "pip", "install", "--upgrade", pkg,
153-
"--quiet", "--disable-pip-version-check")
154-
if err != nil {
155-
return errors.Wrap(err, "pip install package failed")
156-
}
157-
158-
return nil
159-
}
160-
161106
// ConvertCommonName returns name which deleted "_" and converted capital letter to their lower case
162107
func ConvertCommonName(name string) string {
163108
return strings.ToLower(strings.Replace(name, "_", "", -1))

shared/utils_unix.go

Lines changed: 0 additions & 72 deletions
This file was deleted.

shared/utils_windows.go

Lines changed: 0 additions & 95 deletions
This file was deleted.

0 commit comments

Comments
 (0)