deal with the ENOENT problem in statfiles()

This commit is contained in:
Martin A. Brown 2016-02-17 18:16:20 -08:00
parent a0de081099
commit bc810dd36c
2 changed files with 17 additions and 0 deletions

View File

@ -2,6 +2,7 @@
from __future__ import absolute_import, division, print_function
import os
import uuid
import unittest
from tempfile import NamedTemporaryFile as ntf
@ -86,6 +87,13 @@ class Test_statfiles(unittest.TestCase):
self.assertIsInstance(statinfo, dict)
self.assertTrue(__file__ in statinfo)
def test_statfiles_nonexistent_file(self):
here = os.path.dirname(os.path.abspath(__file__))
this = os.path.join(here, str(uuid.uuid4()))
statinfo = statfiles(this)
self.assertIsInstance(statinfo, dict)
self.assertEquals(0, len(statinfo))
class Test_att_statinfo(unittest.TestCase):

View File

@ -12,6 +12,7 @@ import functools
from tempfile import mkstemp
import logging
logdir = 'tldp-document-build-logs'
def getLogger(**opts):
level = opts.get('level', logging.INFO)
@ -90,17 +91,23 @@ def statfile(name):
raise e
if os.path.islink(name):
st = os.lstat(name)
else:
st = None
return st
def statfiles(name, relative=None):
statinfo = dict()
if not os.path.exists(name):
return statinfo
if not os.path.isdir(name):
if relative:
relpath = os.path.relpath(name, start=relative)
else:
relpath = name
statinfo[relpath] = statfile(name)
if statinfo[relpath] is None:
del statinfo[relpath]
else:
for root, dirs, files in os.walk(name):
for x in files:
@ -110,6 +117,8 @@ def statfiles(name, relative=None):
else:
relpath = foundpath
statinfo[relpath] = statfile(foundpath)
if statinfo[relpath] is None:
del statinfo[relpath]
return statinfo