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