Mercurial > cpython-lru_cache-weakref
annotate Lib/distutils/filelist.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 47788c90f80b |
| children |
| rev | line source |
|---|---|
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
1 """distutils.filelist |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
2 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
3 Provides the FileList class, used for poking about the filesystem |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
4 and building lists of files. |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
5 """ |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
6 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
7 __revision__ = "$Id$" |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
8 |
| 55676 | 9 import os, re |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
10 import fnmatch |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
11 from distutils.util import convert_path |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
12 from distutils.errors import DistutilsTemplateError, DistutilsInternalError |
|
23682
d507ad862342
Add missing import of log.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23669
diff
changeset
|
13 from distutils import log |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
14 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
15 class FileList: |
|
13746
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
16 """A list of files built by on exploring the filesystem and filtered by |
|
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
17 applying various patterns to what we find there. |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
18 |
|
13746
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
19 Instance attributes: |
|
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
20 dir |
|
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
21 directory from which files will be taken -- only used if |
|
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
22 'allfiles' not supplied to constructor |
|
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
23 files |
|
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
24 list of filenames currently being built/filtered/manipulated |
|
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
25 allfiles |
|
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
26 complete list of files under consideration (ie. without any |
|
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
27 filtering applied) |
|
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
28 """ |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
29 |
| 55676 | 30 def __init__(self, warn=None, debug_print=None): |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
31 # ignore argument to FileList, but keep them for backwards |
|
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
32 # compatibility |
|
13754
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
33 self.allfiles = None |
|
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
34 self.files = [] |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
35 |
| 55676 | 36 def set_allfiles(self, allfiles): |
|
13754
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
37 self.allfiles = allfiles |
|
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
38 |
| 55676 | 39 def findall(self, dir=os.curdir): |
|
13754
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
40 self.allfiles = findall(dir) |
|
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
41 |
| 55676 | 42 def debug_print(self, msg): |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
43 """Print 'msg' to stdout if the global DEBUG (taken from the |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
44 DISTUTILS_DEBUG environment variable) flag is true. |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
45 """ |
|
25212
12949eced55f
Use distutils.debug.DEBUG instead of distutils.core.DEBUG.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23682
diff
changeset
|
46 from distutils.debug import DEBUG |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
47 if DEBUG: |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
48 print msg |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
49 |
|
13754
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
50 # -- List-like methods --------------------------------------------- |
|
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
51 |
| 55676 | 52 def append(self, item): |
|
13754
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
53 self.files.append(item) |
|
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
54 |
| 55676 | 55 def extend(self, items): |
|
13754
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
56 self.files.extend(items) |
|
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
57 |
| 55676 | 58 def sort(self): |
|
13754
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
59 # Not a strict lexical sort! |
|
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
60 sortable_files = map(os.path.split, self.files) |
|
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
61 sortable_files.sort() |
|
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
62 self.files = [] |
|
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
63 for sort_tuple in sortable_files: |
|
63663
cdf6a3a50407
Merged revisions 79191 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57059
diff
changeset
|
64 self.files.append(os.path.join(*sort_tuple)) |
|
13754
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
65 |
|
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
66 |
|
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
67 # -- Other miscellaneous utility methods --------------------------- |
|
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
68 |
| 55676 | 69 def remove_duplicates(self): |
|
13754
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
70 # Assumes list has been sorted! |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
71 for i in range(len(self.files) - 1, 0, -1): |
|
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
72 if self.files[i] == self.files[i - 1]: |
|
13754
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
73 del self.files[i] |
|
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
74 |
|
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
75 |
|
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
76 # -- "File template" methods --------------------------------------- |
| 21267 | 77 |
| 55676 | 78 def _parse_template_line(self, line): |
| 79 words = line.split() | |
|
13746
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
80 action = words[0] |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
81 |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
82 patterns = dir = dir_pattern = None |
|
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
83 |
|
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
84 if action in ('include', 'exclude', |
|
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
85 'global-include', 'global-exclude'): |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
86 if len(words) < 2: |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
87 raise DistutilsTemplateError, \ |
|
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
88 "'%s' expects <pattern1> <pattern2> ..." % action |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
89 |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
90 patterns = map(convert_path, words[1:]) |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
91 |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
92 elif action in ('recursive-include', 'recursive-exclude'): |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
93 if len(words) < 3: |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
94 raise DistutilsTemplateError, \ |
|
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
95 "'%s' expects <dir> <pattern1> <pattern2> ..." % action |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
96 |
|
13746
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
97 dir = convert_path(words[1]) |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
98 patterns = map(convert_path, words[2:]) |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
99 |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
100 elif action in ('graft', 'prune'): |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
101 if len(words) != 2: |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
102 raise DistutilsTemplateError, \ |
|
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
103 "'%s' expects a single <dir_pattern>" % action |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
104 |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
105 dir_pattern = convert_path(words[1]) |
|
13746
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
106 |
|
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
107 else: |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
108 raise DistutilsTemplateError, "unknown action '%s'" % action |
|
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
109 |
| 13751 | 110 return (action, patterns, dir, dir_pattern) |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
111 |
| 55676 | 112 def process_template_line(self, line): |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
113 # Parse the line: split it up, make sure the right number of words |
|
13748
083b4e547171
Renamed 'select_pattern()' to 'include_pattern()'.
Greg Ward <gward@python.net>
parents:
13747
diff
changeset
|
114 # is there, and return the relevant words. 'action' is always |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
115 # defined: it's the first word of the line. Which of the other |
|
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
116 # three are defined depends on the action; it'll be either |
|
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
117 # patterns, (dir and patterns), or (dir_pattern). |
|
55682
eb10099366ea
added more test coverage for distutils.filelist to prevent regressions when fnmatch or re are changed
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
55676
diff
changeset
|
118 action, patterns, dir, dir_pattern = self._parse_template_line(line) |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
119 |
|
13746
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
120 # OK, now we know that the action is valid and we have the |
|
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
121 # right number of words on the line for that action -- so we |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
122 # can proceed with minimal error-checking. |
|
13746
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
123 if action == 'include': |
| 55676 | 124 self.debug_print("include " + ' '.join(patterns)) |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
125 for pattern in patterns: |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
126 if not self.include_pattern(pattern, anchor=1): |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
127 log.warn("warning: no files found matching '%s'", |
|
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
128 pattern) |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
129 |
|
13746
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
130 elif action == 'exclude': |
| 55676 | 131 self.debug_print("exclude " + ' '.join(patterns)) |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
132 for pattern in patterns: |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
133 if not self.exclude_pattern(pattern, anchor=1): |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
134 log.warn(("warning: no previously-included files " |
|
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
135 "found matching '%s'"), pattern) |
|
13746
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
136 |
|
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
137 elif action == 'global-include': |
| 55676 | 138 self.debug_print("global-include " + ' '.join(patterns)) |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
139 for pattern in patterns: |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
140 if not self.include_pattern(pattern, anchor=0): |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
141 log.warn(("warning: no files found matching '%s' " + |
|
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
142 "anywhere in distribution"), pattern) |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
143 |
|
13746
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
144 elif action == 'global-exclude': |
| 55676 | 145 self.debug_print("global-exclude " + ' '.join(patterns)) |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
146 for pattern in patterns: |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
147 if not self.exclude_pattern(pattern, anchor=0): |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
148 log.warn(("warning: no previously-included files matching " |
|
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
149 "'%s' found anywhere in distribution"), |
|
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
150 pattern) |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
151 |
|
13746
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
152 elif action == 'recursive-include': |
|
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
153 self.debug_print("recursive-include %s %s" % |
| 55676 | 154 (dir, ' '.join(patterns))) |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
155 for pattern in patterns: |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
156 if not self.include_pattern(pattern, prefix=dir): |
|
32162
e53162252fbe
Fix typo (from SF bug #962602)
Walter Dörwald <walter@livinglogic.de>
parents:
26285
diff
changeset
|
157 log.warn(("warning: no files found matching '%s' " + |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
32162
diff
changeset
|
158 "under directory '%s'"), |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
159 pattern, dir) |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
160 |
|
13746
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
161 elif action == 'recursive-exclude': |
|
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
162 self.debug_print("recursive-exclude %s %s" % |
| 55676 | 163 (dir, ' '.join(patterns))) |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
164 for pattern in patterns: |
|
13746
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
165 if not self.exclude_pattern(pattern, prefix=dir): |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
166 log.warn(("warning: no previously-included files matching " |
|
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
167 "'%s' found under directory '%s'"), |
|
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
168 pattern, dir) |
| 21267 | 169 |
|
13746
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
170 elif action == 'graft': |
|
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
171 self.debug_print("graft " + dir_pattern) |
|
13748
083b4e547171
Renamed 'select_pattern()' to 'include_pattern()'.
Greg Ward <gward@python.net>
parents:
13747
diff
changeset
|
172 if not self.include_pattern(None, prefix=dir_pattern): |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
173 log.warn("warning: no directories found matching '%s'", |
|
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
174 dir_pattern) |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
175 |
|
13746
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
176 elif action == 'prune': |
|
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
177 self.debug_print("prune " + dir_pattern) |
|
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
178 if not self.exclude_pattern(None, prefix=dir_pattern): |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
179 log.warn(("no previously-included directories found " + |
|
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
180 "matching '%s'"), dir_pattern) |
|
13746
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
181 else: |
|
13747
7ae9fa409b46
Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents:
13746
diff
changeset
|
182 raise DistutilsInternalError, \ |
|
13746
a44dbbde5ecf
Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents:
13745
diff
changeset
|
183 "this cannot happen: invalid action '%s'" % action |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
184 |
|
13754
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
185 # -- Filtering/selection methods ----------------------------------- |
|
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
186 |
| 55676 | 187 def include_pattern(self, pattern, anchor=1, prefix=None, is_regex=0): |
|
13748
083b4e547171
Renamed 'select_pattern()' to 'include_pattern()'.
Greg Ward <gward@python.net>
parents:
13747
diff
changeset
|
188 """Select strings (presumably filenames) from 'self.files' that |
| 55676 | 189 match 'pattern', a Unix-style wildcard (glob) pattern. |
| 190 | |
| 191 Patterns are not quite the same as implemented by the 'fnmatch' | |
| 192 module: '*' and '?' match non-special characters, where "special" | |
| 193 is platform-dependent: slash on Unix; colon, slash, and backslash on | |
|
13748
083b4e547171
Renamed 'select_pattern()' to 'include_pattern()'.
Greg Ward <gward@python.net>
parents:
13747
diff
changeset
|
194 DOS/Windows; and colon on Mac OS. |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
195 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
196 If 'anchor' is true (the default), then the pattern match is more |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
197 stringent: "*.py" will match "foo.py" but not "foo/bar.py". If |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
198 'anchor' is false, both of these will match. |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
199 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
200 If 'prefix' is supplied, then only filenames starting with 'prefix' |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
201 (itself a pattern) and ending with 'pattern', with anything in between |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
202 them, will match. 'anchor' is ignored in this case. |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
203 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
204 If 'is_regex' is true, 'anchor' and 'prefix' are ignored, and |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
205 'pattern' is assumed to be either a string containing a regex or a |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
206 regex object -- no translation is done, the regex is just compiled |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
207 and used as-is. |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
208 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
209 Selected strings will be added to self.files. |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
210 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
211 Return 1 if files are found. |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
212 """ |
|
75256
47788c90f80b
Fix long-standing bugs with MANIFEST.in parsing on Windows (#6884).
Éric Araujo <merwok@netwok.org>
parents:
74879
diff
changeset
|
213 # XXX docstring lying about what the special chars are? |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
214 files_found = 0 |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
215 pattern_re = translate_pattern(pattern, anchor, prefix, is_regex) |
|
13748
083b4e547171
Renamed 'select_pattern()' to 'include_pattern()'.
Greg Ward <gward@python.net>
parents:
13747
diff
changeset
|
216 self.debug_print("include_pattern: applying regex r'%s'" % |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
217 pattern_re.pattern) |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
218 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
219 # delayed loading of allfiles list |
|
13754
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
220 if self.allfiles is None: |
|
e8dc0a0be4ba
Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents:
13751
diff
changeset
|
221 self.findall() |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
222 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
223 for name in self.allfiles: |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
224 if pattern_re.search(name): |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
225 self.debug_print(" adding " + name) |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
226 self.files.append(name) |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
227 files_found = 1 |
| 21267 | 228 |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
229 return files_found |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
230 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
231 |
| 55676 | 232 def exclude_pattern(self, pattern, anchor=1, prefix=None, is_regex=0): |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
233 """Remove strings (presumably filenames) from 'files' that match |
| 55676 | 234 'pattern'. |
| 235 | |
| 236 Other parameters are the same as for 'include_pattern()', above. | |
| 237 The list 'self.files' is modified in place. Return 1 if files are | |
| 238 found. | |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
239 """ |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
240 files_found = 0 |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
241 pattern_re = translate_pattern(pattern, anchor, prefix, is_regex) |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
242 self.debug_print("exclude_pattern: applying regex r'%s'" % |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
243 pattern_re.pattern) |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
244 for i in range(len(self.files)-1, -1, -1): |
|
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
245 if pattern_re.search(self.files[i]): |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
246 self.debug_print(" removing " + self.files[i]) |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
247 del self.files[i] |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
248 files_found = 1 |
| 21267 | 249 |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
250 return files_found |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
251 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
252 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
253 # ---------------------------------------------------------------------- |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
254 # Utility functions |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
255 |
| 55676 | 256 def findall(dir = os.curdir): |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
257 """Find all files under 'dir' and return the list of full filenames |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
258 (relative to 'dir'). |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
259 """ |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
260 from stat import ST_MODE, S_ISREG, S_ISDIR, S_ISLNK |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
261 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
262 list = [] |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
263 stack = [dir] |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
264 pop = stack.pop |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
265 push = stack.append |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
266 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
267 while stack: |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
268 dir = pop() |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
269 names = os.listdir(dir) |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
270 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
271 for name in names: |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
272 if dir != os.curdir: # avoid the dreaded "./" syndrome |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
273 fullname = os.path.join(dir, name) |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
274 else: |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
275 fullname = name |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
276 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
277 # Avoid excess stat calls -- just one will do, thank you! |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
278 stat = os.stat(fullname) |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
279 mode = stat[ST_MODE] |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
280 if S_ISREG(mode): |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
281 list.append(fullname) |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
282 elif S_ISDIR(mode) and not S_ISLNK(mode): |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
283 push(fullname) |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
284 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
285 return list |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
286 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
287 |
|
52908
7e7813ada2d1
Merged revisions 71280 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
45468
diff
changeset
|
288 def glob_to_re(pattern): |
| 55676 | 289 """Translate a shell-like glob pattern to a regular expression. |
| 290 | |
| 291 Return a string containing the regex. Differs from | |
| 292 'fnmatch.translate()' in that '*' does not match "special characters" | |
| 293 (which are platform-specific). | |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
294 """ |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
295 pattern_re = fnmatch.translate(pattern) |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
296 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
297 # '?' and '*' in the glob pattern become '.' and '.*' in the RE, which |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
298 # IMHO is wrong -- '?' and '*' aren't supposed to match slash in Unix, |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
299 # and by extension they shouldn't match such "special characters" under |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
300 # any OS. So change all non-escaped dots in the RE to match any |
|
75256
47788c90f80b
Fix long-standing bugs with MANIFEST.in parsing on Windows (#6884).
Éric Araujo <merwok@netwok.org>
parents:
74879
diff
changeset
|
301 # character except the special characters (currently: just os.sep). |
|
47788c90f80b
Fix long-standing bugs with MANIFEST.in parsing on Windows (#6884).
Éric Araujo <merwok@netwok.org>
parents:
74879
diff
changeset
|
302 sep = os.sep |
|
47788c90f80b
Fix long-standing bugs with MANIFEST.in parsing on Windows (#6884).
Éric Araujo <merwok@netwok.org>
parents:
74879
diff
changeset
|
303 if os.sep == '\\': |
|
47788c90f80b
Fix long-standing bugs with MANIFEST.in parsing on Windows (#6884).
Éric Araujo <merwok@netwok.org>
parents:
74879
diff
changeset
|
304 # we're using a regex to manipulate a regex, so we need |
|
47788c90f80b
Fix long-standing bugs with MANIFEST.in parsing on Windows (#6884).
Éric Araujo <merwok@netwok.org>
parents:
74879
diff
changeset
|
305 # to escape the backslash twice |
|
47788c90f80b
Fix long-standing bugs with MANIFEST.in parsing on Windows (#6884).
Éric Araujo <merwok@netwok.org>
parents:
74879
diff
changeset
|
306 sep = r'\\\\' |
|
47788c90f80b
Fix long-standing bugs with MANIFEST.in parsing on Windows (#6884).
Éric Araujo <merwok@netwok.org>
parents:
74879
diff
changeset
|
307 escaped = r'\1[^%s]' % sep |
|
47788c90f80b
Fix long-standing bugs with MANIFEST.in parsing on Windows (#6884).
Éric Araujo <merwok@netwok.org>
parents:
74879
diff
changeset
|
308 pattern_re = re.sub(r'((?<!\\)(\\\\)*)\.', escaped, pattern_re) |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
309 return pattern_re |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
310 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
311 |
| 55676 | 312 def translate_pattern(pattern, anchor=1, prefix=None, is_regex=0): |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
313 """Translate a shell-like wildcard pattern to a compiled regular |
| 55676 | 314 expression. |
| 315 | |
| 316 Return the compiled regex. If 'is_regex' true, | |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
317 then 'pattern' is directly compiled to a regex (if it's a string) |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
318 or just returned as-is (assumes it's a regex object). |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
319 """ |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
320 if is_regex: |
| 55676 | 321 if isinstance(pattern, str): |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
322 return re.compile(pattern) |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
323 else: |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
324 return pattern |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
325 |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
326 if pattern: |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
327 pattern_re = glob_to_re(pattern) |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
328 else: |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
329 pattern_re = '' |
| 21267 | 330 |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
331 if prefix is not None: |
|
57059
4a8459e32919
reapplied r74493 (after #6665 fix has been backported)
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52908
diff
changeset
|
332 # ditch end of pattern character |
|
4a8459e32919
reapplied r74493 (after #6665 fix has been backported)
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52908
diff
changeset
|
333 empty_pattern = glob_to_re('') |
| 55676 | 334 prefix_re = glob_to_re(prefix)[:-len(empty_pattern)] |
|
75256
47788c90f80b
Fix long-standing bugs with MANIFEST.in parsing on Windows (#6884).
Éric Araujo <merwok@netwok.org>
parents:
74879
diff
changeset
|
335 sep = os.sep |
|
47788c90f80b
Fix long-standing bugs with MANIFEST.in parsing on Windows (#6884).
Éric Araujo <merwok@netwok.org>
parents:
74879
diff
changeset
|
336 if os.sep == '\\': |
|
47788c90f80b
Fix long-standing bugs with MANIFEST.in parsing on Windows (#6884).
Éric Araujo <merwok@netwok.org>
parents:
74879
diff
changeset
|
337 sep = r'\\' |
|
47788c90f80b
Fix long-standing bugs with MANIFEST.in parsing on Windows (#6884).
Éric Araujo <merwok@netwok.org>
parents:
74879
diff
changeset
|
338 pattern_re = "^" + sep.join((prefix_re, ".*" + pattern_re)) |
|
13745
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
339 else: # no prefix -- respect anchor flag |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
340 if anchor: |
|
5c067c1b5ed0
Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff
changeset
|
341 pattern_re = "^" + pattern_re |
| 21267 | 342 |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
13754
diff
changeset
|
343 return re.compile(pattern_re) |
