mirror of https://github.com/tLDP/python-tldp
improve statfiles, deprecate ill-conceived fileset
statfiles now returns a dict() of statinfo and can handle relative paths
This commit is contained in:
parent
57ae52abde
commit
515595cac9
|
@ -9,7 +9,7 @@ from tldptesttools import TestToolsFilesystem
|
||||||
|
|
||||||
# -- SUT
|
# -- SUT
|
||||||
from tldp.utils import makefh, which, execute
|
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):
|
class Test_execute(TestToolsFilesystem):
|
||||||
|
@ -60,24 +60,31 @@ class Test_which(unittest.TestCase):
|
||||||
os.unlink(f.name)
|
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):
|
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__))
|
here = os.path.dirname(os.path.abspath(__file__))
|
||||||
me = os.path.join('.', os.path.basename(__file__))
|
|
||||||
statinfo = statfiles(here)
|
statinfo = statfiles(here)
|
||||||
self.assertIsInstance(statinfo, dict)
|
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):
|
class Test_att_statinfo(unittest.TestCase):
|
||||||
|
|
|
@ -82,24 +82,23 @@ def makefh(thing):
|
||||||
return f
|
return f
|
||||||
|
|
||||||
|
|
||||||
def getfileset(dirname):
|
def statfiles(name, relative=None):
|
||||||
statinfo = statfiles(dirname)
|
|
||||||
return set(statinfo.keys())
|
|
||||||
|
|
||||||
|
|
||||||
def statfiles(dirname):
|
|
||||||
statinfo = dict()
|
statinfo = dict()
|
||||||
ocwd = os.getcwd()
|
if not os.path.isdir(name):
|
||||||
os.chdir(dirname)
|
if relative:
|
||||||
for root, dirs, files in os.walk('.'):
|
relpath = os.path.relpath(name, start=relative)
|
||||||
for x in files:
|
else:
|
||||||
relpath = os.path.join(root, x)
|
relpath = name
|
||||||
try:
|
statinfo[relpath] = os.stat(name)
|
||||||
statinfo[relpath] = os.stat(relpath)
|
else:
|
||||||
except OSError as e:
|
for root, dirs, files in os.walk(name):
|
||||||
if e.errno != errno.ENOENT: # -- ho-hum, race condition
|
for x in files:
|
||||||
raise e
|
foundpath = os.path.join(root, x)
|
||||||
os.chdir(ocwd)
|
if relative:
|
||||||
|
relpath = os.path.relpath(foundpath, start=relative)
|
||||||
|
else:
|
||||||
|
relpath = foundpath
|
||||||
|
statinfo[relpath] = os.stat(foundpath)
|
||||||
return statinfo
|
return statinfo
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue