[libvirt] [PATCH libvirt-sanbox] image: ignore OSError on listdir()

marcandre.lureau at redhat.com marcandre.lureau at redhat.com
Thu Aug 4 13:01:37 UTC 2016


From: Marc-André Lureau <marcandre.lureau at redhat.com>

If the directory to list is missing, don't raise an exception but
return empty list instead. This fixes for example running
"virt-sandbox-image list" without a
~/.local/share/libvirt/templates/virt-builder.

Signed-off-by: Marc-André Lureau <marcandre.lureau at redhat.com>
---
 libvirt-sandbox/image/sources/docker.py      | 18 +++++++++++++++---
 libvirt-sandbox/image/sources/virtbuilder.py |  6 +++++-
 2 files changed, 20 insertions(+), 4 deletions(-)
 mode change 100644 => 100755 libvirt-sandbox/image/sources/docker.py
 mode change 100644 => 100755 libvirt-sandbox/image/sources/virtbuilder.py

diff --git a/libvirt-sandbox/image/sources/docker.py b/libvirt-sandbox/image/sources/docker.py
old mode 100644
new mode 100755
index f6724af..43e9c32
--- a/libvirt-sandbox/image/sources/docker.py
+++ b/libvirt-sandbox/image/sources/docker.py
@@ -358,7 +358,11 @@ class DockerSource(base.Source):
 
     def list_templates(self, templatedir):
         indexes = []
-        imagedirs = os.listdir(templatedir)
+        try:
+            imagedirs = os.listdir(templatedir)
+        except OSError:
+            return []
+
         for imagetagid in imagedirs:
             indexfile = templatedir + "/" + imagetagid + "/index.json"
             if os.path.exists(indexfile):
@@ -552,7 +556,11 @@ class DockerSource(base.Source):
     def _get_image_list(self, image, templatedir):
         imageparent = {}
         imagenames = {}
-        imagedirs = os.listdir(templatedir)
+        imagedirs = []
+        try:
+            imagedirs = os.listdir(templatedir)
+        except OSError:
+            pass
         for imagetagid in imagedirs:
             indexfile = templatedir + "/" + imagetagid + "/index.json"
             if os.path.exists(indexfile):
@@ -585,7 +593,11 @@ class DockerSource(base.Source):
         imageusage = {}
         imageparent = {}
         imagenames = {}
-        imagedirs = os.listdir(templatedir)
+        imagedirs = []
+        try:
+            imagedirs = os.listdir(templatedir)
+        except OSError:
+            pass
         for imagetagid in imagedirs:
             indexfile = templatedir + "/" + imagetagid + "/index.json"
             if os.path.exists(indexfile):
diff --git a/libvirt-sandbox/image/sources/virtbuilder.py b/libvirt-sandbox/image/sources/virtbuilder.py
old mode 100644
new mode 100755
index fefe0dd..6bd9695
--- a/libvirt-sandbox/image/sources/virtbuilder.py
+++ b/libvirt-sandbox/image/sources/virtbuilder.py
@@ -68,7 +68,11 @@ class VirtBuilderSource(base.Source):
 
     def list_templates(self, templatedir):
         files = []
-        imagefiles = os.listdir(templatedir)
+        try:
+            imagefiles = os.listdir(templatedir)
+        except OSError:
+            return []
+
         for filename in imagefiles:
             if not filename.endswith(".qcow2"):
                 continue
-- 
2.9.0




More information about the libvir-list mailing list