1 |
commit: 3bffbc6150be9ee81d47547cbff113a8d45edb6d |
2 |
Author: Brian Dolbec <dolsen <AT> gentoo <DOT> org> |
3 |
AuthorDate: Thu Aug 17 01:50:21 2017 +0000 |
4 |
Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> |
5 |
CommitDate: Fri Mar 30 03:51:20 2018 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=3bffbc61 |
7 |
|
8 |
module.py: Extend the module loader for API version checking |
9 |
|
10 |
If provided with an iterable of compatibility versions, The controller |
11 |
will check the plugin modules module_spec 'version' variable is |
12 |
compatible with the base application. |
13 |
|
14 |
pym/portage/module.py | 22 ++++++++++++++++++++-- |
15 |
1 file changed, 20 insertions(+), 2 deletions(-) |
16 |
|
17 |
diff --git a/pym/portage/module.py b/pym/portage/module.py |
18 |
index c79e65518..bd7c94d4e 100644 |
19 |
--- a/pym/portage/module.py |
20 |
+++ b/pym/portage/module.py |
21 |
@@ -15,6 +15,10 @@ class InvalidModuleName(PortageException): |
22 |
"""An invalid or unknown module name.""" |
23 |
|
24 |
|
25 |
+class ModuleVersionError(PortageException): |
26 |
+ '''An incompatible module version''' |
27 |
+ |
28 |
+ |
29 |
class Module(object): |
30 |
"""Class to define and hold our plug-in module |
31 |
|
32 |
@@ -87,16 +91,17 @@ class Modules(object): |
33 |
@param namepath: Python import path to the "modules" directory |
34 |
""" |
35 |
|
36 |
- def __init__(self, path, namepath): |
37 |
+ def __init__(self, path, namepath, compat_versions=None): |
38 |
self._module_path = path |
39 |
self._namepath = namepath |
40 |
+ self.compat_versions = compat_versions |
41 |
self.parents = [] |
42 |
self._modules = self._get_all_modules() |
43 |
self.modules = ProtectedDict(self._modules) |
44 |
self.module_names = sorted(self._modules) |
45 |
|
46 |
def _get_all_modules(self): |
47 |
- """scans the emaint modules dir for loadable modules |
48 |
+ """scans the _module_path dir for loadable modules |
49 |
|
50 |
@rtype: dictionary of module_plugins |
51 |
""" |
52 |
@@ -117,6 +122,7 @@ class Modules(object): |
53 |
kids = {} |
54 |
for entry in importables: |
55 |
new_module = Module(entry, self._namepath) |
56 |
+ self._check_compat(new_module) |
57 |
for module_name in new_module.kids: |
58 |
kid = new_module.kids[module_name] |
59 |
kid['parent'] = new_module |
60 |
@@ -211,6 +217,8 @@ class Modules(object): |
61 |
|
62 |
@type modname: string |
63 |
@param modname: the module class name |
64 |
+ @type var: string |
65 |
+ @param var: the base level variable to return |
66 |
@type dictionary |
67 |
@return: the modules class exported options descriptions |
68 |
""" |
69 |
@@ -220,3 +228,13 @@ class Modules(object): |
70 |
raise InvalidModuleName( |
71 |
"Module name '%s' is invalid or not found" % modname) |
72 |
return value |
73 |
+ |
74 |
+ def _check_compat(self, module): |
75 |
+ if self.compat_versions: |
76 |
+ if not module.module_spec['version'] in self.compat_versions: |
77 |
+ raise ModuleVersionError( |
78 |
+ "Error loading '%s' plugin module: %s, version: %s\n" |
79 |
+ "Module is not compatible with the current application version\n" |
80 |
+ "Compatible module API versions are: %s" |
81 |
+ % (self._namepath, module.module_spec['name'], |
82 |
+ module.module_spec['version'], self.compat_versions)) |