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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Marc-André Lureau <marcandre.lureau@xxxxxxxxxx>

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@xxxxxxxxxx>
---
 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

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]