_modified.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """Timestamp comparison of files and groups of files."""
  2. import functools
  3. import os.path
  4. from .errors import DistutilsFileError
  5. from .py39compat import zip_strict
  6. from ._functools import splat
  7. def _newer(source, target):
  8. return not os.path.exists(target) or (
  9. os.path.getmtime(source) > os.path.getmtime(target)
  10. )
  11. def newer(source, target):
  12. """
  13. Is source modified more recently than target.
  14. Returns True if 'source' is modified more recently than
  15. 'target' or if 'target' does not exist.
  16. Raises DistutilsFileError if 'source' does not exist.
  17. """
  18. if not os.path.exists(source):
  19. raise DistutilsFileError("file '%s' does not exist" % os.path.abspath(source))
  20. return _newer(source, target)
  21. def newer_pairwise(sources, targets, newer=newer):
  22. """
  23. Filter filenames where sources are newer than targets.
  24. Walk two filename iterables in parallel, testing if each source is newer
  25. than its corresponding target. Returns a pair of lists (sources,
  26. targets) where source is newer than target, according to the semantics
  27. of 'newer()'.
  28. """
  29. newer_pairs = filter(splat(newer), zip_strict(sources, targets))
  30. return tuple(map(list, zip(*newer_pairs))) or ([], [])
  31. def newer_group(sources, target, missing='error'):
  32. """
  33. Is target out-of-date with respect to any file in sources.
  34. Return True if 'target' is out-of-date with respect to any file
  35. listed in 'sources'. In other words, if 'target' exists and is newer
  36. than every file in 'sources', return False; otherwise return True.
  37. ``missing`` controls how to handle a missing source file:
  38. - error (default): allow the ``stat()`` call to fail.
  39. - ignore: silently disregard any missing source files.
  40. - newer: treat missing source files as "target out of date". This
  41. mode is handy in "dry-run" mode: it will pretend to carry out
  42. commands that wouldn't work because inputs are missing, but
  43. that doesn't matter because dry-run won't run the commands.
  44. """
  45. def missing_as_newer(source):
  46. return missing == 'newer' and not os.path.exists(source)
  47. ignored = os.path.exists if missing == 'ignore' else None
  48. return any(
  49. missing_as_newer(source) or _newer(source, target)
  50. for source in filter(ignored, sources)
  51. )
  52. newer_pairwise_group = functools.partial(newer_pairwise, newer=newer_group)