blob: 2b759056ea8b2a0042355d715d984c496dae54a6 [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
Éric Araujo33af2632011-08-02 03:16:12 +020010from stat import ST_MTIME
Greg Wardaebf7062000-04-04 02:05:59 +000011from distutils.errors import DistutilsFileError
12
Tarek Ziadé1bd9b5e2009-12-10 15:29:03 +000013def newer(source, target):
14 """Tells if the target is newer than the source.
Greg Wardaebf7062000-04-04 02:05:59 +000015
Tarek Ziadé1bd9b5e2009-12-10 15:29:03 +000016 Return true if 'source' exists and is more recently modified than
17 'target', or if 'source' exists and 'target' doesn't.
18
19 Return false if both exist and 'target' is the same age or younger
20 than 'source'. Raise DistutilsFileError if 'source' does not exist.
21
22 Note that this test is not very accurate: files created in the same second
23 will have the same "age".
Greg Wardca4289f2000-09-26 02:13:49 +000024 """
25 if not os.path.exists(source):
Tarek Ziadé1bd9b5e2009-12-10 15:29:03 +000026 raise DistutilsFileError("file '%s' does not exist" %
27 os.path.abspath(source))
Greg Wardca4289f2000-09-26 02:13:49 +000028 if not os.path.exists(target):
Tarek Ziadé1bd9b5e2009-12-10 15:29:03 +000029 return True
Greg Wardaebf7062000-04-04 02:05:59 +000030
Éric Araujo33af2632011-08-02 03:16:12 +020031 return os.stat(source)[ST_MTIME] > os.stat(target)[ST_MTIME]
Greg Wardaebf7062000-04-04 02:05:59 +000032
Tarek Ziadé1bd9b5e2009-12-10 15:29:03 +000033def newer_pairwise(sources, targets):
Greg Wardaebf7062000-04-04 02:05:59 +000034 """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 = []
Tarek Ziadé1bd9b5e2009-12-10 15:29:03 +000045 for source, target in zip(sources, targets):
46 if newer(source, target):
47 n_sources.append(source)
48 n_targets.append(target)
Greg Wardaebf7062000-04-04 02:05:59 +000049
Tarek Ziadé1bd9b5e2009-12-10 15:29:03 +000050 return n_sources, n_targets
Greg Wardaebf7062000-04-04 02:05:59 +000051
Tarek Ziadé1bd9b5e2009-12-10 15:29:03 +000052def newer_group(sources, target, missing='error'):
Greg Wardca4289f2000-09-26 02:13:49 +000053 """Return true if 'target' is out-of-date with respect to any file
Tarek Ziadé1bd9b5e2009-12-10 15:29:03 +000054 listed in 'sources'.
55
56 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é1bd9b5e2009-12-10 15:29:03 +000069 return True
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.
Éric Araujo33af2632011-08-02 03:16:12 +020075 target_mtime = os.stat(target)[ST_MTIME]
Tarek Ziadé1bd9b5e2009-12-10 15:29:03 +000076
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é1bd9b5e2009-12-10 15:29:03 +000084 return True # out-of-date
Fred Drakeb94b8492001-12-06 20:51:35 +000085
Éric Araujo33af2632011-08-02 03:16:12 +020086 if os.stat(source)[ST_MTIME] > target_mtime:
Tarek Ziadé1bd9b5e2009-12-10 15:29:03 +000087 return True
Greg Wardaebf7062000-04-04 02:05:59 +000088
Tarek Ziadé1bd9b5e2009-12-10 15:29:03 +000089 return False