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 from __future__ import absolute_import, division, print_function
import os import os
import uuid
import unittest import unittest
from tempfile import NamedTemporaryFile as ntf from tempfile import NamedTemporaryFile as ntf
@ -86,6 +87,13 @@ class Test_statfiles(unittest.TestCase):
self.assertIsInstance(statinfo, dict) self.assertIsInstance(statinfo, dict)
self.assertTrue(__file__ in statinfo) 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): class Test_att_statinfo(unittest.TestCase):

View File

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