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