Skip to content

Commit

Permalink
refactor: python3 must be specified for python path
Browse files Browse the repository at this point in the history
  • Loading branch information
debugtalk committed Jun 13, 2022
1 parent 05564d4 commit 115bafb
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 251 deletions.
2 changes: 1 addition & 1 deletion funppy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.5.0'
__version__ = 'v0.5.0'

from funppy.plugin import register, serve

Expand Down
3 changes: 1 addition & 2 deletions funppy/examples/debugtalk.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import logging
from typing import List

import funppy


def sum(*args):
result = 0
Expand Down Expand Up @@ -44,6 +42,7 @@ def teardown_hook_example(name):


if __name__ == '__main__':
import funppy
funppy.register("sum", sum)
funppy.register("sum_ints", sum_ints)
funppy.register("concatenate", concatenate)
Expand Down
2 changes: 2 additions & 0 deletions go_plugin_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//go:build linux || freebsd || darwin
// +build linux freebsd darwin

// go plugin doesn't support windows

package funplugin
Expand Down
34 changes: 24 additions & 10 deletions hashicorp_plugin_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package funplugin

import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"

"github.com/httprunner/funplugin/shared"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -39,23 +40,36 @@ func TestHashicorpGoPlugin(t *testing.T) {
assertPlugin(t, plugin)
}

func TestHashicorpPythonPlugin(t *testing.T) {
plugin, err := Init("funppy/examples/debugtalk.py")
func TestHashicorpPythonPluginWithoutVenv(t *testing.T) {
_, err := Init("funppy/examples/debugtalk.py")
assert.EqualError(t, err, "python3 not specified")
}

func TestHashicorpPythonPluginWithVenv(t *testing.T) {
dir, err := ioutil.TempDir(os.TempDir(), "prefix")
if err != nil {
t.Fatal(err)
}
defer plugin.Quit()
defer os.RemoveAll(dir)

assertPlugin(t, plugin)
}
venvDir := filepath.Join(dir, ".hrp", "venv")
err = exec.Command("python3", "-m", "venv", venvDir).Run()
if err != nil {
t.Fatal(err)
}

func TestHashicorpPythonPluginWithVenv(t *testing.T) {
home, _ := os.UserHomeDir()
venvDir := filepath.Join(home, ".hrp", "venv")
python3, err := shared.EnsurePython3Venv(venvDir)
var python3 string
if runtime.GOOS == "windows" {
python3 = filepath.Join(venvDir, "Scripts", "python3.exe")
} else {
python3 = filepath.Join(venvDir, "bin", "python3")
}

err = exec.Command(python3, "-m", "pip", "install", "funppy").Run()
if err != nil {
t.Fatal(err)
}

plugin, err := Init("funppy/examples/debugtalk.py", WithPython3(python3))
if err != nil {
t.Fatal(err)
Expand Down
17 changes: 2 additions & 15 deletions init.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package funplugin

import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/pkg/errors"
"github.com/rs/zerolog/log"

"github.com/httprunner/funplugin/shared"
)

type IPlugin interface {
Expand Down Expand Up @@ -64,17 +61,7 @@ func Init(path string, options ...Option) (plugin IPlugin, err error) {
case ".py":
// found hashicorp python plugin file
if option.python3 == "" {
// default python3 venv path in $HOME/.hrp/venv
home, err := os.UserHomeDir()
if err != nil {
return nil, errors.Wrap(err, "get user home dir failed")
}
venvDir := filepath.Join(home, ".hrp", "venv")
python3, err := shared.EnsurePython3Venv(venvDir, "funppy")
if err != nil {
return nil, errors.Wrap(err, "ensure python venv failed")
}
option.python3 = python3
return nil, errors.New("python3 not specified")
}
option.langType = langTypePython
return newHashicorpPlugin(path, option)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "funppy"
version = "0.5.0"
version = "v0.5.0"
description = "Python plugin over gRPC for funplugin"
license = "Apache-2.0"
authors = ["debugtalk <[email protected]>"]
Expand Down
55 changes: 0 additions & 55 deletions shared/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package shared

import (
"fmt"
"os/exec"
"reflect"
"strings"

"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -105,59 +103,6 @@ func call(fn reflect.Value, args []reflect.Value) (interface{}, error) {
}
}

func InstallPythonPackage(python3 string, pkg string) (err error) {
var pkgName string
if strings.Contains(pkg, "==") {
// funppy==0.4.2
pkgInfo := strings.Split(pkg, "==")
pkgName = pkgInfo[0]
} else if strings.Contains(pkg, ">=") {
// httprunner>=4.0.0-beta
pkgInfo := strings.Split(pkg, ">=")
pkgName = pkgInfo[0]
} else {
pkgName = pkg
}

defer func() {
if err == nil {
// check package version
if out, err := exec.Command(
python3, "-c", fmt.Sprintf("import %s; print(%s.__version__)", pkgName, pkgName),
).Output(); err == nil {
log.Info().
Str("name", pkgName).
Str("version", strings.TrimSpace(string(out))).
Msg("python package is ready")
}
}
}()

// check if package installed
err = exec.Command(python3, "-c", fmt.Sprintf("import %s", pkgName)).Run()
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("package", pkg).Msg("installing python package")

// install package
err = execCommand(python3, "-m", "pip", "install", "--upgrade", pkg,
"--quiet", "--disable-pip-version-check")
if err != nil {
return errors.Wrap(err, "pip install package failed")
}

return nil
}

// ConvertCommonName returns name which deleted "_" and converted capital letter to their lower case
func ConvertCommonName(name string) string {
return strings.ToLower(strings.Replace(name, "_", "", -1))
Expand Down
72 changes: 0 additions & 72 deletions shared/utils_unix.go

This file was deleted.

95 changes: 0 additions & 95 deletions shared/utils_windows.go

This file was deleted.

0 comments on commit 115bafb

Please sign in to comment.