Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 1 | """distutils.filelist |
| 2 | |
| 3 | Provides the FileList class, used for poking about the filesystem |
| 4 | and building lists of files. |
| 5 | """ |
| 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 | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 9 | __revision__ = "$Id$" |
| 10 | |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 11 | import os, re |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 12 | import fnmatch |
| 13 | from types import * |
| 14 | from glob import glob |
| 15 | from distutils.util import convert_path |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 16 | from distutils.errors import DistutilsTemplateError, DistutilsInternalError |
Jeremy Hylton | 4f2f133 | 2002-06-04 21:04:03 +0000 | [diff] [blame] | 17 | from distutils import log |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 18 | |
| 19 | class FileList: |
| 20 | |
Greg Ward | c98927a | 2000-07-30 00:08:13 +0000 | [diff] [blame] | 21 | """A list of files built by on exploring the filesystem and filtered by |
| 22 | applying various patterns to what we find there. |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 23 | |
Greg Ward | c98927a | 2000-07-30 00:08:13 +0000 | [diff] [blame] | 24 | Instance attributes: |
| 25 | dir |
| 26 | directory from which files will be taken -- only used if |
| 27 | 'allfiles' not supplied to constructor |
| 28 | files |
| 29 | list of filenames currently being built/filtered/manipulated |
| 30 | allfiles |
| 31 | complete list of files under consideration (ie. without any |
| 32 | filtering applied) |
| 33 | """ |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 34 | |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 35 | def __init__(self, |
| 36 | warn=None, |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 37 | debug_print=None): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 38 | # ignore argument to FileList, but keep them for backwards |
| 39 | # compatibility |
Greg Ward | c98927a | 2000-07-30 00:08:13 +0000 | [diff] [blame] | 40 | |
Greg Ward | 979db97 | 2000-07-30 01:45:42 +0000 | [diff] [blame] | 41 | self.allfiles = None |
| 42 | self.files = [] |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 43 | |
Greg Ward | 979db97 | 2000-07-30 01:45:42 +0000 | [diff] [blame] | 44 | def set_allfiles (self, allfiles): |
| 45 | self.allfiles = allfiles |
| 46 | |
| 47 | def findall (self, dir=os.curdir): |
| 48 | self.allfiles = findall(dir) |
| 49 | |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 50 | def debug_print (self, msg): |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 51 | """Print 'msg' to stdout if the global DEBUG (taken from the |
| 52 | DISTUTILS_DEBUG environment variable) flag is true. |
| 53 | """ |
Jeremy Hylton | fcd7353 | 2002-09-11 16:31:53 +0000 | [diff] [blame] | 54 | from distutils.debug import DEBUG |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 55 | if DEBUG: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 56 | print(msg) |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 57 | |
Greg Ward | 979db97 | 2000-07-30 01:45:42 +0000 | [diff] [blame] | 58 | # -- List-like methods --------------------------------------------- |
| 59 | |
| 60 | def append (self, item): |
| 61 | self.files.append(item) |
| 62 | |
| 63 | def extend (self, items): |
| 64 | self.files.extend(items) |
| 65 | |
| 66 | def sort (self): |
| 67 | # Not a strict lexical sort! |
Collin Winter | dc40ae6 | 2007-07-17 00:39:32 +0000 | [diff] [blame] | 68 | sortable_files = sorted(map(os.path.split, self.files)) |
Greg Ward | 979db97 | 2000-07-30 01:45:42 +0000 | [diff] [blame] | 69 | self.files = [] |
| 70 | for sort_tuple in sortable_files: |
Neal Norwitz | d910855 | 2006-03-17 08:00:19 +0000 | [diff] [blame] | 71 | self.files.append(os.path.join(*sort_tuple)) |
Greg Ward | 979db97 | 2000-07-30 01:45:42 +0000 | [diff] [blame] | 72 | |
| 73 | |
| 74 | # -- Other miscellaneous utility methods --------------------------- |
| 75 | |
| 76 | def remove_duplicates (self): |
| 77 | # Assumes list has been sorted! |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 78 | for i in range(len(self.files) - 1, 0, -1): |
| 79 | if self.files[i] == self.files[i - 1]: |
Greg Ward | 979db97 | 2000-07-30 01:45:42 +0000 | [diff] [blame] | 80 | del self.files[i] |
| 81 | |
| 82 | |
| 83 | # -- "File template" methods --------------------------------------- |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 84 | |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 85 | def _parse_template_line (self, line): |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 86 | words = line.split() |
Greg Ward | c98927a | 2000-07-30 00:08:13 +0000 | [diff] [blame] | 87 | action = words[0] |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 88 | |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 89 | patterns = dir = dir_pattern = None |
| 90 | |
| 91 | if action in ('include', 'exclude', |
| 92 | 'global-include', 'global-exclude'): |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 93 | if len(words) < 2: |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 94 | raise DistutilsTemplateError, \ |
| 95 | "'%s' expects <pattern1> <pattern2> ..." % action |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 96 | |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 97 | patterns = map(convert_path, words[1:]) |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 98 | |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 99 | elif action in ('recursive-include', 'recursive-exclude'): |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 100 | if len(words) < 3: |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 101 | raise DistutilsTemplateError, \ |
| 102 | "'%s' expects <dir> <pattern1> <pattern2> ..." % action |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 103 | |
Greg Ward | c98927a | 2000-07-30 00:08:13 +0000 | [diff] [blame] | 104 | dir = convert_path(words[1]) |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 105 | patterns = map(convert_path, words[2:]) |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 106 | |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 107 | elif action in ('graft', 'prune'): |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 108 | if len(words) != 2: |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 109 | raise DistutilsTemplateError, \ |
| 110 | "'%s' expects a single <dir_pattern>" % action |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 111 | |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 112 | dir_pattern = convert_path(words[1]) |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 113 | |
Greg Ward | c98927a | 2000-07-30 00:08:13 +0000 | [diff] [blame] | 114 | else: |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 115 | raise DistutilsTemplateError, "unknown action '%s'" % action |
| 116 | |
Greg Ward | d5dcc17 | 2000-07-30 01:04:22 +0000 | [diff] [blame] | 117 | return (action, patterns, dir, dir_pattern) |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 118 | |
| 119 | # _parse_template_line () |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 120 | |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 121 | |
| 122 | def process_template_line (self, line): |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 123 | |
| 124 | # Parse the line: split it up, make sure the right number of words |
Greg Ward | 0f34185 | 2000-07-30 00:36:25 +0000 | [diff] [blame] | 125 | # is there, and return the relevant words. 'action' is always |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 126 | # defined: it's the first word of the line. Which of the other |
| 127 | # three are defined depends on the action; it'll be either |
| 128 | # patterns, (dir and patterns), or (dir_pattern). |
| 129 | (action, patterns, dir, dir_pattern) = self._parse_template_line(line) |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 130 | |
Greg Ward | c98927a | 2000-07-30 00:08:13 +0000 | [diff] [blame] | 131 | # OK, now we know that the action is valid and we have the |
| 132 | # right number of words on the line for that action -- so we |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 133 | # can proceed with minimal error-checking. |
Greg Ward | c98927a | 2000-07-30 00:08:13 +0000 | [diff] [blame] | 134 | if action == 'include': |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 135 | self.debug_print("include " + ' '.join(patterns)) |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 136 | for pattern in patterns: |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 137 | if not self.include_pattern(pattern, anchor=1): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 138 | log.warn("warning: no files found matching '%s'", |
| 139 | pattern) |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 140 | |
Greg Ward | c98927a | 2000-07-30 00:08:13 +0000 | [diff] [blame] | 141 | elif action == 'exclude': |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 142 | self.debug_print("exclude " + ' '.join(patterns)) |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 143 | for pattern in patterns: |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 144 | if not self.exclude_pattern(pattern, anchor=1): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 145 | log.warn(("warning: no previously-included files " |
| 146 | "found matching '%s'"), pattern) |
Greg Ward | c98927a | 2000-07-30 00:08:13 +0000 | [diff] [blame] | 147 | |
| 148 | elif action == 'global-include': |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 149 | self.debug_print("global-include " + ' '.join(patterns)) |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 150 | for pattern in patterns: |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 151 | if not self.include_pattern(pattern, anchor=0): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 152 | log.warn(("warning: no files found matching '%s' " + |
| 153 | "anywhere in distribution"), pattern) |
Greg Ward | c98927a | 2000-07-30 00:08:13 +0000 | [diff] [blame] | 154 | |
| 155 | elif action == 'global-exclude': |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 156 | self.debug_print("global-exclude " + ' '.join(patterns)) |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 157 | for pattern in patterns: |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 158 | if not self.exclude_pattern(pattern, anchor=0): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 159 | log.warn(("warning: no previously-included files matching " |
| 160 | "'%s' found anywhere in distribution"), |
| 161 | pattern) |
Greg Ward | c98927a | 2000-07-30 00:08:13 +0000 | [diff] [blame] | 162 | |
| 163 | elif action == 'recursive-include': |
| 164 | self.debug_print("recursive-include %s %s" % |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 165 | (dir, ' '.join(patterns))) |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 166 | for pattern in patterns: |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 167 | if not self.include_pattern(pattern, prefix=dir): |
Walter Dörwald | cbd0b36 | 2004-05-31 15:12:27 +0000 | [diff] [blame] | 168 | log.warn(("warning: no files found matching '%s' " + |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 169 | "under directory '%s'"), |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 170 | pattern, dir) |
Greg Ward | c98927a | 2000-07-30 00:08:13 +0000 | [diff] [blame] | 171 | |
| 172 | elif action == 'recursive-exclude': |
| 173 | self.debug_print("recursive-exclude %s %s" % |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 174 | (dir, ' '.join(patterns))) |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 175 | for pattern in patterns: |
Greg Ward | c98927a | 2000-07-30 00:08:13 +0000 | [diff] [blame] | 176 | if not self.exclude_pattern(pattern, prefix=dir): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 177 | log.warn(("warning: no previously-included files matching " |
| 178 | "'%s' found under directory '%s'"), |
| 179 | pattern, dir) |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 180 | |
Greg Ward | c98927a | 2000-07-30 00:08:13 +0000 | [diff] [blame] | 181 | elif action == 'graft': |
| 182 | self.debug_print("graft " + dir_pattern) |
Greg Ward | 0f34185 | 2000-07-30 00:36:25 +0000 | [diff] [blame] | 183 | if not self.include_pattern(None, prefix=dir_pattern): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 184 | log.warn("warning: no directories found matching '%s'", |
| 185 | dir_pattern) |
Greg Ward | c98927a | 2000-07-30 00:08:13 +0000 | [diff] [blame] | 186 | |
| 187 | elif action == 'prune': |
| 188 | self.debug_print("prune " + dir_pattern) |
| 189 | if not self.exclude_pattern(None, prefix=dir_pattern): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 190 | log.warn(("no previously-included directories found " + |
| 191 | "matching '%s'"), dir_pattern) |
Greg Ward | c98927a | 2000-07-30 00:08:13 +0000 | [diff] [blame] | 192 | else: |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 193 | raise DistutilsInternalError, \ |
Greg Ward | c98927a | 2000-07-30 00:08:13 +0000 | [diff] [blame] | 194 | "this cannot happen: invalid action '%s'" % action |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 195 | |
Greg Ward | 7b3d56c | 2000-07-30 00:21:36 +0000 | [diff] [blame] | 196 | # process_template_line () |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 197 | |
| 198 | |
Greg Ward | 979db97 | 2000-07-30 01:45:42 +0000 | [diff] [blame] | 199 | # -- Filtering/selection methods ----------------------------------- |
| 200 | |
Greg Ward | 0f34185 | 2000-07-30 00:36:25 +0000 | [diff] [blame] | 201 | def include_pattern (self, pattern, |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 202 | anchor=1, prefix=None, is_regex=0): |
Greg Ward | 0f34185 | 2000-07-30 00:36:25 +0000 | [diff] [blame] | 203 | """Select strings (presumably filenames) from 'self.files' that |
| 204 | match 'pattern', a Unix-style wildcard (glob) pattern. Patterns |
| 205 | are not quite the same as implemented by the 'fnmatch' module: '*' |
| 206 | and '?' match non-special characters, where "special" is platform- |
| 207 | dependent: slash on Unix; colon, slash, and backslash on |
| 208 | DOS/Windows; and colon on Mac OS. |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 209 | |
| 210 | If 'anchor' is true (the default), then the pattern match is more |
| 211 | stringent: "*.py" will match "foo.py" but not "foo/bar.py". If |
| 212 | 'anchor' is false, both of these will match. |
| 213 | |
| 214 | If 'prefix' is supplied, then only filenames starting with 'prefix' |
| 215 | (itself a pattern) and ending with 'pattern', with anything in between |
| 216 | them, will match. 'anchor' is ignored in this case. |
| 217 | |
| 218 | If 'is_regex' is true, 'anchor' and 'prefix' are ignored, and |
| 219 | 'pattern' is assumed to be either a string containing a regex or a |
| 220 | regex object -- no translation is done, the regex is just compiled |
| 221 | and used as-is. |
| 222 | |
| 223 | Selected strings will be added to self.files. |
| 224 | |
| 225 | Return 1 if files are found. |
| 226 | """ |
| 227 | files_found = 0 |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 228 | pattern_re = translate_pattern(pattern, anchor, prefix, is_regex) |
Greg Ward | 0f34185 | 2000-07-30 00:36:25 +0000 | [diff] [blame] | 229 | self.debug_print("include_pattern: applying regex r'%s'" % |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 230 | pattern_re.pattern) |
| 231 | |
| 232 | # delayed loading of allfiles list |
Greg Ward | 979db97 | 2000-07-30 01:45:42 +0000 | [diff] [blame] | 233 | if self.allfiles is None: |
| 234 | self.findall() |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 235 | |
| 236 | for name in self.allfiles: |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 237 | if pattern_re.search(name): |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 238 | self.debug_print(" adding " + name) |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 239 | self.files.append(name) |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 240 | files_found = 1 |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 241 | |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 242 | return files_found |
| 243 | |
Greg Ward | 0f34185 | 2000-07-30 00:36:25 +0000 | [diff] [blame] | 244 | # include_pattern () |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 245 | |
| 246 | |
| 247 | def exclude_pattern (self, pattern, |
| 248 | anchor=1, prefix=None, is_regex=0): |
| 249 | """Remove strings (presumably filenames) from 'files' that match |
| 250 | 'pattern'. Other parameters are the same as for |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 251 | 'include_pattern()', above. |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 252 | The list 'self.files' is modified in place. |
| 253 | Return 1 if files are found. |
| 254 | """ |
| 255 | files_found = 0 |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 256 | pattern_re = translate_pattern(pattern, anchor, prefix, is_regex) |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 257 | self.debug_print("exclude_pattern: applying regex r'%s'" % |
| 258 | pattern_re.pattern) |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 259 | for i in range(len(self.files)-1, -1, -1): |
| 260 | if pattern_re.search(self.files[i]): |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 261 | self.debug_print(" removing " + self.files[i]) |
| 262 | del self.files[i] |
| 263 | files_found = 1 |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 264 | |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 265 | return files_found |
| 266 | |
| 267 | # exclude_pattern () |
| 268 | |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 269 | # class FileList |
| 270 | |
| 271 | |
| 272 | # ---------------------------------------------------------------------- |
| 273 | # Utility functions |
| 274 | |
| 275 | def findall (dir = os.curdir): |
| 276 | """Find all files under 'dir' and return the list of full filenames |
| 277 | (relative to 'dir'). |
| 278 | """ |
| 279 | from stat import ST_MODE, S_ISREG, S_ISDIR, S_ISLNK |
| 280 | |
| 281 | list = [] |
| 282 | stack = [dir] |
| 283 | pop = stack.pop |
| 284 | push = stack.append |
| 285 | |
| 286 | while stack: |
| 287 | dir = pop() |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 288 | names = os.listdir(dir) |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 289 | |
| 290 | for name in names: |
| 291 | if dir != os.curdir: # avoid the dreaded "./" syndrome |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 292 | fullname = os.path.join(dir, name) |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 293 | else: |
| 294 | fullname = name |
| 295 | |
| 296 | # Avoid excess stat calls -- just one will do, thank you! |
| 297 | stat = os.stat(fullname) |
| 298 | mode = stat[ST_MODE] |
| 299 | if S_ISREG(mode): |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 300 | list.append(fullname) |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 301 | elif S_ISDIR(mode) and not S_ISLNK(mode): |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 302 | push(fullname) |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 303 | |
| 304 | return list |
| 305 | |
| 306 | |
| 307 | def glob_to_re (pattern): |
| 308 | """Translate a shell-like glob pattern to a regular expression; return |
| 309 | a string containing the regex. Differs from 'fnmatch.translate()' in |
| 310 | that '*' does not match "special characters" (which are |
| 311 | platform-specific). |
| 312 | """ |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 313 | pattern_re = fnmatch.translate(pattern) |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 314 | |
| 315 | # '?' and '*' in the glob pattern become '.' and '.*' in the RE, which |
| 316 | # IMHO is wrong -- '?' and '*' aren't supposed to match slash in Unix, |
| 317 | # and by extension they shouldn't match such "special characters" under |
| 318 | # any OS. So change all non-escaped dots in the RE to match any |
| 319 | # character except the special characters. |
| 320 | # XXX currently the "special characters" are just slash -- i.e. this is |
| 321 | # Unix-only. |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 322 | pattern_re = re.sub(r'(^|[^\\])\.', r'\1[^/]', pattern_re) |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 323 | return pattern_re |
| 324 | |
| 325 | # glob_to_re () |
| 326 | |
| 327 | |
| 328 | def translate_pattern (pattern, anchor=1, prefix=None, is_regex=0): |
| 329 | """Translate a shell-like wildcard pattern to a compiled regular |
| 330 | expression. Return the compiled regex. If 'is_regex' true, |
| 331 | then 'pattern' is directly compiled to a regex (if it's a string) |
| 332 | or just returned as-is (assumes it's a regex object). |
| 333 | """ |
| 334 | if is_regex: |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 335 | if isinstance(pattern, basestring): |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 336 | return re.compile(pattern) |
| 337 | else: |
| 338 | return pattern |
| 339 | |
| 340 | if pattern: |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 341 | pattern_re = glob_to_re(pattern) |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 342 | else: |
| 343 | pattern_re = '' |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 344 | |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 345 | if prefix is not None: |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 346 | prefix_re = (glob_to_re(prefix))[0:-1] # ditch trailing $ |
| 347 | pattern_re = "^" + os.path.join(prefix_re, ".*" + pattern_re) |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 348 | else: # no prefix -- respect anchor flag |
| 349 | if anchor: |
| 350 | pattern_re = "^" + pattern_re |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 351 | |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 352 | return re.compile(pattern_re) |
Greg Ward | adc1172 | 2000-07-30 00:04:17 +0000 | [diff] [blame] | 353 | |
| 354 | # translate_pattern () |