Skip to content

Commit

Permalink
image: ignore OSError on listdir()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
elmarco authored and berrange committed Aug 11, 2016
1 parent ad14f86 commit d6f45b1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
18 changes: 15 additions & 3 deletions libvirt-sandbox/image/sources/docker.py 100644 → 100755
Expand Up @@ -358,7 +358,11 @@ def _was_downloaded(self, image, templatedir):

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):
Expand Down Expand Up @@ -552,7 +556,11 @@ def create_template(self, template, templatedir, connect=None):
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):
Expand Down Expand Up @@ -585,7 +593,11 @@ def delete_template(self, template, templatedir):
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):
Expand Down
6 changes: 5 additions & 1 deletion libvirt-sandbox/image/sources/virtbuilder.py 100644 → 100755
Expand Up @@ -68,7 +68,11 @@ def create_template(self, template, templatedir, connect=None):

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
Expand Down

0 comments on commit d6f45b1

Please sign in to comment.