Skip to content

Commit

Permalink
Merge pull request #471 from Pingdred/install_plugins_requirements
Browse files Browse the repository at this point in the history
Install plugins requirements at runtime
  • Loading branch information
pieroit authored Sep 28, 2023
2 parents b31cc4f + db73eeb commit 6461ad1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/cat/mad_hatter/mad_hatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def execute_hook(self, hook_name, *args):
*deepcopy(args[1:]),
cat=self.ccat
)
log.debug(f"Hook {hook.plugin_id}::{hook.name} returned {tea_spoon}")
#log.debug(f"Hook {hook.plugin_id}::{hook.name} returned {tea_spoon}")
if tea_spoon is not None:
tea_cup = tea_spoon
except Exception as e:
Expand Down
10 changes: 10 additions & 0 deletions core/cat/mad_hatter/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def __init__(self, plugin_path: str):
# plugin manifest (name, decription, thumb, etc.)
self._manifest = self._load_manifest()

self._install_requirements()

# list of tools and hooks contained in the plugin.
# The MadHatter will cache them for easier access,
# but they are created and stored in each plugin instance
Expand Down Expand Up @@ -166,6 +168,14 @@ def _load_manifest(self):
meta["version"] = json_file_data.get("version", "0.0.1")

return meta

def _install_requirements(self):
req_file = os.path.join(self.path, "requirements.txt")

if os.path.exists(req_file):
log.critical(f"Installing requirements for: {self.id}")
os.system(f'pip install --no-cache-dir -r "{req_file}"')


# lists of hooks and tools
def _load_decorated_functions(self):
Expand Down
3 changes: 3 additions & 0 deletions core/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def clean_up_mocks():
shutil.rmtree(tbr)
else:
os.remove(tbr)

# Uninstall mock plugin requirements
os.system("pip uninstall -y pip-install-test")


@pytest.fixture(scope="function")
Expand Down
9 changes: 9 additions & 0 deletions core/tests/mad_hatter/test_plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os
import pytest
import fnmatch
import subprocess

from inspect import isfunction

from cat.mad_hatter.mad_hatter import Plugin
Expand Down Expand Up @@ -52,10 +55,16 @@ def test_create_plugin(plugin):
assert plugin.manifest["name"] == "MockPlugin"
assert "Description not found" in plugin.manifest["description"]

# Check if plugin requirement is installed
result = subprocess.run(['pip', 'list'], stdout=subprocess.PIPE)
result = result.stdout.decode()
assert fnmatch.fnmatch(result, "*pip-install-test*")

# hooks and tools
assert plugin.hooks == []
assert plugin.tools == []


def test_activate_plugin(plugin):

# activate it
Expand Down
1 change: 1 addition & 0 deletions core/tests/mocks/mock_plugin/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pip-install-test

0 comments on commit 6461ad1

Please sign in to comment.