| 1 |
"""distutils.dep_util |
| 2 |
|
| 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.""" |
| 6 |
|
| 7 |
import os |
| 8 |
from distutils.errors import DistutilsFileError |
| 9 |
|
| 10 |
|
| 11 |
def newer (source, target): |
| 12 |
"""Return true if 'source' exists and is more recently modified than |
| 13 |
'target', or if 'source' exists and 'target' doesn't. Return false if |
| 14 |
both exist and 'target' is the same age or younger than 'source'. |
| 15 |
Raise DistutilsFileError if 'source' does not exist. |
| 16 |
""" |
| 17 |
if not os.path.exists(source): |
| 18 |
raise DistutilsFileError("file '%s' does not exist" % |
| 19 |
os.path.abspath(source)) |
| 20 |
if not os.path.exists(target): |
| 21 |
return 1 |
| 22 |
|
| 23 |
from stat import ST_MTIME |
| 24 |
mtime1 = os.stat(source)[ST_MTIME] |
| 25 |
mtime2 = os.stat(target)[ST_MTIME] |
| 26 |
|
| 27 |
return mtime1 > mtime2 |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
def newer_pairwise (sources, targets): |
| 33 |
"""Walk two filename lists in parallel, testing if each source is newer |
| 34 |
than its corresponding target. Return a pair of lists (sources, |
| 35 |
targets) where source is newer than target, according to the semantics |
| 36 |
of 'newer()'. |
| 37 |
""" |
| 38 |
if len(sources) != len(targets): |
| 39 |
raise ValueError("'sources' and 'targets' must be same length") |
| 40 |
|
| 41 |
|
| 42 |
n_sources = [] |
| 43 |
n_targets = [] |
| 44 |
for i in range(len(sources)): |
| 45 |
if newer(sources[i], targets[i]): |
| 46 |
n_sources.append(sources[i]) |
| 47 |
n_targets.append(targets[i]) |
| 48 |
|
| 49 |
return (n_sources, n_targets) |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
def newer_group (sources, target, missing='error'): |
| 55 |
"""Return true if 'target' is out-of-date with respect to any file |
| 56 |
listed in 'sources'. In other words, if 'target' exists and is newer |
| 57 |
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 |
""" |
| 67 |
|
| 68 |
if not os.path.exists(target): |
| 69 |
return 1 |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
from stat import ST_MTIME |
| 76 |
target_mtime = os.stat(target)[ST_MTIME] |
| 77 |
for source in sources: |
| 78 |
if not os.path.exists(source): |
| 79 |
if missing == 'error': |
| 80 |
pass |
| 81 |
elif missing == 'ignore': |
| 82 |
continue |
| 83 |
elif missing == 'newer': |
| 84 |
return 1 |
| 85 |
|
| 86 |
source_mtime = os.stat(source)[ST_MTIME] |
| 87 |
if source_mtime > target_mtime: |
| 88 |
return 1 |
| 89 |
else: |
| 90 |
return 0 |
| 91 |
|
| 92 |
|