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: dep_util.py 58054 2007-09-08 17:39:28Z thomas.wouters $"
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.
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):
25
from stat import ST_MTIME
26
mtime1 = os.stat(source)[ST_MTIME]
27
mtime2 = os.stat(target)[ST_MTIME]
29
return mtime1 > mtime2
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
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
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)
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
69
# If the target doesn't even exist, then it's definitely out-of-date.
70
if not os.path.exists(target):
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
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: