blob: d74f5e4e92f3edeb5a2868ac049973eef7b245cb [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 +00007import os
8from distutils.errors import DistutilsFileError
9
10
Tarek Ziadé36797272010-07-22 12:50:05 +000011def newer (source, target):
12 """Return true if 'source' exists and is more recently modified than
13 'target', or if 'source' exists and 'target' doesn't. Return false if
14 both exist and 'target' is the same age or younger than 'source'.
15 Raise DistutilsFileError if 'source' does not exist.
Greg Wardca4289f2000-09-26 02:13:49 +000016 """
17 if not os.path.exists(source):
Thomas Wouters89d996e2007-09-08 17:39:28 +000018 raise DistutilsFileError("file '%s' does not exist" %
19 os.path.abspath(source))
Greg Wardca4289f2000-09-26 02:13:49 +000020 if not os.path.exists(target):
Tarek Ziadé36797272010-07-22 12:50:05 +000021 return 1
Greg Wardaebf7062000-04-04 02:05:59 +000022
Tarek Ziadé36797272010-07-22 12:50:05 +000023 from stat import ST_MTIME
24 mtime1 = os.stat(source)[ST_MTIME]
25 mtime2 = os.stat(target)[ST_MTIME]
Greg Wardaebf7062000-04-04 02:05:59 +000026
Tarek Ziadé36797272010-07-22 12:50:05 +000027 return mtime1 > mtime2
28
29# newer ()
30
31
32def newer_pairwise (sources, targets):
Greg Wardaebf7062000-04-04 02:05:59 +000033 """Walk two filename lists in parallel, testing if each source is newer
Greg Wardca4289f2000-09-26 02:13:49 +000034 than its corresponding target. Return a pair of lists (sources,
35 targets) where source is newer than target, according to the semantics
36 of 'newer()'.
37 """
38 if len(sources) != len(targets):
Collin Winter5b7e9d72007-08-30 03:52:21 +000039 raise ValueError("'sources' and 'targets' must be same length")
Greg Wardaebf7062000-04-04 02:05:59 +000040
41 # build a pair of lists (sources, targets) where source is newer
42 n_sources = []
43 n_targets = []
Tarek Ziadé36797272010-07-22 12:50:05 +000044 for i in range(len(sources)):
45 if newer(sources[i], targets[i]):
46 n_sources.append(sources[i])
47 n_targets.append(targets[i])
Greg Wardaebf7062000-04-04 02:05:59 +000048
Tarek Ziadé36797272010-07-22 12:50:05 +000049 return (n_sources, n_targets)
Greg Wardaebf7062000-04-04 02:05:59 +000050
Tarek Ziadé36797272010-07-22 12:50:05 +000051# newer_pairwise ()
52
53
54def newer_group (sources, target, missing='error'):
Greg Wardca4289f2000-09-26 02:13:49 +000055 """Return true if 'target' is out-of-date with respect to any file
Tarek Ziadé36797272010-07-22 12:50:05 +000056 listed in 'sources'. In other words, if 'target' exists and is newer
Greg Wardca4289f2000-09-26 02:13:49 +000057 than every file in 'sources', return false; otherwise return true.
58 'missing' controls what we do when a source file is missing; the
59 default ("error") is to blow up with an OSError from inside 'stat()';
60 if it is "ignore", we silently drop any missing source files; if it is
61 "newer", any missing source files make us assume that 'target' is
62 out-of-date (this is handy in "dry-run" mode: it'll make you pretend to
63 carry out commands that wouldn't work because inputs are missing, but
64 that doesn't matter because you're not actually going to run the
65 commands).
66 """
Greg Wardaebf7062000-04-04 02:05:59 +000067 # If the target doesn't even exist, then it's definitely out-of-date.
Greg Wardca4289f2000-09-26 02:13:49 +000068 if not os.path.exists(target):
Tarek Ziadé36797272010-07-22 12:50:05 +000069 return 1
Fred Drakeb94b8492001-12-06 20:51:35 +000070
Greg Wardaebf7062000-04-04 02:05:59 +000071 # Otherwise we have to find out the hard way: if *any* source file
72 # is more recent than 'target', then 'target' is out-of-date and
73 # we can immediately return true. If we fall through to the end
74 # of the loop, then 'target' is up-to-date and we return false.
Tarek Ziadé36797272010-07-22 12:50:05 +000075 from stat import ST_MTIME
76 target_mtime = os.stat(target)[ST_MTIME]
Greg Wardaebf7062000-04-04 02:05:59 +000077 for source in sources:
Greg Wardca4289f2000-09-26 02:13:49 +000078 if not os.path.exists(source):
Greg Wardaebf7062000-04-04 02:05:59 +000079 if missing == 'error': # blow up when we stat() the file
Fred Drakeb94b8492001-12-06 20:51:35 +000080 pass
81 elif missing == 'ignore': # missing source dropped from
Greg Wardaebf7062000-04-04 02:05:59 +000082 continue # target's dependency list
83 elif missing == 'newer': # missing source means target is
Tarek Ziadé36797272010-07-22 12:50:05 +000084 return 1 # out-of-date
Fred Drakeb94b8492001-12-06 20:51:35 +000085
Tarek Ziadé36797272010-07-22 12:50:05 +000086 source_mtime = os.stat(source)[ST_MTIME]
87 if source_mtime > target_mtime:
88 return 1
89 else:
90 return 0
Greg Wardaebf7062000-04-04 02:05:59 +000091
Tarek Ziadé36797272010-07-22 12:50:05 +000092# newer_group ()