mirror of https://github.com/tLDP/python-tldp
improve and generalize statfiles and getfileset
statfiles is the heavy lifter; call os.stat() while walking the directory simplify getfileset; use the keys returned in the statinfo dict()
This commit is contained in:
parent
01be89c949
commit
f72583fca6
|
@ -8,7 +8,8 @@ from tempfile import NamedTemporaryFile as ntf
|
||||||
from tldptesttools import TestToolsFilesystem
|
from tldptesttools import TestToolsFilesystem
|
||||||
|
|
||||||
# -- SUT
|
# -- 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):
|
class Test_execute(TestToolsFilesystem):
|
||||||
|
@ -64,9 +65,26 @@ class Test_getfileset(unittest.TestCase):
|
||||||
def test_getfileset(self):
|
def test_getfileset(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__))
|
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):
|
class Test_makefh(unittest.TestCase):
|
||||||
|
|
||||||
def test_makefh(self):
|
def test_makefh(self):
|
||||||
|
|
|
@ -14,7 +14,7 @@ import logging
|
||||||
|
|
||||||
|
|
||||||
def getLogger(**opts):
|
def getLogger(**opts):
|
||||||
level = opts.get('level', logging.DEBUG)
|
level = opts.get('level', logging.INFO)
|
||||||
logging.basicConfig(stream=sys.stderr, level=level)
|
logging.basicConfig(stream=sys.stderr, level=level)
|
||||||
logger = logging.getLogger()
|
logger = logging.getLogger()
|
||||||
return logger
|
return logger
|
||||||
|
@ -83,29 +83,31 @@ def makefh(thing):
|
||||||
|
|
||||||
|
|
||||||
def getfileset(dirname):
|
def getfileset(dirname):
|
||||||
q = set()
|
statinfo = statfiles(dirname)
|
||||||
|
return set(statinfo.keys())
|
||||||
|
|
||||||
|
|
||||||
|
def statfiles(dirname):
|
||||||
|
statinfo = dict()
|
||||||
ocwd = os.getcwd()
|
ocwd = os.getcwd()
|
||||||
os.chdir(dirname)
|
os.chdir(dirname)
|
||||||
for root, dirs, files in os.walk('.'):
|
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)
|
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
|
return statinfo
|
||||||
|
|
||||||
|
|
||||||
def att_statinfo(statinfo, attr='st_mtime', func=max):
|
def att_statinfo(statinfo, attr='st_mtime', func=max):
|
||||||
x = func([getattr(v, attr) for v in statinfo.values()])
|
if statinfo:
|
||||||
return x
|
return func([getattr(v, attr) for v in statinfo.values()])
|
||||||
|
else:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
max_size = functools.partial(att_statinfo, attr='st_size', func=max)
|
max_size = functools.partial(att_statinfo, attr='st_size', func=max)
|
||||||
|
|
Loading…
Reference in New Issue