From 515595cac9328cbb2a52b3c983ae35b5e8bfee3f Mon Sep 17 00:00:00 2001 From: "Martin A. Brown" Date: Wed, 17 Feb 2016 11:18:47 -0800 Subject: [PATCH] improve statfiles, deprecate ill-conceived fileset statfiles now returns a dict() of statinfo and can handle relative paths --- tests/test_utils.py | 35 +++++++++++++++++++++-------------- tldp/utils.py | 33 ++++++++++++++++----------------- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index bbfbb7c..aeb0a35 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -9,7 +9,7 @@ from tldptesttools import TestToolsFilesystem # -- SUT from tldp.utils import makefh, which, execute -from tldp.utils import getfileset, statfiles, att_statinfo +from tldp.utils import statfiles, att_statinfo class Test_execute(TestToolsFilesystem): @@ -60,24 +60,31 @@ class Test_which(unittest.TestCase): os.unlink(f.name) -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__)) - fileset = getfileset(here) - self.assertIsInstance(fileset, set) - self.assertTrue(me in fileset) - - class Test_statfiles(unittest.TestCase): - def test_statfiles(self): + def test_statfiles_dir_rel(self): + here = os.path.dirname(os.path.abspath(__file__)) + statinfo = statfiles(here, relative=here) + self.assertIsInstance(statinfo, dict) + self.assertTrue(os.path.basename(__file__) in statinfo) + + def test_statfiles_dir_abs(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) + self.assertTrue(__file__ in statinfo) + + def test_statfiles_file_rel(self): + here = os.path.dirname(os.path.abspath(__file__)) + statinfo = statfiles(__file__, relative=here) + self.assertIsInstance(statinfo, dict) + self.assertTrue(os.path.basename(__file__) in statinfo) + + def test_statfiles_file_abs(self): + here = os.path.dirname(os.path.abspath(__file__)) + statinfo = statfiles(__file__) + self.assertIsInstance(statinfo, dict) + self.assertTrue(__file__ in statinfo) class Test_att_statinfo(unittest.TestCase): diff --git a/tldp/utils.py b/tldp/utils.py index a3396fc..41ed952 100644 --- a/tldp/utils.py +++ b/tldp/utils.py @@ -82,24 +82,23 @@ def makefh(thing): return f -def getfileset(dirname): - statinfo = statfiles(dirname) - return set(statinfo.keys()) - - -def statfiles(dirname): +def statfiles(name, relative=None): statinfo = dict() - ocwd = os.getcwd() - os.chdir(dirname) - for root, dirs, files in os.walk('.'): - 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) + if not os.path.isdir(name): + if relative: + relpath = os.path.relpath(name, start=relative) + else: + relpath = name + statinfo[relpath] = os.stat(name) + else: + for root, dirs, files in os.walk(name): + for x in files: + foundpath = os.path.join(root, x) + if relative: + relpath = os.path.relpath(foundpath, start=relative) + else: + relpath = foundpath + statinfo[relpath] = os.stat(foundpath) return statinfo