blob: 97812edaf5bcd207e8e3ba3fafa7c8c3865202a7 [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
7# created 2000/04/03, Greg Ward (extracted from util.py)
8
9__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
17 'target', or if 'source' exists and 'target' doesn't. Return
18 false if both exist and 'target' is the same age or younger than
19 'source'. Raise DistutilsFileError if 'source' does not
20 exist."""
21
22 if not os.path.exists (source):
23 raise DistutilsFileError, "file '%s' does not exist" % source
24 if not os.path.exists (target):
25 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
38 than its corresponding target. Return a pair of lists (sources,
39 targets) where source is newer than target, according to the
40 semantics of 'newer()'."""
41
42 if len (sources) != len (targets):
43 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 = []
48 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])
52
53 return (n_sources, n_targets)
54
55# newer_pairwise ()
56
57
58def newer_group (sources, target, missing='error'):
59 """Return true if 'target' is out-of-date with respect to any
60 file listed in 'sources'. In other words, if 'target' exists and
61 is newer than every file in 'sources', return false; otherwise
62 return true. 'missing' controls what we do when a source file is
63 missing; the default ("error") is to blow up with an OSError from
64 inside 'stat()'; if it is "ignore", we silently drop any missing
65 source files; if it is "newer", any missing source files make us
66 assume that 'target' is out-of-date (this is handy in "dry-run"
67 mode: it'll make you pretend to carry out commands that wouldn't
68 work because inputs are missing, but that doesn't matter because
69 you're not actually going to run the commands)."""
70
71 # If the target doesn't even exist, then it's definitely out-of-date.
72 if not os.path.exists (target):
73 return 1
74
75 # 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
80 target_mtime = os.stat (target)[ST_MTIME]
81 for source in sources:
82 if not os.path.exists (source):
83 if missing == 'error': # blow up when we stat() the file
84 pass
85 elif missing == 'ignore': # missing source dropped from
86 continue # target's dependency list
87 elif missing == 'newer': # missing source means target is
88 return 1 # out-of-date
89
90 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 ()
97
98
99# XXX this isn't used anywhere, and worse, it has the same name as a method
100# in Command with subtly different semantics. (This one just has one
101# source -> one dest; that one has many sources -> one dest.) Nuke it?
102def make_file (src, dst, func, args,
103 verbose=0, update_message=None, noupdate_message=None):
104 """Makes 'dst' from 'src' (both filenames) by calling 'func' with
105 'args', but only if it needs to: i.e. if 'dst' does not exist or
106 'src' is newer than 'dst'."""
107
108 if newer (src, dst):
109 if verbose and update_message:
110 print update_message
111 apply (func, args)
112 else:
113 if verbose and noupdate_message:
114 print noupdate_message
115
116# make_file ()