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