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

From: Marc-André Lureau <marcandre.lureau@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@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

On Thu, Aug 04, 2016 at 05:01:37PM +0400, marcandre.lureau@redhat.com wrote:
From: Marc-André Lureau <marcandre.lureau@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@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
ACK Regards, Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
participants (2)
-
Daniel P. Berrange
-
marcandre.lureau@redhat.com