/Lib/distutils/dep_util.py

http://unladen-swallow.googlecode.com/ · Python · 97 lines · 76 code · 7 blank · 14 comment · 15 complexity · 1d52c06440c4e454a0758023d1999557 MD5 · raw file

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