image: ignore OSError on listdir() master
authorMarc-André Lureau <marcandre.lureau@redhat.com>
Thu, 4 Aug 2016 13:01:37 +0000 (17:01 +0400)
committerDaniel P. Berrange <berrange@redhat.com>
Thu, 11 Aug 2016 11:01:45 +0000 (12:01 +0100)
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@redhat.com>

libvirt-sandbox/image/sources/docker.py [changed mode: 0644->0755]
libvirt-sandbox/image/sources/virtbuilder.py [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index f6724af..43e9c32
@@ -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):
old mode 100644 (file)
new mode 100755 (executable)
index fefe0dd..6bd9695
@@ -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