Repository navigation
Expand file tree
/
Copy pathloader.py
More file actions
100 lines (80 loc) · 3.75 KB
/
Copy pathloader.py
File metadata and controls
100 lines (80 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""In-process plugin loading (architecture §14, §34)."""
from __future__ import annotations
import hashlib
import importlib.util
import sys
from dataclasses import dataclass
from pathlib import Path
from coretrace_python.analysis import AnalysisManager
from coretrace_python.plugins.api import PLUGIN_API_VERSION, Plugin
from coretrace_python.plugins.manifest import ManifestError, PluginManifest, load_manifest
MANIFEST_FILENAME = "plugin.toml"
class IncompatiblePluginError(Exception):
"""The plugin targets a plugin API version this engine does not provide."""
@dataclass(frozen=True)
class LoadedPlugin:
manifest: PluginManifest
plugin: Plugin
directory: Path
def load_plugin(directory: Path, manager: AnalysisManager) -> LoadedPlugin:
"""Load the plugin in ``directory`` and check it against its manifest."""
manifest_path = directory / MANIFEST_FILENAME
manifest = load_manifest(manifest_path)
if not manifest.plugin_api.contains(PLUGIN_API_VERSION):
raise IncompatiblePluginError(
f"plugin {manifest.name!r} requires plugin_api {manifest.plugin_api}"
f" but this engine provides {PLUGIN_API_VERSION}"
)
for analysis_name in manifest.requires:
try:
manager.analysis(analysis_name)
except KeyError:
raise ManifestError(
f"{manifest_path}: unknown analysis {analysis_name!r} in requires"
) from None
plugin_class = _import_entrypoint(directory, manifest)
declared = {analysis.name for analysis in plugin_class.requires}
if declared != set(manifest.requires):
raise ManifestError(
f"{manifest_path}: manifest requires {sorted(manifest.requires)}"
f" but {manifest.entrypoint.class_name} declares {sorted(declared)}"
)
return LoadedPlugin(manifest, plugin_class(), directory)
def discover_plugins(root: Path, manager: AnalysisManager) -> tuple[LoadedPlugin, ...]:
"""Load every plugin below ``root``, in path order."""
return tuple(
load_plugin(manifest_path.parent, manager)
for manifest_path in sorted(root.rglob(MANIFEST_FILENAME))
)
def _import_entrypoint(directory: Path, manifest: PluginManifest) -> type[Plugin]:
"""Import the entrypoint, a module ``<name>.py`` or a package ``<name>/__init__.py``.
The module name derives from the path, so two plugins may both call their module
``models``; a package is registered in ``sys.modules`` under that name before it
runs, which is what its relative imports resolve through."""
package_path = directory / manifest.entrypoint.module / "__init__.py"
is_package = package_path.is_file()
module_path = package_path if is_package else directory / f"{manifest.entrypoint.module}.py"
if not module_path.is_file():
raise ManifestError(
f"{directory / MANIFEST_FILENAME}: entrypoint module"
f" {manifest.entrypoint.module!r} not found"
)
unique = hashlib.sha1(str(module_path.resolve()).encode()).hexdigest()[:12]
name = f"coretrace_plugin_{unique}"
spec = importlib.util.spec_from_file_location(
name,
module_path,
submodule_search_locations=[str(module_path.parent)] if is_package else None,
)
assert spec is not None and spec.loader is not None
module = importlib.util.module_from_spec(spec)
if is_package:
sys.modules[name] = module
spec.loader.exec_module(module)
candidate = getattr(module, manifest.entrypoint.class_name, None)
if not (isinstance(candidate, type) and issubclass(candidate, Plugin)):
raise ManifestError(
f"{directory / MANIFEST_FILENAME}: entrypoint class"
f" {manifest.entrypoint.class_name!r} is not a Plugin"
)
return candidate