PageRenderTime 27ms CodeModel.GetById 15ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/distutils/dep_util.py

http://unladen-swallow.googlecode.com/
Python | 97 lines | 82 code | 5 blank | 10 comment | 6 complexity | 1d52c06440c4e454a0758023d1999557 MD5 | raw file
 1"""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# This module should be kept compatible with Python 2.1.
 8
 9__revision__ = "$Id: dep_util.py 58049 2007-09-08 00:34:17Z skip.montanaro $"
10
11import os
12
13
14def newer (source, target):
15    """Return true if 'source' exists and is more recently modified than
16    'target', or if 'source' exists and 'target' doesn't.  Return false if
17    both exist and 'target' is the same age or younger than 'source'.
18    Raise DistutilsFileError if 'source' does not exist.
19    """
20    if not os.path.exists(source):
21        # Delay import to speed up interpreter startup.
22        from distutils.errors import DistutilsFileError
23        raise DistutilsFileError, ("file '%s' does not exist" %
24                                   os.path.abspath(source))
25    if not os.path.exists(target):
26        return 1
27
28    from stat import ST_MTIME
29    mtime1 = os.stat(source)[ST_MTIME]
30    mtime2 = os.stat(target)[ST_MTIME]
31
32    return mtime1 > mtime2
33
34# newer ()
35
36
37def newer_pairwise (sources, targets):
38    """Walk two filename lists in parallel, testing if each source is newer
39    than its corresponding target.  Return a pair of lists (sources,
40    targets) where source is newer than target, according to the semantics
41    of 'newer()'.
42    """
43    if len(sources) != len(targets):
44        raise ValueError, "'sources' and 'targets' must be same length"
45
46    # build a pair of lists (sources, targets) where  source is newer
47    n_sources = []
48    n_targets = []
49    for i in range(len(sources)):
50        if newer(sources[i], targets[i]):
51            n_sources.append(sources[i])
52            n_targets.append(targets[i])
53
54    return (n_sources, n_targets)
55
56# newer_pairwise ()
57
58
59def newer_group (sources, target, missing='error'):
60    """Return true if 'target' is out-of-date with respect to any file
61    listed in 'sources'.  In other words, if 'target' exists and is newer
62    than every file in 'sources', return false; otherwise return true.
63    'missing' controls what we do when a source file is missing; the
64    default ("error") is to blow up with an OSError from inside 'stat()';
65    if it is "ignore", we silently drop any missing source files; if it is
66    "newer", any missing source files make us assume that 'target' is
67    out-of-date (this is handy in "dry-run" mode: it'll make you pretend to
68    carry out commands that wouldn't work because inputs are missing, but
69    that doesn't matter because you're not actually going to run the
70    commands).
71    """
72    # If the target doesn't even exist, then it's definitely out-of-date.
73    if not os.path.exists(target):
74        return 1
75
76    # Otherwise we have to find out the hard way: if *any* source file
77    # is more recent than 'target', then 'target' is out-of-date and
78    # we can immediately return true.  If we fall through to the end
79    # of the loop, then 'target' is up-to-date and we return false.
80    from stat import ST_MTIME
81    target_mtime = os.stat(target)[ST_MTIME]
82    for source in sources:
83        if not os.path.exists(source):
84            if missing == 'error':      # blow up when we stat() the file
85                pass
86            elif missing == 'ignore':   # missing source dropped from
87                continue                #  target's dependency list
88            elif missing == 'newer':    # missing source means target is
89                return 1                #  out-of-date
90
91        source_mtime = os.stat(source)[ST_MTIME]
92        if source_mtime > target_mtime:
93            return 1
94    else:
95        return 0
96
97# newer_group ()