blob: 2c6d7927f019f949a49cea9f9fa1b6fb6c949b0b [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
Martin v. Löwis5a6601c2004-11-10 22:23:15 +00007# This module should be kept compatible with Python 2.1.
Andrew M. Kuchlingd448f662002-11-19 13:12:28 +00008
Greg Wardaebf7062000-04-04 02:05:59 +00009__revision__ = "$Id$"
10
11import os
12from distutils.errors import DistutilsFileError
13
14
15def newer (source, target):
16 """Return true if 'source' exists and is more recently modified than
Greg Wardca4289f2000-09-26 02:13:49 +000017 'target', or if 'source' exists and 'target' doesn't. Return false if
18 both exist and 'target' is the same age or younger than 'source'.
19 Raise DistutilsFileError if 'source' does not exist.
20 """
21 if not os.path.exists(source):
Skip Montanaro05393132007-09-08 00:34:17 +000022 raise DistutilsFileError, ("file '%s' does not exist" %
23 os.path.abspath(source))
Greg Wardca4289f2000-09-26 02:13:49 +000024 if not os.path.exists(target):
Greg Wardaebf7062000-04-04 02:05:59 +000025 return 1
26
27 from stat import ST_MTIME
28 mtime1 = os.stat(source)[ST_MTIME]
29 mtime2 = os.stat(target)[ST_MTIME]
30
31 return mtime1 > mtime2
32
33# newer ()
34
35
36def newer_pairwise (sources, targets):
37 """Walk two filename lists in parallel, testing if each source is newer
Greg Wardca4289f2000-09-26 02:13:49 +000038 than its corresponding target. Return a pair of lists (sources,
39 targets) where source is newer than target, according to the semantics
40 of 'newer()'.
41 """
42 if len(sources) != len(targets):
Greg Wardaebf7062000-04-04 02:05:59 +000043 raise ValueError, "'sources' and 'targets' must be same length"
44
45 # build a pair of lists (sources, targets) where source is newer
46 n_sources = []
47 n_targets = []
Greg Wardca4289f2000-09-26 02:13:49 +000048 for i in range(len(sources)):
49 if newer(sources[i], targets[i]):
50 n_sources.append(sources[i])
51 n_targets.append(targets[i])
Greg Wardaebf7062000-04-04 02:05:59 +000052
53 return (n_sources, n_targets)
54
55# newer_pairwise ()
56
57
58def newer_group (sources, target, missing='error'):
Greg Wardca4289f2000-09-26 02:13:49 +000059 """Return true if 'target' is out-of-date with respect to any file
60 listed in 'sources'. In other words, if 'target' exists and is newer
61 than every file in 'sources', return false; otherwise return true.
62 'missing' controls what we do when a source file is missing; the
63 default ("error") is to blow up with an OSError from inside 'stat()';
64 if it is "ignore", we silently drop any missing source files; if it is
65 "newer", any missing source files make us assume that 'target' is
66 out-of-date (this is handy in "dry-run" mode: it'll make you pretend to
67 carry out commands that wouldn't work because inputs are missing, but
68 that doesn't matter because you're not actually going to run the
69 commands).
70 """
Greg Wardaebf7062000-04-04 02:05:59 +000071 # If the target doesn't even exist, then it's definitely out-of-date.
Greg Wardca4289f2000-09-26 02:13:49 +000072 if not os.path.exists(target):
Greg Wardaebf7062000-04-04 02:05:59 +000073 return 1
Fred Drakeb94b8492001-12-06 20:51:35 +000074
Greg Wardaebf7062000-04-04 02:05:59 +000075 # Otherwise we have to find out the hard way: if *any* source file
76 # is more recent than 'target', then 'target' is out-of-date and
77 # we can immediately return true. If we fall through to the end
78 # of the loop, then 'target' is up-to-date and we return false.
79 from stat import ST_MTIME
Greg Wardca4289f2000-09-26 02:13:49 +000080 target_mtime = os.stat(target)[ST_MTIME]
Greg Wardaebf7062000-04-04 02:05:59 +000081 for source in sources:
Greg Wardca4289f2000-09-26 02:13:49 +000082 if not os.path.exists(source):
Greg Wardaebf7062000-04-04 02:05:59 +000083 if missing == 'error': # blow up when we stat() the file
Fred Drakeb94b8492001-12-06 20:51:35 +000084 pass
85 elif missing == 'ignore': # missing source dropped from
Greg Wardaebf7062000-04-04 02:05:59 +000086 continue # target's dependency list
87 elif missing == 'newer': # missing source means target is
88 return 1 # out-of-date
Fred Drakeb94b8492001-12-06 20:51:35 +000089
Greg Wardaebf7062000-04-04 02:05:59 +000090 source_mtime = os.stat(source)[ST_MTIME]
91 if source_mtime > target_mtime:
92 return 1
93 else:
94 return 0
95
96# newer_group ()