From bc810dd36c65f2dc8a8a39c550d4af2968c7c462 Mon Sep 17 00:00:00 2001 From: "Martin A. Brown" Date: Wed, 17 Feb 2016 18:16:20 -0800 Subject: [PATCH] deal with the ENOENT problem in statfiles() --- tests/test_utils.py | 8 ++++++++ tldp/utils.py | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/tests/test_utils.py b/tests/test_utils.py index aeb0a35..144abdd 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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): diff --git a/tldp/utils.py b/tldp/utils.py index a588309..6ed9720 100644 --- a/tldp/utils.py +++ b/tldp/utils.py @@ -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