[libvirt] [sandbox PATCH v4 03/21] Image: Add Hooking Mechanism

Eren Yagdiran erenyagdiran at gmail.com
Fri Aug 28 13:47:31 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     | 27 +++++++++++++++++++++++++++
 virt-sandbox-image/sources/__init__.py   | 26 ++++++++++++++++++++++++++
 virt-sandbox-image/virt-sandbox-image.py | 15 +++++++++------
 8 files changed, 93 insertions(+), 10 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..508ca80
--- /dev/null
+++ b/virt-sandbox-image/sources/Source.py
@@ -0,0 +1,27 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+#
+# 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>
+
+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..df8603b
--- /dev/null
+++ b/virt-sandbox-image/sources/__init__.py
@@ -0,0 +1,26 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+#
+# 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>
+#
+
+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..fa9e1c8
--- 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>
 #
@@ -32,11 +32,14 @@ import sys
 import urllib2
 import subprocess
 
-default_index_server = "index.docker.io"
-default_template_dir = "/var/lib/libvirt/templates"
-
-debug = True
-verbose = 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")
-- 
2.1.0




More information about the libvir-list mailing list