annotate Lib/distutils/filelist.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 9a4b19330aa9
children 384f965e12cd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
23669
e32152afde4a Remove unused imports caught by pychecker
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23668
diff changeset
9 import os, string, 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 types import *
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
12 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
13 from distutils.errors import DistutilsTemplateError, DistutilsInternalError
23682
d507ad862342 Add missing import of log.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23669
diff changeset
14 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
15
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
16 class FileList:
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
17
13746
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
18 """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
19 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
20
13746
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
21 Instance attributes:
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
22 dir
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
23 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
24 'allfiles' not supplied to constructor
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
25 files
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
26 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
27 allfiles
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
28 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
29 filtering applied)
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
30 """
13745
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
31
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14863
diff changeset
32 def __init__(self,
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14863
diff changeset
33 warn=None,
13745
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
34 debug_print=None):
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
35 # 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
36 # compatibility
13746
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
37
13754
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
38 self.allfiles = None
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
39 self.files = []
13745
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
40
13754
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
41 def set_allfiles (self, allfiles):
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
42 self.allfiles = allfiles
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
43
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
44 def findall (self, dir=os.curdir):
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
45 self.allfiles = findall(dir)
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
46
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
47 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
48 """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
49 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
50 """
25212
12949eced55f Use distutils.debug.DEBUG instead of distutils.core.DEBUG.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23682
diff changeset
51 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
52 if DEBUG:
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
53 print msg
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
54
13754
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
55 # -- List-like methods ---------------------------------------------
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
56
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
57 def append (self, item):
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
58 self.files.append(item)
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
59
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
60 def extend (self, items):
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
61 self.files.extend(items)
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
62
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
63 def sort (self):
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
64 # Not a strict lexical sort!
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
65 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
66 sortable_files.sort()
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
67 self.files = []
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
68 for sort_tuple in sortable_files:
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
69 self.files.append(apply(os.path.join, sort_tuple))
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
70
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
71
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
72 # -- Other miscellaneous utility methods ---------------------------
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
73
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
74 def remove_duplicates (self):
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
75 # Assumes list has been sorted!
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
76 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
77 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
78 del self.files[i]
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
79
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
80
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
81 # -- "File template" methods ---------------------------------------
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14863
diff changeset
82
13747
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
83 def _parse_template_line (self, line):
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
84 words = string.split(line)
13746
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
85 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
86
13747
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
87 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
88
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
89 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
90 'global-include', 'global-exclude'):
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
91 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
92 raise DistutilsTemplateError, \
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
93 "'%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
94
13747
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
95 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
96
13747
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
97 elif action in ('recursive-include', 'recursive-exclude'):
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
98 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
99 raise DistutilsTemplateError, \
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
100 "'%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
101
13746
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
102 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
103 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
104
13747
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
105 elif action in ('graft', 'prune'):
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
106 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
107 raise DistutilsTemplateError, \
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
108 "'%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
109
13747
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
110 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
111
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
112 else:
13747
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
113 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
114
13751
83dba3f5f7b4 Typo fix.
Greg Ward <gward@python.net>
parents: 13749
diff changeset
115 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
116
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
117 # _parse_template_line ()
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14863
diff changeset
118
13747
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
119
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14863
diff changeset
120 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
121
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
122 # 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
123 # 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
124 # 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
125 # 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
126 # patterns, (dir and patterns), or (dir_pattern).
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
127 (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
128
13746
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
129 # 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
130 # 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
131 # 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
132 if action == 'include':
13747
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
133 self.debug_print("include " + string.join(patterns))
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
134 for pattern in patterns:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
135 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
136 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
137 pattern)
13745
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
138
13746
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
139 elif action == 'exclude':
13747
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
140 self.debug_print("exclude " + string.join(patterns))
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
141 for pattern in patterns:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
142 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
143 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
144 "found matching '%s'"), pattern)
13746
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
145
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
146 elif action == 'global-include':
13747
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
147 self.debug_print("global-include " + string.join(patterns))
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
148 for pattern in patterns:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
149 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
150 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
151 "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
152
13746
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
153 elif action == 'global-exclude':
13747
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
154 self.debug_print("global-exclude " + string.join(patterns))
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.exclude_pattern(pattern, anchor=0):
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
157 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
158 "'%s' found anywhere in distribution"),
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
159 pattern)
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-include':
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
162 self.debug_print("recursive-include %s %s" %
13747
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
163 (dir, string.join(patterns)))
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:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
165 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
166 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
167 "under directory '%s'"),
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
168 pattern, dir)
13745
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
169
13746
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
170 elif action == 'recursive-exclude':
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
171 self.debug_print("recursive-exclude %s %s" %
13747
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
172 (dir, string.join(patterns)))
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
173 for pattern in patterns:
13746
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
174 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
175 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
176 "'%s' found under directory '%s'"),
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
177 pattern, dir)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14863
diff changeset
178
13746
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
179 elif action == 'graft':
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
180 self.debug_print("graft " + dir_pattern)
13748
083b4e547171 Renamed 'select_pattern()' to 'include_pattern()'.
Greg Ward <gward@python.net>
parents: 13747
diff changeset
181 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
182 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
183 dir_pattern)
13745
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
184
13746
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
185 elif action == 'prune':
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
186 self.debug_print("prune " + dir_pattern)
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
187 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
188 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
189 "matching '%s'"), dir_pattern)
13746
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
190 else:
13747
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
191 raise DistutilsInternalError, \
13746
a44dbbde5ecf Added class docstring and ditched inappropriate class attrs.
Greg Ward <gward@python.net>
parents: 13745
diff changeset
192 "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
193
13747
7ae9fa409b46 Renamed 'process_line()' to 'process_template_line()', and factored out
Greg Ward <gward@python.net>
parents: 13746
diff changeset
194 # process_template_line ()
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
13754
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
197 # -- Filtering/selection methods -----------------------------------
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
198
13748
083b4e547171 Renamed 'select_pattern()' to 'include_pattern()'.
Greg Ward <gward@python.net>
parents: 13747
diff changeset
199 def include_pattern (self, pattern,
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
200 anchor=1, prefix=None, is_regex=0):
13748
083b4e547171 Renamed 'select_pattern()' to 'include_pattern()'.
Greg Ward <gward@python.net>
parents: 13747
diff changeset
201 """Select strings (presumably filenames) from 'self.files' that
083b4e547171 Renamed 'select_pattern()' to 'include_pattern()'.
Greg Ward <gward@python.net>
parents: 13747
diff changeset
202 match 'pattern', a Unix-style wildcard (glob) pattern. Patterns
083b4e547171 Renamed 'select_pattern()' to 'include_pattern()'.
Greg Ward <gward@python.net>
parents: 13747
diff changeset
203 are not quite the same as implemented by the 'fnmatch' module: '*'
083b4e547171 Renamed 'select_pattern()' to 'include_pattern()'.
Greg Ward <gward@python.net>
parents: 13747
diff changeset
204 and '?' match non-special characters, where "special" is platform-
083b4e547171 Renamed 'select_pattern()' to 'include_pattern()'.
Greg Ward <gward@python.net>
parents: 13747
diff changeset
205 dependent: slash on Unix; colon, slash, and backslash on
083b4e547171 Renamed 'select_pattern()' to 'include_pattern()'.
Greg Ward <gward@python.net>
parents: 13747
diff changeset
206 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
207
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
208 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
209 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
210 '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
211
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
212 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
213 (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
214 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
215
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
216 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
217 '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
218 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
219 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
220
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
221 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
222
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
223 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
224 """
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
225 files_found = 0
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
226 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
227 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
228 pattern_re.pattern)
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
229
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
230 # delayed loading of allfiles list
13754
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
231 if self.allfiles is None:
e8dc0a0be4ba Added list-like methods: 'append()', 'extend()', 'sort()'.
Greg Ward <gward@python.net>
parents: 13751
diff changeset
232 self.findall()
13745
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
233
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
234 for name in self.allfiles:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
235 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
236 self.debug_print(" adding " + name)
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
237 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
238 files_found = 1
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14863
diff changeset
239
13745
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
240 return files_found
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
241
13748
083b4e547171 Renamed 'select_pattern()' to 'include_pattern()'.
Greg Ward <gward@python.net>
parents: 13747
diff changeset
242 # include_pattern ()
13745
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
243
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
244
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
245 def exclude_pattern (self, pattern,
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
246 anchor=1, prefix=None, is_regex=0):
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
247 """Remove strings (presumably filenames) from 'files' that match
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
248 'pattern'. Other parameters are the same as for
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14863
diff changeset
249 'include_pattern()', above.
13745
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
250 The list 'self.files' is modified in place.
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
251 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
252 """
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
253 files_found = 0
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
254 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
255 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
256 pattern_re.pattern)
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
257 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
258 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
259 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
260 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
261 files_found = 1
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14863
diff changeset
262
13745
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
263 return files_found
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
264
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
265 # exclude_pattern ()
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 # class FileList
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
268
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
269
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 # Utility functions
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
272
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
273 def findall (dir = os.curdir):
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
274 """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
275 (relative to 'dir').
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 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
278
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
279 list = []
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
280 stack = [dir]
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
281 pop = stack.pop
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
282 push = stack.append
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
283
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
284 while stack:
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
285 dir = pop()
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
286 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
287
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
288 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
289 if dir != os.curdir: # avoid the dreaded "./" syndrome
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
290 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
291 else:
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
292 fullname = name
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
293
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
294 # 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
295 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
296 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
297 if S_ISREG(mode):
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
298 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
299 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
300 push(fullname)
13745
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
301
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
302 return list
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
303
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
304
52907
9a4b19330aa9 Fixed #1491431: distutils.filelist.glob_to_re was broken for some edge cases (detailed in the test
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
305 def 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
306 """Translate a shell-like glob pattern to a regular expression; return
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
307 a string containing the regex. Differs from 'fnmatch.translate()' in
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
308 that '*' does not match "special characters" (which are
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
309 platform-specific).
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
310 """
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
311 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
312
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
313 # '?' 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
314 # 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
315 # 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
316 # any OS. So change all non-escaped dots in the RE to match any
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
317 # character except the special characters.
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
318 # XXX currently the "special characters" are just slash -- i.e. this is
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
319 # Unix-only.
52907
9a4b19330aa9 Fixed #1491431: distutils.filelist.glob_to_re was broken for some edge cases (detailed in the test
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
320 pattern_re = re.sub(r'((?<!\\)(\\\\)*)\.', r'\1[^/]', pattern_re)
9a4b19330aa9 Fixed #1491431: distutils.filelist.glob_to_re was broken for some edge cases (detailed in the test
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
321
13745
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
322 return pattern_re
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
323
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
324 # glob_to_re ()
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
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
327 def translate_pattern (pattern, anchor=1, prefix=None, is_regex=0):
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
328 """Translate a shell-like wildcard pattern to a compiled regular
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
329 expression. Return the compiled regex. If 'is_regex' true,
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
330 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
331 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
332 """
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
333 if is_regex:
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
334 if type(pattern) is StringType:
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
335 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
336 else:
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
337 return pattern
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
338
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
339 if pattern:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
340 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
341 else:
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
342 pattern_re = ''
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14863
diff changeset
343
13745
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
344 if prefix is not None:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
345 prefix_re = (glob_to_re(prefix))[0:-1] # ditch trailing $
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
346 pattern_re = "^" + os.path.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
347 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
348 if anchor:
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
349 pattern_re = "^" + pattern_re
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14863
diff changeset
350
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13754
diff changeset
351 return re.compile(pattern_re)
13745
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
352
5c067c1b5ed0 Provides the FileList class for building a list of filenames by exploring
Greg Ward <gward@python.net>
parents:
diff changeset
353 # translate_pattern ()