Change to flush and close logic to fix #1760556.
[python.git] / Lib / distutils / dep_util.py
blob2c6d7927f019f949a49cea9f9fa1b6fb6c949b0b
1 """distutils.dep_util
3 Utility functions for simple, timestamp-based dependency of files
4 and groups of files; also, function based entirely on such
5 timestamp dependency analysis."""
7 # This module should be kept compatible with Python 2.1.
9 __revision__ = "$Id$"
11 import os
12 from distutils.errors import DistutilsFileError
15 def 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 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):
22 raise DistutilsFileError, ("file '%s' does not exist" %
23 os.path.abspath(source))
24 if not os.path.exists(target):
25 return 1
27 from stat import ST_MTIME
28 mtime1 = os.stat(source)[ST_MTIME]
29 mtime2 = os.stat(target)[ST_MTIME]
31 return mtime1 > mtime2
33 # newer ()
36 def 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 semantics
40 of 'newer()'.
41 """
42 if len(sources) != len(targets):
43 raise ValueError, "'sources' and 'targets' must be same length"
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])
53 return (n_sources, n_targets)
55 # newer_pairwise ()
58 def newer_group (sources, target, missing='error'):
59 """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 """
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
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
90 source_mtime = os.stat(source)[ST_MTIME]
91 if source_mtime > target_mtime:
92 return 1
93 else:
94 return 0
96 # newer_group ()