improve statfiles, deprecate ill-conceived fileset

statfiles now returns a dict() of statinfo and can handle relative paths
This commit is contained in:
Martin A. Brown 2016-02-17 11:18:47 -08:00
parent 57ae52abde
commit 515595cac9
2 changed files with 37 additions and 31 deletions

View File

@ -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):

View File

@ -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