[libvirt] [sandbox PATCH v2 03/19] Image: Add Hooking Mechanism

Eren Yagdiran erenyagdiran at gmail.com
Tue Aug 4 20:11:09 UTC 2015


Any custom source provider can be added to virt-sandbox-image as a source
---
 .gitignore                               |  1 +
 bin/Makefile.am                          | 16 ++++++++++++----
 bin/virt-sandbox-image.in                |  3 +++
 configure.ac                             |  2 ++
 virt-sandbox-image/Makefile.am           | 13 +++++++++++++
 virt-sandbox-image/sources/Source.py     | 31 +++++++++++++++++++++++++++++++
 virt-sandbox-image/sources/__init__.py   | 29 +++++++++++++++++++++++++++++
 virt-sandbox-image/virt-sandbox-image.py | 13 ++++++++++++-
 8 files changed, 103 insertions(+), 5 deletions(-)
 create mode 100644 bin/virt-sandbox-image.in
 create mode 100644 virt-sandbox-image/Makefile.am
 create mode 100644 virt-sandbox-image/sources/Source.py
 create mode 100644 virt-sandbox-image/sources/__init__.py
 mode change 100644 => 100755 virt-sandbox-image/virt-sandbox-image.py

diff --git a/.gitignore b/.gitignore
index f77ea12..ef5b5aa 100644
--- a/.gitignore
+++ b/.gitignore
@@ -56,3 +56,4 @@ bin/virt-sandbox
 bin/virt-sandbox-service-util
 build/
 bin/*.1
+bin/virt-sandbox-image
diff --git a/bin/Makefile.am b/bin/Makefile.am
index 416f86f..df4c7dc 100644
--- a/bin/Makefile.am
+++ b/bin/Makefile.am
@@ -3,7 +3,11 @@ bin_PROGRAMS = virt-sandbox
 
 libexec_PROGRAMS = virt-sandbox-service-util
 
-bin_SCRIPTS = virt-sandbox-service
+bin_SCRIPTS = virt-sandbox-service \
+              virt-sandbox-image
+
+virt-sandbox-image: virt-sandbox-image.in
+	sed -e 's,[@]pkgpythondir[@],$(pkgpythondir),g' < $< >$@
 
 virtsandboxcompdir = $(datarootdir)/bash-completion/completions/
 
@@ -20,8 +24,11 @@ POD_FILES = \
 	virt-sandbox-service-reload.pod \
 	virt-sandbox-service-upgrade.pod \
 	$(NULL)
-EXTRA_DIST = $(bin_SCRIPTS) $(POD_FILES) virt-sandbox-service-bash-completion.sh virt-sandbox-service.logrotate
-EXTRA_DIST += virt-sandbox-service-bash-completion.sh
+EXTRA_DIST = virt-sandbox-service \
+             virt-sandbox-image.in \
+             $(POD_FILES) \
+             virt-sandbox-service-bash-completion.sh \
+             virt-sandbox-service.logrotate
 
 man1_MANS = \
 	virt-sandbox.1 \
@@ -64,7 +71,8 @@ virt-sandbox-service-reload.1: virt-sandbox-service-reload.pod Makefile
 virt-sandbox-service-upgrade.1: virt-sandbox-service-upgrade.pod Makefile
 	$(AM_V_GEN)$(POD2MAN) $< $(srcdir)/$@
 
-CLEANFILES = $(man1_MANS)
+CLEANFILES = $(man1_MANS) \
+             virt-sandbox-image
 
 virt_sandbox_SOURCES = virt-sandbox.c
 virt_sandbox_CFLAGS = \
diff --git a/bin/virt-sandbox-image.in b/bin/virt-sandbox-image.in
new file mode 100644
index 0000000..732bb38
--- /dev/null
+++ b/bin/virt-sandbox-image.in
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+exec "@pkgpythondir@/virt-sandbox-image.py" "$@"
diff --git a/configure.ac b/configure.ac
index 8f6da04..69b5870 100644
--- a/configure.ac
+++ b/configure.ac
@@ -124,11 +124,13 @@ dnl Should be in m4/virt-gettext.m4 but intltoolize is too
 dnl dumb to find it there
 IT_PROG_INTLTOOL([0.35.0])
 
+AM_PATH_PYTHON
 
 AC_OUTPUT(Makefile
           libvirt-sandbox/Makefile
           libvirt-sandbox/tests/Makefile
           bin/Makefile
+          virt-sandbox-image/Makefile
           examples/Makefile
           docs/Makefile
           docs/libvirt-sandbox/Makefile
diff --git a/virt-sandbox-image/Makefile.am b/virt-sandbox-image/Makefile.am
new file mode 100644
index 0000000..5ab4d2e
--- /dev/null
+++ b/virt-sandbox-image/Makefile.am
@@ -0,0 +1,13 @@
+
+EXTRA_DIST = \
+	virt-sandbox-image.py \
+	sources
+
+install-data-local:
+	$(mkinstalldirs) $(DESTDIR)/$(pkgpythondir)/sources
+	$(INSTALL) -m 0755 $(srcdir)/virt-sandbox-image.py $(DESTDIR)$(pkgpythondir)
+	$(INSTALL) -m 0644 $(srcdir)/sources/__init__.py $(DESTDIR)$(pkgpythondir)/sources
+	$(INSTALL) -m 0644 $(srcdir)/sources/Source.py $(DESTDIR)$(pkgpythondir)/sources
+
+uninstall-local:
+	rm -f $(DESTDIR)$(pkgpythondir)
diff --git a/virt-sandbox-image/sources/Source.py b/virt-sandbox-image/sources/Source.py
new file mode 100644
index 0000000..43f0720
--- /dev/null
+++ b/virt-sandbox-image/sources/Source.py
@@ -0,0 +1,31 @@
+'''
+*
+* libvirt-sandbox-config-diskaccess.h: libvirt sandbox configuration
+*
+* Copyright (C) 2015 Universitat Politècnica de Catalunya.
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU Lesser General Public
+* License as published by the Free Software Foundation; either
+* version 2.1 of the License, or (at your option) any later version.
+*
+* This library 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
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this library; if not, write to the Free Software
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*
+* Author: Eren Yagdiran <erenyagdiran at gmail.com>
+*
+'''
+#!/usr/bin/python
+
+from abc import ABCMeta, abstractmethod
+
+class Source():
+    __metaclass__ = ABCMeta
+    def __init__(self):
+        pass
diff --git a/virt-sandbox-image/sources/__init__.py b/virt-sandbox-image/sources/__init__.py
new file mode 100644
index 0000000..4830c7a
--- /dev/null
+++ b/virt-sandbox-image/sources/__init__.py
@@ -0,0 +1,29 @@
+'''
+*
+* libvirt-sandbox-config-diskaccess.h: libvirt sandbox configuration
+*
+* Copyright (C) 2015 Universitat Politècnica de Catalunya.
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU Lesser General Public
+* License as published by the Free Software Foundation; either
+* version 2.1 of the License, or (at your option) any later version.
+*
+* This library 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
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this library; if not, write to the Free Software
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*
+* Author: Eren Yagdiran <erenyagdiran at gmail.com>
+*
+'''
+#!/usr/bin/python
+
+import os
+import glob
+modules = glob.glob(os.path.dirname(__file__)+"/*.py")
+__all__ = [ os.path.basename(f)[:-3] for f in modules]
diff --git a/virt-sandbox-image/virt-sandbox-image.py b/virt-sandbox-image/virt-sandbox-image.py
old mode 100644
new mode 100755
index a9cb0ff..b20b240
--- a/virt-sandbox-image/virt-sandbox-image.py
+++ b/virt-sandbox-image/virt-sandbox-image.py
@@ -1,5 +1,5 @@
 #!/usr/bin/python -Es
-#
+# -*- coding: utf-8 -*-
 # Authors: Daniel P. Berrange <berrange at redhat.com>
 #          Eren Yagdiran <erenyagdiran at gmail.com>
 #
@@ -38,6 +38,17 @@ default_template_dir = "/var/lib/libvirt/templates"
 debug = True
 verbose = True
 
+sys.dont_write_bytecode = True
+
+import importlib
+def dynamic_source_loader(name):
+    name = name[0].upper() + name[1:]
+    modname = "sources." + name + "Source"
+    mod = importlib.import_module(modname)
+    classname = name + "Source"
+    classimpl = getattr(mod,classname)
+    return classimpl()
+
 gettext.bindtextdomain("libvirt-sandbox", "/usr/share/locale")
 gettext.textdomain("libvirt-sandbox")
 try:
-- 
2.1.0




More information about the libvir-list mailing list