General cleanup, raise normalization in Lib/distutils.

This commit is contained in:
Collin Winter 2007-08-30 03:52:21 +00:00
parent a73bfee73d
commit 5b7e9d76f3
47 changed files with 963 additions and 1640 deletions

View file

@ -4,20 +4,16 @@ Provides the FileList class, used for poking about the filesystem
and building lists of files.
"""
# This module should be kept compatible with Python 2.1.
__revision__ = "$Id$"
import os, re
import fnmatch
from types import *
from glob import glob
from distutils.util import convert_path
from distutils.errors import DistutilsTemplateError, DistutilsInternalError
from distutils import log
class FileList:
"""A list of files built by on exploring the filesystem and filtered by
applying various patterns to what we find there.
@ -32,22 +28,19 @@ class FileList:
filtering applied)
"""
def __init__(self,
warn=None,
debug_print=None):
def __init__(self, warn=None, debug_print=None):
# ignore argument to FileList, but keep them for backwards
# compatibility
self.allfiles = None
self.files = []
def set_allfiles (self, allfiles):
def set_allfiles(self, allfiles):
self.allfiles = allfiles
def findall (self, dir=os.curdir):
def findall(self, dir=os.curdir):
self.allfiles = findall(dir)
def debug_print (self, msg):
def debug_print(self, msg):
"""Print 'msg' to stdout if the global DEBUG (taken from the
DISTUTILS_DEBUG environment variable) flag is true.
"""
@ -57,13 +50,13 @@ class FileList:
# -- List-like methods ---------------------------------------------
def append (self, item):
def append(self, item):
self.files.append(item)
def extend (self, items):
def extend(self, items):
self.files.extend(items)
def sort (self):
def sort(self):
# Not a strict lexical sort!
sortable_files = sorted(map(os.path.split, self.files))
self.files = []
@ -73,7 +66,7 @@ class FileList:
# -- Other miscellaneous utility methods ---------------------------
def remove_duplicates (self):
def remove_duplicates(self):
# Assumes list has been sorted!
for i in range(len(self.files) - 1, 0, -1):
if self.files[i] == self.files[i - 1]:
@ -82,7 +75,7 @@ class FileList:
# -- "File template" methods ---------------------------------------
def _parse_template_line (self, line):
def _parse_template_line(self, line):
words = line.split()
action = words[0]
@ -91,36 +84,26 @@ class FileList:
if action in ('include', 'exclude',
'global-include', 'global-exclude'):
if len(words) < 2:
raise DistutilsTemplateError, \
"'%s' expects <pattern1> <pattern2> ..." % action
raise DistutilsTemplateError(
"'%s' expects <pattern1> <pattern2> ..." % action)
patterns = map(convert_path, words[1:])
elif action in ('recursive-include', 'recursive-exclude'):
if len(words) < 3:
raise DistutilsTemplateError, \
"'%s' expects <dir> <pattern1> <pattern2> ..." % action
raise DistutilsTemplateError(
"'%s' expects <dir> <pattern1> <pattern2> ..." % action)
dir = convert_path(words[1])
patterns = map(convert_path, words[2:])
elif action in ('graft', 'prune'):
if len(words) != 2:
raise DistutilsTemplateError, \
"'%s' expects a single <dir_pattern>" % action
raise DistutilsTemplateError(
"'%s' expects a single <dir_pattern>" % action)
dir_pattern = convert_path(words[1])
else:
raise DistutilsTemplateError, "unknown action '%s'" % action
raise DistutilsTemplateError("unknown action '%s'" % action)
return (action, patterns, dir, dir_pattern)
# _parse_template_line ()
def process_template_line (self, line):
def process_template_line(self, line):
# Parse the line: split it up, make sure the right number of words
# is there, and return the relevant words. 'action' is always
# defined: it's the first word of the line. Which of the other
@ -149,7 +132,7 @@ class FileList:
self.debug_print("global-include " + ' '.join(patterns))
for pattern in patterns:
if not self.include_pattern(pattern, anchor=0):
log.warn(("warning: no files found matching '%s' " +
log.warn(("warning: no files found matching '%s' "
"anywhere in distribution"), pattern)
elif action == 'global-exclude':
@ -165,7 +148,7 @@ class FileList:
(dir, ' '.join(patterns)))
for pattern in patterns:
if not self.include_pattern(pattern, prefix=dir):
log.warn(("warning: no files found matching '%s' " +
log.warn(("warning: no files found matching '%s' "
"under directory '%s'"),
pattern, dir)
@ -187,19 +170,16 @@ class FileList:
elif action == 'prune':
self.debug_print("prune " + dir_pattern)
if not self.exclude_pattern(None, prefix=dir_pattern):
log.warn(("no previously-included directories found " +
log.warn(("no previously-included directories found "
"matching '%s'"), dir_pattern)
else:
raise DistutilsInternalError, \
"this cannot happen: invalid action '%s'" % action
# process_template_line ()
raise DistutilsInternalError(
"this cannot happen: invalid action '%s'" % action)
# -- Filtering/selection methods -----------------------------------
def include_pattern (self, pattern,
anchor=1, prefix=None, is_regex=0):
def include_pattern(self, pattern, anchor=1, prefix=None, is_regex=0):
"""Select strings (presumably filenames) from 'self.files' that
match 'pattern', a Unix-style wildcard (glob) pattern. Patterns
are not quite the same as implemented by the 'fnmatch' module: '*'
@ -222,9 +202,9 @@ class FileList:
Selected strings will be added to self.files.
Return 1 if files are found.
Return True if files are found, False otherwise.
"""
files_found = 0
files_found = False
pattern_re = translate_pattern(pattern, anchor, prefix, is_regex)
self.debug_print("include_pattern: applying regex r'%s'" %
pattern_re.pattern)
@ -237,12 +217,9 @@ class FileList:
if pattern_re.search(name):
self.debug_print(" adding " + name)
self.files.append(name)
files_found = 1
files_found = True
return files_found
# include_pattern ()
def exclude_pattern (self, pattern,
anchor=1, prefix=None, is_regex=0):
@ -250,9 +227,9 @@ class FileList:
'pattern'. Other parameters are the same as for
'include_pattern()', above.
The list 'self.files' is modified in place.
Return 1 if files are found.
Return True if files are found, False otherwise.
"""
files_found = 0
files_found = False
pattern_re = translate_pattern(pattern, anchor, prefix, is_regex)
self.debug_print("exclude_pattern: applying regex r'%s'" %
pattern_re.pattern)
@ -260,19 +237,14 @@ class FileList:
if pattern_re.search(self.files[i]):
self.debug_print(" removing " + self.files[i])
del self.files[i]
files_found = 1
files_found = True
return files_found
# exclude_pattern ()
# class FileList
# ----------------------------------------------------------------------
# Utility functions
def findall (dir = os.curdir):
def findall(dir=os.curdir):
"""Find all files under 'dir' and return the list of full filenames
(relative to 'dir').
"""
@ -300,11 +272,10 @@ def findall (dir = os.curdir):
list.append(fullname)
elif S_ISDIR(mode) and not S_ISLNK(mode):
push(fullname)
return list
def glob_to_re (pattern):
def 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
@ -322,10 +293,8 @@ def glob_to_re (pattern):
pattern_re = re.sub(r'(^|[^\\])\.', r'\1[^/]', pattern_re)
return pattern_re
# glob_to_re ()
def translate_pattern (pattern, anchor=1, prefix=None, is_regex=0):
def 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)
@ -350,5 +319,3 @@ def translate_pattern (pattern, anchor=1, prefix=None, is_regex=0):
pattern_re = "^" + pattern_re
return re.compile(pattern_re)
# translate_pattern ()