ยปCore Development>Code coverage>Lib/distutils/dep_util.py

Python code coverage for Lib/distutils/dep_util.py

#countcontent
1n/a"""distutils.dep_util
2n/a
3n/aUtility functions for simple, timestamp-based dependency of files
4n/aand groups of files; also, function based entirely on such
5n/atimestamp dependency analysis."""
6n/a
7n/aimport os
8n/afrom distutils.errors import DistutilsFileError
9n/a
10n/a
11n/adef newer (source, target):
12n/a """Return true if 'source' exists and is more recently modified than
13n/a 'target', or if 'source' exists and 'target' doesn't. Return false if
14n/a both exist and 'target' is the same age or younger than 'source'.
15n/a Raise DistutilsFileError if 'source' does not exist.
16n/a """
17n/a if not os.path.exists(source):
18n/a raise DistutilsFileError("file '%s' does not exist" %
19n/a os.path.abspath(source))
20n/a if not os.path.exists(target):
21n/a return 1
22n/a
23n/a from stat import ST_MTIME
24n/a mtime1 = os.stat(source)[ST_MTIME]
25n/a mtime2 = os.stat(target)[ST_MTIME]
26n/a
27n/a return mtime1 > mtime2
28n/a
29n/a# newer ()
30n/a
31n/a
32n/adef newer_pairwise (sources, targets):
33n/a """Walk two filename lists in parallel, testing if each source is newer
34n/a than its corresponding target. Return a pair of lists (sources,
35n/a targets) where source is newer than target, according to the semantics
36n/a of 'newer()'.
37n/a """
38n/a if len(sources) != len(targets):
39n/a raise ValueError("'sources' and 'targets' must be same length")
40n/a
41n/a # build a pair of lists (sources, targets) where source is newer
42n/a n_sources = []
43n/a n_targets = []
44n/a for i in range(len(sources)):
45n/a if newer(sources[i], targets[i]):
46n/a n_sources.append(sources[i])
47n/a n_targets.append(targets[i])
48n/a
49n/a return (n_sources, n_targets)
50n/a
51n/a# newer_pairwise ()
52n/a
53n/a
54n/adef newer_group (sources, target, missing='error'):
55n/a """Return true if 'target' is out-of-date with respect to any file
56n/a listed in 'sources'. In other words, if 'target' exists and is newer
57n/a than every file in 'sources', return false; otherwise return true.
58n/a 'missing' controls what we do when a source file is missing; the
59n/a default ("error") is to blow up with an OSError from inside 'stat()';
60n/a if it is "ignore", we silently drop any missing source files; if it is
61n/a "newer", any missing source files make us assume that 'target' is
62n/a out-of-date (this is handy in "dry-run" mode: it'll make you pretend to
63n/a carry out commands that wouldn't work because inputs are missing, but
64n/a that doesn't matter because you're not actually going to run the
65n/a commands).
66n/a """
67n/a # If the target doesn't even exist, then it's definitely out-of-date.
68n/a if not os.path.exists(target):
69n/a return 1
70n/a
71n/a # Otherwise we have to find out the hard way: if *any* source file
72n/a # is more recent than 'target', then 'target' is out-of-date and
73n/a # we can immediately return true. If we fall through to the end
74n/a # of the loop, then 'target' is up-to-date and we return false.
75n/a from stat import ST_MTIME
76n/a target_mtime = os.stat(target)[ST_MTIME]
77n/a for source in sources:
78n/a if not os.path.exists(source):
79n/a if missing == 'error': # blow up when we stat() the file
80n/a pass
81n/a elif missing == 'ignore': # missing source dropped from
82n/a continue # target's dependency list
83n/a elif missing == 'newer': # missing source means target is
84n/a return 1 # out-of-date
85n/a
86n/a source_mtime = os.stat(source)[ST_MTIME]
87n/a if source_mtime > target_mtime:
88n/a return 1
89n/a else:
90n/a return 0
91n/a
92n/a# newer_group ()