dep_util.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. import os
  6. from distutils.errors import DistutilsFileError
  7. def newer (source, target):
  8. """Return true if 'source' exists and is more recently modified than
  9. 'target', or if 'source' exists and 'target' doesn't. Return false if
  10. both exist and 'target' is the same age or younger than 'source'.
  11. Raise DistutilsFileError if 'source' does not exist.
  12. """
  13. if not os.path.exists(source):
  14. raise DistutilsFileError("file '%s' does not exist" %
  15. os.path.abspath(source))
  16. if not os.path.exists(target):
  17. return 1
  18. from stat import ST_MTIME
  19. mtime1 = os.stat(source)[ST_MTIME]
  20. mtime2 = os.stat(target)[ST_MTIME]
  21. return mtime1 > mtime2
  22. # newer ()
  23. def newer_pairwise (sources, targets):
  24. """Walk two filename lists in parallel, testing if each source is newer
  25. than its corresponding target. Return a pair of lists (sources,
  26. targets) where source is newer than target, according to the semantics
  27. of 'newer()'.
  28. """
  29. if len(sources) != len(targets):
  30. raise ValueError("'sources' and 'targets' must be same length")
  31. # build a pair of lists (sources, targets) where source is newer
  32. n_sources = []
  33. n_targets = []
  34. for i in range(len(sources)):
  35. if newer(sources[i], targets[i]):
  36. n_sources.append(sources[i])
  37. n_targets.append(targets[i])
  38. return (n_sources, n_targets)
  39. # newer_pairwise ()
  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'. In other words, if 'target' exists and is newer
  43. than every file in 'sources', return false; otherwise return true.
  44. 'missing' controls what we do when a source file is missing; the
  45. default ("error") is to blow up with an OSError from inside 'stat()';
  46. if it is "ignore", we silently drop any missing source files; if it is
  47. "newer", any missing source files make us assume that 'target' is
  48. out-of-date (this is handy in "dry-run" mode: it'll make you pretend to
  49. carry out commands that wouldn't work because inputs are missing, but
  50. that doesn't matter because you're not actually going to run the
  51. commands).
  52. """
  53. # If the target doesn't even exist, then it's definitely out-of-date.
  54. if not os.path.exists(target):
  55. return 1
  56. # Otherwise we have to find out the hard way: if *any* source file
  57. # is more recent than 'target', then 'target' is out-of-date and
  58. # we can immediately return true. If we fall through to the end
  59. # of the loop, then 'target' is up-to-date and we return false.
  60. from stat import ST_MTIME
  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 1 # out-of-date
  70. source_mtime = os.stat(source)[ST_MTIME]
  71. if source_mtime > target_mtime:
  72. return 1
  73. else:
  74. return 0
  75. # newer_group ()