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