annotate Lib/distutils/dep_util.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents aebe3243bb2c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
1 """distutils.dep_util
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
2
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
3 Utility functions for simple, timestamp-based dependency of files
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
4 and groups of files; also, function based entirely on such
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
5 timestamp dependency analysis."""
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
6
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
7 __revision__ = "$Id$"
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
8
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
9 import os
71699
aebe3243bb2c Fix incorrect mtime comparison in distutils (#11933).
Éric Araujo <merwok@netwok.org>
parents: 57642
diff changeset
10 from stat import ST_MTIME
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
11 from distutils.errors import DistutilsFileError
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
12
57642
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
13 def newer(source, target):
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
14 """Tells if the target is newer than the source.
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
15
57642
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
16 Return true if 'source' exists and is more recently modified than
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
17 'target', or if 'source' exists and 'target' doesn't.
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
18
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
19 Return false if both exist and 'target' is the same age or younger
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
20 than 'source'. Raise DistutilsFileError if 'source' does not exist.
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
21
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
22 Note that this test is not very accurate: files created in the same second
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
23 will have the same "age".
14864
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
24 """
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
25 if not os.path.exists(source):
57642
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
26 raise DistutilsFileError("file '%s' does not exist" %
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
27 os.path.abspath(source))
14864
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
28 if not os.path.exists(target):
57642
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
29 return True
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
30
71699
aebe3243bb2c Fix incorrect mtime comparison in distutils (#11933).
Éric Araujo <merwok@netwok.org>
parents: 57642
diff changeset
31 return os.stat(source)[ST_MTIME] > os.stat(target)[ST_MTIME]
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
32
57642
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
33 def newer_pairwise(sources, targets):
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
34 """Walk two filename lists in parallel, testing if each source is newer
14864
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
35 than its corresponding target. Return a pair of lists (sources,
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
36 targets) where source is newer than target, according to the semantics
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
37 of 'newer()'.
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
38 """
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
39 if len(sources) != len(targets):
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
40 raise ValueError, "'sources' and 'targets' must be same length"
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
41
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
42 # build a pair of lists (sources, targets) where source is newer
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
43 n_sources = []
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
44 n_targets = []
57642
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
45 for source, target in zip(sources, targets):
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
46 if newer(source, target):
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
47 n_sources.append(source)
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
48 n_targets.append(target)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
49
57642
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
50 return n_sources, n_targets
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
51
57642
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
52 def newer_group(sources, target, missing='error'):
14864
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
53 """Return true if 'target' is out-of-date with respect to any file
57642
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
54 listed in 'sources'.
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
55
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
56 In other words, if 'target' exists and is newer
14864
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
57 than every file in 'sources', return false; otherwise return true.
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
58 'missing' controls what we do when a source file is missing; the
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
59 default ("error") is to blow up with an OSError from inside 'stat()';
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
60 if it is "ignore", we silently drop any missing source files; if it is
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
61 "newer", any missing source files make us assume that 'target' is
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
62 out-of-date (this is handy in "dry-run" mode: it'll make you pretend to
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
63 carry out commands that wouldn't work because inputs are missing, but
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
64 that doesn't matter because you're not actually going to run the
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
65 commands).
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
66 """
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
67 # If the target doesn't even exist, then it's definitely out-of-date.
14864
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
68 if not os.path.exists(target):
57642
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
69 return True
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14864
diff changeset
70
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
71 # Otherwise we have to find out the hard way: if *any* source file
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
72 # is more recent than 'target', then 'target' is out-of-date and
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
73 # we can immediately return true. If we fall through to the end
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
74 # of the loop, then 'target' is up-to-date and we return false.
71699
aebe3243bb2c Fix incorrect mtime comparison in distutils (#11933).
Éric Araujo <merwok@netwok.org>
parents: 57642
diff changeset
75 target_mtime = os.stat(target)[ST_MTIME]
57642
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
76
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
77 for source in sources:
14864
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
78 if not os.path.exists(source):
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
79 if missing == 'error': # blow up when we stat() the file
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14864
diff changeset
80 pass
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14864
diff changeset
81 elif missing == 'ignore': # missing source dropped from
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
82 continue # target's dependency list
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
83 elif missing == 'newer': # missing source means target is
57642
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
84 return True # out-of-date
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14864
diff changeset
85
71699
aebe3243bb2c Fix incorrect mtime comparison in distutils (#11933).
Éric Araujo <merwok@netwok.org>
parents: 57642
diff changeset
86 if os.stat(source)[ST_MTIME] > target_mtime:
57642
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
87 return True
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
88
57642
9211a5d7d0b4 added test coverage for distutils.dep_util, and cleaned up the module
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
89 return False