codekingpro/portable-devtools
114k
1"""Timestamp comparison of files and groups of files."""2 3import functools4import os.path5 6from ._functools import splat7from .errors import DistutilsFileError8from .py39compat import zip_strict9 10 11def _newer(source, target):12 return not os.path.exists(target) or (13 os.path.getmtime(source) > os.path.getmtime(target)14 )15 16 17def newer(source, target):18 """19 Is source modified more recently than target.20 21 Returns True if 'source' is modified more recently than22 'target' or if 'target' does not exist.23 24 Raises DistutilsFileError if 'source' does not exist.25 """26 if not os.path.exists(source):27 raise DistutilsFileError("file '%s' does not exist" % os.path.abspath(source))28 29 return _newer(source, target)30 31 32def newer_pairwise(sources, targets, newer=newer):33 """34 Filter filenames where sources are newer than targets.35 36 Walk two filename iterables in parallel, testing if each source is newer37 than its corresponding target. Returns a pair of lists (sources,38 targets) where source is newer than target, according to the semantics39 of 'newer()'.40 """41 newer_pairs = filter(splat(newer), zip_strict(sources, targets))42 return tuple(map(list, zip(*newer_pairs))) or ([], [])43 44 45def newer_group(sources, target, missing='error'):46 """47 Is target out-of-date with respect to any file in sources.48 49 Return True if 'target' is out-of-date with respect to any file50 listed in 'sources'. In other words, if 'target' exists and is newer51 than every file in 'sources', return False; otherwise return True.52 ``missing`` controls how to handle a missing source file:53 54 - error (default): allow the ``stat()`` call to fail.55 - ignore: silently disregard any missing source files.56 - newer: treat missing source files as "target out of date". This57 mode is handy in "dry-run" mode: it will pretend to carry out58 commands that wouldn't work because inputs are missing, but59 that doesn't matter because dry-run won't run the commands.60 """61 62 def missing_as_newer(source):63 return missing == 'newer' and not os.path.exists(source)64 65 ignored = os.path.exists if missing == 'ignore' else None66 return any(67 missing_as_newer(source) or _newer(source, target)68 for source in filter(ignored, sources)69 )70 71 72newer_pairwise_group = functools.partial(newer_pairwise, newer=newer_group)73 