a bunch of filesystem stat()ing functions

This commit is contained in:
Martin A. Brown 2016-02-16 23:42:22 -08:00
parent f17d164b52
commit 6e3362b9aa
1 changed files with 46 additions and 2 deletions

View File

@ -6,13 +6,15 @@ import os
import io import io
import sys import sys
import errno import errno
import operator
import subprocess import subprocess
import functools
from tempfile import mkstemp from tempfile import mkstemp
import logging import logging
def getLogger(**opts): def getLogger(**opts):
level = opts.get('level', logging.INFO) level = opts.get('level', logging.DEBUG)
logging.basicConfig(stream=sys.stderr, level=level) logging.basicConfig(stream=sys.stderr, level=level)
logger = logging.getLogger() logger = logging.getLogger()
return logger return logger
@ -24,7 +26,7 @@ def execute(cmd, stdin=None, stdout=None, stderr=None,
logdir=None, env=os.environ): logdir=None, env=os.environ):
prefix = os.path.basename(cmd[0]) + '.' + str(os.getpid()) + '-' prefix = os.path.basename(cmd[0]) + '.' + str(os.getpid()) + '-'
if logdir is None: if logdir is None:
raise Exception("Missing required parameter: logdir.") raise Exception("Missing required parameter: logdir.")
if not os.path.isdir(logdir): if not os.path.isdir(logdir):
@ -90,5 +92,47 @@ def getfileset(dirname):
return q 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
max_size = functools.partial(att_statinfo, attr='st_size', func=max)
min_size = functools.partial(att_statinfo, attr='st_size', func=min)
max_mtime = functools.partial(att_statinfo, attr='st_mtime', func=max)
min_mtime = functools.partial(att_statinfo, attr='st_mtime', func=min)
max_ctime = functools.partial(att_statinfo, attr='st_ctime', func=max)
min_ctime = functools.partial(att_statinfo, attr='st_ctime', func=min)
max_atime = functools.partial(att_statinfo, attr='st_atime', func=max)
min_atime = functools.partial(att_statinfo, attr='st_atime', func=min)
def sieve(operand, statinfo, attr='st_mtime', func=operator.gt):
result = set()
for fname, stbuf in statinfo.items():
if func(getattr(stbuf, attr), operand):
result.add(fname)
return result
mtime_gt = functools.partial(sieve, attr='st_mtime', func=operator.gt)
mtime_lt = functools.partial(sieve, attr='st_mtime', func=operator.lt)
size_gt = functools.partial(sieve, attr='st_size', func=operator.gt)
size_lt = functools.partial(sieve, attr='st_size', func=operator.lt)
# #
# -- end of file # -- end of file