Issue #7295: Do not use a hardcoded file name in test_tarfile.
[python.git] / Lib / distutils / dep_util.py
blob39eecfb248750965dead46d284f58156abe8364d
1 """distutils.dep_util
3 Utility functions for simple, timestamp-based dependency of files
4 and groups of files; also, function based entirely on such
5 timestamp dependency analysis."""
7 __revision__ = "$Id$"
9 import os
10 from distutils.errors import DistutilsFileError
13 def newer (source, target):
14 """Return true if 'source' exists and is more recently modified than
15 'target', or if 'source' exists and 'target' doesn't. Return false if
16 both exist and 'target' is the same age or younger than 'source'.
17 Raise DistutilsFileError if 'source' does not exist.
18 """
19 if not os.path.exists(source):
20 raise DistutilsFileError, ("file '%s' does not exist" %
21 os.path.abspath(source))
22 if not os.path.exists(target):
23 return 1
25 from stat import ST_MTIME
26 mtime1 = os.stat(source)[ST_MTIME]
27 mtime2 = os.stat(target)[ST_MTIME]
29 return mtime1 > mtime2
31 # newer ()
34 def newer_pairwise (sources, targets):
35 """Walk two filename lists in parallel, testing if each source is newer
36 than its corresponding target. Return a pair of lists (sources,
37 targets) where source is newer than target, according to the semantics
38 of 'newer()'.
39 """
40 if len(sources) != len(targets):
41 raise ValueError, "'sources' and 'targets' must be same length"
43 # build a pair of lists (sources, targets) where source is newer
44 n_sources = []
45 n_targets = []
46 for i in range(len(sources)):
47 if newer(sources[i], targets[i]):
48 n_sources.append(sources[i])
49 n_targets.append(targets[i])
51 return (n_sources, n_targets)
53 # newer_pairwise ()
56 def newer_group (sources, target, missing='error'):
57 """Return true if 'target' is out-of-date with respect to any file
58 listed in 'sources'. In other words, if 'target' exists and is newer
59 than every file in 'sources', return false; otherwise return true.
60 'missing' controls what we do when a source file is missing; the
61 default ("error") is to blow up with an OSError from inside 'stat()';
62 if it is "ignore", we silently drop any missing source files; if it is
63 "newer", any missing source files make us assume that 'target' is
64 out-of-date (this is handy in "dry-run" mode: it'll make you pretend to
65 carry out commands that wouldn't work because inputs are missing, but
66 that doesn't matter because you're not actually going to run the
67 commands).
68 """
69 # If the target doesn't even exist, then it's definitely out-of-date.
70 if not os.path.exists(target):
71 return 1
73 # Otherwise we have to find out the hard way: if *any* source file
74 # is more recent than 'target', then 'target' is out-of-date and
75 # we can immediately return true. If we fall through to the end
76 # of the loop, then 'target' is up-to-date and we return false.
77 from stat import ST_MTIME
78 target_mtime = os.stat(target)[ST_MTIME]
79 for source in sources:
80 if not os.path.exists(source):
81 if missing == 'error': # blow up when we stat() the file
82 pass
83 elif missing == 'ignore': # missing source dropped from
84 continue # target's dependency list
85 elif missing == 'newer': # missing source means target is
86 return 1 # out-of-date
88 source_mtime = os.stat(source)[ST_MTIME]
89 if source_mtime > target_mtime:
90 return 1
91 else:
92 return 0
94 # newer_group ()