0001 """distutils.dep_util 0002 0003 Utility functions for simple, timestamp-based dependency of files 0004 and groups of files; also, function based entirely on such 0005 timestamp dependency analysis.""" 0006 0007 # This module should be kept compatible with Python 1.5.2. 0008 0009 __revision__ = "$Id: dep_util.py,v 1.6 2002/11/19 13:12:27 akuchling Exp $" 0010 0011 import os 0012 from distutils.errors import DistutilsFileError 0013 0014 0015 def newer (source, target): 0016 """Return true if 'source' exists and is more recently modified than 0017 'target', or if 'source' exists and 'target' doesn't. Return false if 0018 both exist and 'target' is the same age or younger than 'source'. 0019 Raise DistutilsFileError if 'source' does not exist. 0020 """ 0021 if not os.path.exists(source): 0022 raise DistutilsFileError, "file '%s' does not exist" % source 0023 if not os.path.exists(target): 0024 return 1 0025 0026 from stat import ST_MTIME 0027 mtime1 = os.stat(source)[ST_MTIME] 0028 mtime2 = os.stat(target)[ST_MTIME] 0029 0030 return mtime1 > mtime2 0031 0032 # newer () 0033 0034 0035 def newer_pairwise (sources, targets): 0036 """Walk two filename lists in parallel, testing if each source is newer 0037 than its corresponding target. Return a pair of lists (sources, 0038 targets) where source is newer than target, according to the semantics 0039 of 'newer()'. 0040 """ 0041 if len(sources) != len(targets): 0042 raise ValueError, "'sources' and 'targets' must be same length" 0043 0044 # build a pair of lists (sources, targets) where source is newer 0045 n_sources = [] 0046 n_targets = [] 0047 for i in range(len(sources)): 0048 if newer(sources[i], targets[i]): 0049 n_sources.append(sources[i]) 0050 n_targets.append(targets[i]) 0051 0052 return (n_sources, n_targets) 0053 0054 # newer_pairwise () 0055 0056 0057 def newer_group (sources, target, missing='error'): 0058 """Return true if 'target' is out-of-date with respect to any file 0059 listed in 'sources'. In other words, if 'target' exists and is newer 0060 than every file in 'sources', return false; otherwise return true. 0061 'missing' controls what we do when a source file is missing; the 0062 default ("error") is to blow up with an OSError from inside 'stat()'; 0063 if it is "ignore", we silently drop any missing source files; if it is 0064 "newer", any missing source files make us assume that 'target' is 0065 out-of-date (this is handy in "dry-run" mode: it'll make you pretend to 0066 carry out commands that wouldn't work because inputs are missing, but 0067 that doesn't matter because you're not actually going to run the 0068 commands). 0069 """ 0070 # If the target doesn't even exist, then it's definitely out-of-date. 0071 if not os.path.exists(target): 0072 return 1 0073 0074 # Otherwise we have to find out the hard way: if *any* source file 0075 # is more recent than 'target', then 'target' is out-of-date and 0076 # we can immediately return true. If we fall through to the end 0077 # of the loop, then 'target' is up-to-date and we return false. 0078 from stat import ST_MTIME 0079 target_mtime = os.stat(target)[ST_MTIME] 0080 for source in sources: 0081 if not os.path.exists(source): 0082 if missing == 'error': # blow up when we stat() the file 0083 pass 0084 elif missing == 'ignore': # missing source dropped from 0085 continue # target's dependency list 0086 elif missing == 'newer': # missing source means target is 0087 return 1 # out-of-date 0088 0089 source_mtime = os.stat(source)[ST_MTIME] 0090 if source_mtime > target_mtime: 0091 return 1 0092 else: 0093 return 0 0094 0095 # newer_group () 0096
Generated by PyXR 0.9.4