🥅 Better solution for invalid plugin detection
Should be done at plugin manager level, not externally. Ignore pyc files though as we currently can't parse their metadata (looking into that). Fix tests while at it.
This commit is contained in:
1 parent
cca3000eb2
commit
9a57362602
6 files changed
+74
-51
No files matched your search
@@ -620,29 +620,12 @@ def init_pluginsystem(
|
||||
|
||||
plugin_validators = []
|
||||
|
||||
def implementation_or_hook_validator(phase, plugin_info):
|
||||
if phase in ("before_import", "before_load", "before_enable"):
|
||||
result = plugin_info.parsed_metadata.get("has_control_properties", False)
|
||||
if not result:
|
||||
logger.info(
|
||||
"Plugin {} declares no control properties, "
|
||||
"might not even be a plugin, disabling".format(plugin_info.name)
|
||||
)
|
||||
return result
|
||||
return True
|
||||
|
||||
plugin_validators.append(implementation_or_hook_validator)
|
||||
|
||||
if safe_mode:
|
||||
|
||||
def safe_mode_validator(phase, plugin_info):
|
||||
if phase in ("before_import", "before_load", "before_enable"):
|
||||
plugin_info.safe_mode_victim = not plugin_info.bundled
|
||||
if not plugin_info.bundled:
|
||||
logger.info(
|
||||
"Plugin {} is not bundled, safe mode is active, "
|
||||
"disabling".format(plugin_info.name)
|
||||
)
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
@@ -300,7 +300,8 @@ class PluginInfo(object):
|
||||
|
||||
if phase == "before_import":
|
||||
result = (
|
||||
not self.forced_disabled
|
||||
self.looks_like_plugin
|
||||
and not self.forced_disabled
|
||||
and not self.blacklisted
|
||||
and not self.incompatible
|
||||
and not self.invalid_syntax
|
||||
@@ -675,6 +676,32 @@ class PluginInfo(object):
|
||||
if key.startswith("attr_")
|
||||
]
|
||||
|
||||
@property
|
||||
def looks_like_plugin(self):
|
||||
"""
|
||||
Returns whether the plugin actually looks like a plugin (has control properties) or not.
|
||||
|
||||
Note that compiled (.pyc) plugins for now will return True here as well, since we
|
||||
currently cannot easily parse metadata from them.
|
||||
"""
|
||||
return (
|
||||
self.parsed_metadata.get("has_control_properties", False) or self.is_compiled
|
||||
)
|
||||
|
||||
@property
|
||||
def is_compiled(self):
|
||||
path = self.location
|
||||
if not path:
|
||||
return False
|
||||
|
||||
if os.path.isdir(path):
|
||||
path = os.path.join(self.location, "__init__.py")
|
||||
|
||||
if not os.path.isfile(path):
|
||||
return False
|
||||
|
||||
return path.endswith(".pyc")
|
||||
|
||||
def _parse_metadata(self):
|
||||
result = {}
|
||||
|
||||
@@ -1366,7 +1393,8 @@ class PluginManager(object):
|
||||
for name, plugin in added.items():
|
||||
try:
|
||||
if (
|
||||
not plugin.blacklisted
|
||||
plugin.looks_like_plugin
|
||||
and not plugin.blacklisted
|
||||
and not plugin.forced_disabled
|
||||
and not plugin.incompatible
|
||||
):
|
||||
@@ -1392,6 +1420,7 @@ class PluginManager(object):
|
||||
try:
|
||||
if (
|
||||
plugin.loaded
|
||||
and plugin.looks_like_plugin
|
||||
and not plugin.forced_disabled
|
||||
and not plugin.incompatible
|
||||
):
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
|
||||
import octoprint.plugin
|
||||
|
||||
|
||||
class TestAssetPlugin(octoprint.plugin.AssetPlugin):
|
||||
pass
|
||||
|
||||
|
||||
__plugin_name__ = "Asset Plugin"
|
||||
__plugin_description__ = "Test asset plugin"
|
||||
__plugin_implementation__ = TestAssetPlugin()
|
||||
__plugin_pythoncompat__ = ">=2.7,<4"
|
||||
@@ -1,21 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
|
||||
import octoprint.plugin
|
||||
|
||||
|
||||
class TestDeprecatedAssetPlugin(octoprint.plugin.AssetPlugin):
|
||||
pass
|
||||
|
||||
|
||||
class TestSecondaryDeprecatedAssetPlugin(octoprint.plugin.AssetPlugin):
|
||||
pass
|
||||
|
||||
|
||||
__plugin_name__ = "Deprecated Plugin"
|
||||
__plugin_description__ = "Test deprecated plugin"
|
||||
__plugin_implementations__ = [
|
||||
TestDeprecatedAssetPlugin(),
|
||||
TestSecondaryDeprecatedAssetPlugin(),
|
||||
]
|
||||
__plugin_pythoncompat__ = ">=2.7,<4"
|
||||
@@ -0,0 +1,2 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
print("I'm not a plugin")
|
||||
+27
-11
@@ -68,6 +68,18 @@ class PluginTestCase(unittest.TestCase):
|
||||
os.path.dirname(os.path.realpath(__file__)), "_plugins"
|
||||
)
|
||||
|
||||
# prepare compiled files
|
||||
import py_compile
|
||||
|
||||
py_compile.compile(
|
||||
os.path.join(self.plugin_folder, "asset_plugin.py"),
|
||||
os.path.join(self.plugin_folder, "asset_plugin_2.pyc"),
|
||||
)
|
||||
py_compile.compile(
|
||||
os.path.join(self.plugin_folder, "not_a_plugin.py"),
|
||||
os.path.join(self.plugin_folder, "not_a_plugin_either.pyc"),
|
||||
)
|
||||
|
||||
plugin_folders = [self.plugin_folder]
|
||||
plugin_bases = [octoprint.plugin.OctoPrintPlugin]
|
||||
plugin_entry_points = None
|
||||
@@ -92,9 +104,10 @@ class PluginTestCase(unittest.TestCase):
|
||||
assert "__plugin_implementation__" in str(w[-1].message)
|
||||
|
||||
def test_plugin_loading(self):
|
||||
self.assertEqual(7, len(self.plugin_manager.enabled_plugins))
|
||||
self.assertEqual(9, len(self.plugin_manager.enabled_plugins))
|
||||
self.assertEqual(10, len(self.plugin_manager.plugins))
|
||||
self.assertEqual(2, len(self.plugin_manager.plugin_hooks))
|
||||
self.assertEqual(4, len(self.plugin_manager.plugin_implementations))
|
||||
self.assertEqual(5, len(self.plugin_manager.plugin_implementations))
|
||||
self.assertEqual(3, len(self.plugin_manager.plugin_implementations_by_type))
|
||||
|
||||
# hook_plugin
|
||||
@@ -137,13 +150,13 @@ class PluginTestCase(unittest.TestCase):
|
||||
),
|
||||
)
|
||||
|
||||
# TestDeprecatedAssetPlugin, NOT TestSecondaryDeprecatedAssetPlugin
|
||||
# TestAssetPlugin
|
||||
self.assertTrue(
|
||||
octoprint.plugin.AssetPlugin
|
||||
in self.plugin_manager.plugin_implementations_by_type
|
||||
)
|
||||
self.assertEqual(
|
||||
1,
|
||||
2,
|
||||
len(
|
||||
self.plugin_manager.plugin_implementations_by_type[
|
||||
octoprint.plugin.AssetPlugin
|
||||
@@ -167,7 +180,7 @@ class PluginTestCase(unittest.TestCase):
|
||||
)
|
||||
|
||||
all_implementations = self.plugin_manager.plugin_implementations
|
||||
self.assertEqual(4, len(all_implementations))
|
||||
self.assertEqual(5, len(all_implementations))
|
||||
for name, impl in all_implementations.items():
|
||||
self.assertTrue(name in self.plugin_manager.enabled_plugins)
|
||||
plugin = self.plugin_manager.enabled_plugins[name]
|
||||
@@ -258,7 +271,8 @@ class PluginTestCase(unittest.TestCase):
|
||||
octoprint.plugin.AssetPlugin
|
||||
)
|
||||
self.assertListEqual(
|
||||
["deprecated_plugin"], list(map(lambda x: x._identifier, implementations))
|
||||
["asset_plugin", "asset_plugin_2"],
|
||||
list(map(lambda x: x._identifier, implementations)),
|
||||
)
|
||||
|
||||
def test_get_filtered_implementations(self):
|
||||
@@ -305,12 +319,14 @@ class PluginTestCase(unittest.TestCase):
|
||||
client1.on_plugin_message.assert_called_once_with(plugin, data, permissions=None)
|
||||
client2.on_plugin_message.assert_called_once_with(plugin, data, permissions=None)
|
||||
|
||||
def test_validate_plugin(self):
|
||||
self.assertTrue("deprecated_plugin" in self.plugin_manager.enabled_plugins)
|
||||
def test_broken_plugin(self):
|
||||
for key in ("not_a_plugin",):
|
||||
self.assertTrue(key in self.plugin_manager.plugins)
|
||||
|
||||
plugin = self.plugin_manager.enabled_plugins["deprecated_plugin"]
|
||||
self.assertTrue(hasattr(plugin.instance, plugin.__class__.attr_implementation))
|
||||
self.assertFalse(hasattr(plugin.instance, plugin.__class__.attr_implementations))
|
||||
plugin = self.plugin_manager.plugins[key]
|
||||
self.assertFalse(plugin.looks_like_plugin)
|
||||
self.assertFalse(plugin.loaded)
|
||||
self.assertFalse(plugin.enabled)
|
||||
|
||||
@ddt.data(
|
||||
(
|
||||
|
||||
Reference in new issue
Block a user