_modified.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """Timestamp comparison of files and groups of files."""
  2. from __future__ import annotations
  3. import functools
  4. import os.path
  5. from collections.abc import Callable, Iterable
  6. from typing import Literal, TypeVar
  7. from jaraco.functools import splat
  8. from .compat.py39 import zip_strict
  9. from .errors import DistutilsFileError
  10. _SourcesT = TypeVar(
  11. "_SourcesT", bound="str | bytes | os.PathLike[str] | os.PathLike[bytes]"
  12. )
  13. _TargetsT = TypeVar(
  14. "_TargetsT", bound="str | bytes | os.PathLike[str] | os.PathLike[bytes]"
  15. )
  16. def _newer(source, target):
  17. return not os.path.exists(target) or (
  18. os.path.getmtime(source) > os.path.getmtime(target)
  19. )
  20. def newer(
  21. source: str | bytes | os.PathLike[str] | os.PathLike[bytes],
  22. target: str | bytes | os.PathLike[str] | os.PathLike[bytes],
  23. ) -> bool:
  24. """
  25. Is source modified more recently than target.
  26. Returns True if 'source' is modified more recently than
  27. 'target' or if 'target' does not exist.
  28. Raises DistutilsFileError if 'source' does not exist.
  29. """
  30. if not os.path.exists(source):
  31. raise DistutilsFileError(f"file {os.path.abspath(source)!r} does not exist")
  32. return _newer(source, target)
  33. def newer_pairwise(
  34. sources: Iterable[_SourcesT],
  35. targets: Iterable[_TargetsT],
  36. newer: Callable[[_SourcesT, _TargetsT], bool] = newer,
  37. ) -> tuple[list[_SourcesT], list[_TargetsT]]:
  38. """
  39. Filter filenames where sources are newer than targets.
  40. Walk two filename iterables in parallel, testing if each source is newer
  41. than its corresponding target. Returns a pair of lists (sources,
  42. targets) where source is newer than target, according to the semantics
  43. of 'newer()'.
  44. """
  45. newer_pairs = filter(splat(newer), zip_strict(sources, targets))
  46. return tuple(map(list, zip(*newer_pairs))) or ([], [])
  47. def newer_group(
  48. sources: Iterable[str | bytes | os.PathLike[str] | os.PathLike[bytes]],
  49. target: str | bytes | os.PathLike[str] | os.PathLike[bytes],
  50. missing: Literal["error", "ignore", "newer"] = "error",
  51. ) -> bool:
  52. """
  53. Is target out-of-date with respect to any file in sources.
  54. Return True if 'target' is out-of-date with respect to any file
  55. listed in 'sources'. In other words, if 'target' exists and is newer
  56. than every file in 'sources', return False; otherwise return True.
  57. ``missing`` controls how to handle a missing source file:
  58. - error (default): allow the ``stat()`` call to fail.
  59. - ignore: silently disregard any missing source files.
  60. - newer: treat missing source files as "target out of date". This
  61. mode is handy in "dry-run" mode: it will pretend to carry out
  62. commands that wouldn't work because inputs are missing, but
  63. that doesn't matter because dry-run won't run the commands.
  64. """
  65. def missing_as_newer(source):
  66. return missing == 'newer' and not os.path.exists(source)
  67. ignored = os.path.exists if missing == 'ignore' else None
  68. return not os.path.exists(target) or any(
  69. missing_as_newer(source) or _newer(source, target)
  70. for source in filter(ignored, sources)
  71. )
  72. newer_pairwise_group = functools.partial(newer_pairwise, newer=newer_group)