dep_util.py 3.3 KB

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