Change in vdsm[master]: adding dynamic_package_load util module for reuseability

ybronhei at redhat.com ybronhei at redhat.com
Wed Feb 3 08:45:04 UTC 2016


Yaniv Bronhaim has uploaded a new change for review.

Change subject: adding dynamic_package_load util module for reuseability
......................................................................

adding dynamic_package_load util module for reuseability

Change-Id: Ie87a63cfabb32c15376fbb1a06bbb20f3941f220
Signed-off-by: Yaniv Bronhaim <ybronhei at redhat.com>
---
M lib/vdsm/Makefile.am
A lib/vdsm/dynamic_package_load.py
M lib/vdsm/tool/configurator.py
M vdsm.spec.in
4 files changed, 60 insertions(+), 25 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/16/53016/1

diff --git a/lib/vdsm/Makefile.am b/lib/vdsm/Makefile.am
index a1628ab..882249d 100644
--- a/lib/vdsm/Makefile.am
+++ b/lib/vdsm/Makefile.am
@@ -31,6 +31,7 @@
 	cpuinfo.py \
 	define.py \
 	dmidecodeUtil.py \
+	dynamic_package_load.py \
 	exception.py \
 	executor.py \
 	health.py \
diff --git a/lib/vdsm/dynamic_package_load.py b/lib/vdsm/dynamic_package_load.py
new file mode 100644
index 0000000..d92a83f
--- /dev/null
+++ b/lib/vdsm/dynamic_package_load.py
@@ -0,0 +1,55 @@
+# Copyright 2016 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+#
+# Refer to the README and COPYING files for full details of the license
+#
+import os
+import re
+
+
+def _import_module(abspkg, mname):
+    """return imported module mname from abspkg."""
+    # TODO: use importlib once python version >= 2.7
+    pkg = "%s.%s" % (abspkg.__name__, mname)
+    return __import__(pkg, globals(), locals(), [mname], level=0)
+
+
+def _listfiles(path, suffix):
+    """Return files with suffix under pkg."""
+    is_type = re.compile(r"^[^_].*\.%s?$" % (suffix)).search
+    return set(
+        os.path.splitext(name)[0]
+        for name in os.listdir(path)
+        if is_type(name)
+    )
+
+
+def pkg_modules(pkg):
+    modules = {}
+    for module in _listfiles(pkg.__path__[0]):
+        modules[module] = _import_module(pkg, module)
+        if not hasattr(modules[module], 'name'):
+            setattr(modules[module], 'name', module)
+    return modules
+
+
+def pkg_api_funcs(pkg, prefix):
+    funcs = {}
+    for module in pkg_modules(pkg):
+        for func in dir(module):
+            if func.startswith('api_'):
+                funcs[func[4:]] = getattr(module, func)
+    return funcs
diff --git a/lib/vdsm/tool/configurator.py b/lib/vdsm/tool/configurator.py
index 20924ba..39afbb0 100644
--- a/lib/vdsm/tool/configurator.py
+++ b/lib/vdsm/tool/configurator.py
@@ -30,8 +30,6 @@
 
 from collections import deque
 import argparse
-import os
-import re
 import sys
 import traceback
 
@@ -42,30 +40,9 @@
     requiresRoot
 from . import configurators
 
+from vdsm import dynamic_package_load
 
-def _import_module(abspkg, mname):
-    """return imported module mname from abspkg."""
-    # TODO: use importlib once python version >= 2.7
-    pkg = "%s.%s" % (abspkg.__name__, mname)
-    return __import__(pkg, globals(), locals(), [mname], level=0)
-
-
-def _listmodules(path):
-    """Return base file names for all modules under pkg."""
-    is_module = re.compile(r"^[^_].*\.pyc?$").search
-    return set(
-        os.path.splitext(name)[0]
-        for name in os.listdir(path)
-        if is_module(name)
-    )
-
-
-_CONFIGURATORS = {}
-for module in _listmodules(configurators.__path__[0]):
-    _CONFIGURATORS[module] = _import_module(configurators, module)
-    if not hasattr(_CONFIGURATORS[module], 'name'):
-        setattr(_CONFIGURATORS[module], 'name', module)
-
+_CONFIGURATORS = dynamic_package_load.pkg_modules(configurators)
 
 #
 # Configurators Interface:
@@ -73,6 +50,7 @@
 # Default implementation follows;
 #
 
+
 def _getrequires(module):
     """Return a set of module names required by this module.
 
diff --git a/vdsm.spec.in b/vdsm.spec.in
index 1ebf9c1..a46f4bf 100644
--- a/vdsm.spec.in
+++ b/vdsm.spec.in
@@ -1088,6 +1088,7 @@
 %{python_sitelib}/%{vdsm_name}/dsaversion.py*
 %{python_sitelib}/%{vdsm_name}/define.py*
 %{python_sitelib}/%{vdsm_name}/dmidecodeUtil.py*
+%{python_sitelib}/%{vdsm_name}/dynamic_package_load.py*
 %{python_sitelib}/%{vdsm_name}/exception.py*
 %{python_sitelib}/%{vdsm_name}/executor.py*
 %{python_sitelib}/%{vdsm_name}/health.py*


-- 
To view, visit https://gerrit.ovirt.org/53016
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie87a63cfabb32c15376fbb1a06bbb20f3941f220
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Yaniv Bronhaim <ybronhei at redhat.com>


More information about the vdsm-patches mailing list