blob: f49665483af0729b38d46d975ced5d693aea8a29 [file] [log] [blame]
Greg Wardaebf7062000-04-04 02:05:59 +00001"""distutils.dep_util
2
3Utility functions for simple, timestamp-based dependency of files
4and groups of files; also, function based entirely on such
5timestamp dependency analysis."""
6
Greg Wardaebf7062000-04-04 02:05:59 +00007__revision__ = "$Id$"
8
9import os
10from distutils.errors import DistutilsFileError
11
12
13def newer (source, target):
14 """Return true if 'source' exists and is more recently modified than
Greg Wardca4289f2000-09-26 02:13:49 +000015 '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):
Greg Wardaebf7062000-04-04 02:05:59 +000020 raise DistutilsFileError, "file '%s' does not exist" % source
Greg Wardca4289f2000-09-26 02:13:49 +000021 if not os.path.exists(target):
Greg Wardaebf7062000-04-04 02:05:59 +000022 return 1
23
24 from stat import ST_MTIME
25 mtime1 = os.stat(source)[ST_MTIME]
26 mtime2 = os.stat(target)[ST_MTIME]
27
28 return mtime1 > mtime2
29
30# newer ()
31
32
33def newer_pairwise (sources, targets):
34 """Walk two filename lists in parallel, testing if each source is newer
Greg Wardca4289f2000-09-26 02:13:49 +000035 than its corresponding target. Return a pair of lists (sources,
36 targets) where source is newer than target, according to the semantics
37 of 'newer()'.
38 """
39 if len(sources) != len(targets):
Greg Wardaebf7062000-04-04 02:05:59 +000040 raise ValueError, "'sources' and 'targets' must be same length"
41
42 # build a pair of lists (sources, targets) where source is newer
43 n_sources = []
44 n_targets = []
Greg Wardca4289f2000-09-26 02:13:49 +000045 for i in range(len(sources)):
46 if newer(sources[i], targets[i]):
47 n_sources.append(sources[i])
48 n_targets.append(targets[i])
Greg Wardaebf7062000-04-04 02:05:59 +000049
50 return (n_sources, n_targets)
51
52# newer_pairwise ()
53
54
55def newer_group (sources, target, missing='error'):
Greg Wardca4289f2000-09-26 02:13:49 +000056 """Return true if 'target' is out-of-date with respect to any file
57 listed in 'sources'. In other words, if 'target' exists and is newer
58 than every file in 'sources', return false; otherwise return true.
59 'missing' controls what we do when a source file is missing; the
60 default ("error") is to blow up with an OSError from inside 'stat()';
61 if it is "ignore", we silently drop any missing source files; if it is
62 "newer", any missing source files make us assume that 'target' is
63 out-of-date (this is handy in "dry-run" mode: it'll make you pretend to
64 carry out commands that wouldn't work because inputs are missing, but
65 that doesn't matter because you're not actually going to run the
66 commands).
67 """
Greg Wardaebf7062000-04-04 02:05:59 +000068 # If the target doesn't even exist, then it's definitely out-of-date.
Greg Wardca4289f2000-09-26 02:13:49 +000069 if not os.path.exists(target):
Greg Wardaebf7062000-04-04 02:05:59 +000070 return 1
Fred Drakeb94b8492001-12-06 20:51:35 +000071
Greg Wardaebf7062000-04-04 02:05:59 +000072 # Otherwise we have to find out the hard way: if *any* source file
73 # is more recent than 'target', then 'target' is out-of-date and
74 # we can immediately return true. If we fall through to the end
75 # of the loop, then 'target' is up-to-date and we return false.
76 from stat import ST_MTIME
Greg Wardca4289f2000-09-26 02:13:49 +000077 target_mtime = os.stat(target)[ST_MTIME]
Greg Wardaebf7062000-04-04 02:05:59 +000078 for source in sources:
Greg Wardca4289f2000-09-26 02:13:49 +000079 if not os.path.exists(source):
Greg Wardaebf7062000-04-04 02:05:59 +000080 if missing == 'error': # blow up when we stat() the file
Fred Drakeb94b8492001-12-06 20:51:35 +000081 pass
82 elif missing == 'ignore': # missing source dropped from
Greg Wardaebf7062000-04-04 02:05:59 +000083 continue # target's dependency list
84 elif missing == 'newer': # missing source means target is
85 return 1 # out-of-date
Fred Drakeb94b8492001-12-06 20:51:35 +000086
Greg Wardaebf7062000-04-04 02:05:59 +000087 source_mtime = os.stat(source)[ST_MTIME]
88 if source_mtime > target_mtime:
89 return 1
90 else:
91 return 0
92
93# newer_group ()