dep_util.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. """distutils.dep_util
  2. Utility functions for simple, timestamp-based dependency of files
  3. and groups of files; also, function based entirely on such
  4. timestamp dependency analysis."""
  5. __revision__ = "$Id$"
  6. import os
  7. from stat import ST_MTIME
  8. from distutils.errors import DistutilsFileError
  9. def newer(source, target):
  10. """Tells if the target is newer than the source.
  11. Return true if 'source' exists and is more recently modified than
  12. 'target', or if 'source' exists and 'target' doesn't.
  13. Return false if both exist and 'target' is the same age or younger
  14. than 'source'. Raise DistutilsFileError if 'source' does not exist.
  15. Note that this test is not very accurate: files created in the same second
  16. will have the same "age".
  17. """
  18. if not os.path.exists(source):
  19. raise DistutilsFileError("file '%s' does not exist" %
  20. os.path.abspath(source))
  21. if not os.path.exists(target):
  22. return True
  23. return os.stat(source)[ST_MTIME] > os.stat(target)[ST_MTIME]
  24. def newer_pairwise(sources, targets):
  25. """Walk two filename lists in parallel, testing if each source is newer
  26. than its corresponding target. Return a pair of lists (sources,
  27. targets) where source is newer than target, according to the semantics
  28. of 'newer()'.
  29. """
  30. if len(sources) != len(targets):
  31. raise ValueError, "'sources' and 'targets' must be same length"
  32. # build a pair of lists (sources, targets) where source is newer
  33. n_sources = []
  34. n_targets = []
  35. for source, target in zip(sources, targets):
  36. if newer(source, target):
  37. n_sources.append(source)
  38. n_targets.append(target)
  39. return n_sources, n_targets
  40. def newer_group(sources, target, missing='error'):
  41. """Return true if 'target' is out-of-date with respect to any file
  42. listed in 'sources'.
  43. In other words, if 'target' exists and is newer
  44. than every file in 'sources', return false; otherwise return true.
  45. 'missing' controls what we do when a source file is missing; the
  46. default ("error") is to blow up with an OSError from inside 'stat()';
  47. if it is "ignore", we silently drop any missing source files; if it is
  48. "newer", any missing source files make us assume that 'target' is
  49. out-of-date (this is handy in "dry-run" mode: it'll make you pretend to
  50. carry out commands that wouldn't work because inputs are missing, but
  51. that doesn't matter because you're not actually going to run the
  52. commands).
  53. """
  54. # If the target doesn't even exist, then it's definitely out-of-date.
  55. if not os.path.exists(target):
  56. return True
  57. # Otherwise we have to find out the hard way: if *any* source file
  58. # is more recent than 'target', then 'target' is out-of-date and
  59. # we can immediately return true. If we fall through to the end
  60. # of the loop, then 'target' is up-to-date and we return false.
  61. target_mtime = os.stat(target)[ST_MTIME]
  62. for source in sources:
  63. if not os.path.exists(source):
  64. if missing == 'error': # blow up when we stat() the file
  65. pass
  66. elif missing == 'ignore': # missing source dropped from
  67. continue # target's dependency list
  68. elif missing == 'newer': # missing source means target is
  69. return True # out-of-date
  70. if os.stat(source)[ST_MTIME] > target_mtime:
  71. return True
  72. return False