diff --git a/tests/test_utils.py b/tests/test_utils.py index 687e06d..bbfbb7c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -8,7 +8,8 @@ from tempfile import NamedTemporaryFile as ntf from tldptesttools import TestToolsFilesystem # -- SUT -from tldp.utils import makefh, which, getfileset, execute +from tldp.utils import makefh, which, execute +from tldp.utils import getfileset, statfiles, att_statinfo class Test_execute(TestToolsFilesystem): @@ -64,9 +65,26 @@ class Test_getfileset(unittest.TestCase): def test_getfileset(self): here = os.path.dirname(os.path.abspath(__file__)) me = os.path.join('.', os.path.basename(__file__)) - self.assertTrue(me in getfileset(here)) + fileset = getfileset(here) + self.assertIsInstance(fileset, set) + self.assertTrue(me in fileset) +class Test_statfiles(unittest.TestCase): + + def test_statfiles(self): + here = os.path.dirname(os.path.abspath(__file__)) + me = os.path.join('.', os.path.basename(__file__)) + statinfo = statfiles(here) + self.assertIsInstance(statinfo, dict) + self.assertTrue(me in statinfo) + + +class Test_att_statinfo(unittest.TestCase): + + def test_max_mtime(self): + pass + class Test_makefh(unittest.TestCase): def test_makefh(self): diff --git a/tldp/utils.py b/tldp/utils.py index 69b352e..a3396fc 100644 --- a/tldp/utils.py +++ b/tldp/utils.py @@ -14,7 +14,7 @@ import logging def getLogger(**opts): - level = opts.get('level', logging.DEBUG) + level = opts.get('level', logging.INFO) logging.basicConfig(stream=sys.stderr, level=level) logger = logging.getLogger() return logger @@ -83,29 +83,31 @@ def makefh(thing): def getfileset(dirname): - q = set() + statinfo = statfiles(dirname) + return set(statinfo.keys()) + + +def statfiles(dirname): + statinfo = dict() ocwd = os.getcwd() os.chdir(dirname) for root, dirs, files in os.walk('.'): - q.update([os.path.join(root, x) for x in files]) + for x in files: + relpath = os.path.join(root, x) + try: + statinfo[relpath] = os.stat(relpath) + except OSError as e: + if e.errno != errno.ENOENT: # -- ho-hum, race condition + raise e os.chdir(ocwd) - return q - - -def statfiles(absdir, fileset): - statinfo = dict() - for fname in fileset: - try: - statinfo[fname] = os.stat(os.path.join(absdir, fname)) - except OSError as e: - if e.errno != errno.ENOENT: # -- ho-hum, race condition - raise e return statinfo def att_statinfo(statinfo, attr='st_mtime', func=max): - x = func([getattr(v, attr) for v in statinfo.values()]) - return x + if statinfo: + return func([getattr(v, attr) for v in statinfo.values()]) + else: + return 0 max_size = functools.partial(att_statinfo, attr='st_size', func=max)