Back to index

python3.2  3.2.2
Classes | Functions
distutils.filelist Namespace Reference

Classes

class  FileList

Functions

def findall
def glob_to_re
def translate_pattern

Detailed Description

distutils.filelist

Provides the FileList class, used for poking about the filesystem
and building lists of files.

Function Documentation

def distutils.filelist.findall (   dir = os.curdir)
Find all files under 'dir' and return the list of full filenames
(relative to 'dir').

Definition at line 244 of file filelist.py.

00244 
00245 def findall(dir=os.curdir):
00246     """Find all files under 'dir' and return the list of full filenames
00247     (relative to 'dir').
00248     """
00249     from stat import ST_MODE, S_ISREG, S_ISDIR, S_ISLNK
00250 
00251     list = []
00252     stack = [dir]
00253     pop = stack.pop
00254     push = stack.append
00255 
00256     while stack:
00257         dir = pop()
00258         names = os.listdir(dir)
00259 
00260         for name in names:
00261             if dir != os.curdir:        # avoid the dreaded "./" syndrome
00262                 fullname = os.path.join(dir, name)
00263             else:
00264                 fullname = name
00265 
00266             # Avoid excess stat calls -- just one will do, thank you!
00267             stat = os.stat(fullname)
00268             mode = stat[ST_MODE]
00269             if S_ISREG(mode):
00270                 list.append(fullname)
00271             elif S_ISDIR(mode) and not S_ISLNK(mode):
00272                 push(fullname)
00273     return list
00274 

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.filelist.glob_to_re (   pattern)
Translate a shell-like glob pattern to a regular expression; return
a string containing the regex.  Differs from 'fnmatch.translate()' in
that '*' does not match "special characters" (which are
platform-specific).

Definition at line 275 of file filelist.py.

00275 
00276 def glob_to_re(pattern):
00277     """Translate a shell-like glob pattern to a regular expression; return
00278     a string containing the regex.  Differs from 'fnmatch.translate()' in
00279     that '*' does not match "special characters" (which are
00280     platform-specific).
00281     """
00282     pattern_re = fnmatch.translate(pattern)
00283 
00284     # '?' and '*' in the glob pattern become '.' and '.*' in the RE, which
00285     # IMHO is wrong -- '?' and '*' aren't supposed to match slash in Unix,
00286     # and by extension they shouldn't match such "special characters" under
00287     # any OS.  So change all non-escaped dots in the RE to match any
00288     # character except the special characters.
00289     # XXX currently the "special characters" are just slash -- i.e. this is
00290     # Unix-only.
00291     pattern_re = re.sub(r'((?<!\\)(\\\\)*)\.', r'\1[^/]', pattern_re)
00292 
00293     return pattern_re
00294 

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.filelist.translate_pattern (   pattern,
  anchor = 1,
  prefix = None,
  is_regex = 0 
)
Translate a shell-like wildcard pattern to a compiled regular
expression.  Return the compiled regex.  If 'is_regex' true,
then 'pattern' is directly compiled to a regex (if it's a string)
or just returned as-is (assumes it's a regex object).

Definition at line 295 of file filelist.py.

00295 
00296 def translate_pattern(pattern, anchor=1, prefix=None, is_regex=0):
00297     """Translate a shell-like wildcard pattern to a compiled regular
00298     expression.  Return the compiled regex.  If 'is_regex' true,
00299     then 'pattern' is directly compiled to a regex (if it's a string)
00300     or just returned as-is (assumes it's a regex object).
00301     """
00302     if is_regex:
00303         if isinstance(pattern, str):
00304             return re.compile(pattern)
00305         else:
00306             return pattern
00307 
00308     if pattern:
00309         pattern_re = glob_to_re(pattern)
00310     else:
00311         pattern_re = ''
00312 
00313     if prefix is not None:
00314         # ditch end of pattern character
00315         empty_pattern = glob_to_re('')
00316         prefix_re = (glob_to_re(prefix))[:-len(empty_pattern)]
00317         pattern_re = "^" + os.path.join(prefix_re, ".*" + pattern_re)
00318     else:                               # no prefix -- respect anchor flag
00319         if anchor:
00320             pattern_re = "^" + pattern_re
00321 
00322     return re.compile(pattern_re)

Here is the call graph for this function:

Here is the caller graph for this function: