diff --git a/Lib/find.py b/Lib/find.py new file mode 100644 index 00000000000..ccd9fdb9651 --- /dev/null +++ b/Lib/find.py @@ -0,0 +1,26 @@ +import fnmatch +import os + +_debug = 0 + +_prune = ['(*)'] + +def find(pattern, dir = os.curdir): + list = [] + names = os.listdir(dir) + names.sort() + for name in names: + if name in (os.curdir, os.pardir): + continue + fullname = os.path.join(dir, name) + if fnmatch.fnmatch(name, pattern): + list.append(fullname) + if os.path.isdir(fullname) and not os.path.islink(fullname): + for p in _prune: + if fnmatch.fnmatch(name, p): + if _debug: print "skip", `fullname` + break + else: + if _debug: print "descend into", `fullname` + list = list + find(pattern, fullname) + return list diff --git a/Lib/fnmatch.py b/Lib/fnmatch.py index 5c1bc711c11..9b31856546f 100644 --- a/Lib/fnmatch.py +++ b/Lib/fnmatch.py @@ -1,23 +1,59 @@ -# module 'fnmatch' -- filename matching with shell patterns -# This version translates the pattern to a regular expression -# and moreover caches the expressions. +"""Filename matching with shell patterns. -import os -import regex +fnmatch(FILENAME, PATTERN) matches according to the local convention. +fnmatchcase(FILENAME, PATTERN) always takes case in account. -cache = {} +The functions operate by translating the pattern into a regular +expression. They cache the compiled regular expressions for speed. + +The function translate(PATTERN) returns a regular expression +corresponding to PATTERN. (It does not compile it.) +""" + +_cache = {} def fnmatch(name, pat): + """Test whether FILENAME matches PATTERN. + + Patterns are Unix shell style: + + * matches everything + ? matches any single character + [seq] matches any character in seq + [!seq] matches any char not in seq + + An initial period in FILENAME is not special. + Both FILENAME and PATTERN are first case-normalized + if the operating system requires it. + If you don't want this, use fnmatchcase(FILENAME, PATTERN). + """ + + import os name = os.path.normcase(name) pat = os.path.normcase(pat) - if not cache.has_key(pat): + return fnmatchcase(name, pat) + +def fnmatchcase(name, pat): + """Test wheter FILENAME matches PATTERN, including case. + + This is a version of fnmatch() which doesn't case-normalize + its arguments. + """ + + if not _cache.has_key(pat): res = translate(pat) + import regex save_syntax = regex.set_syntax(0) - cache[pat] = regex.compile(res) + _cache[pat] = regex.compile(res) save_syntax = regex.set_syntax(save_syntax) - return cache[pat].match(name) == len(name) + return _cache[pat].match(name) == len(name) def translate(pat): + """Translate a shell PATTERN to a regular expression. + + There is no way to quote meta-characters. + """ + i, n = 0, len(pat) res = '' while i < n: diff --git a/Lib/lib-old/find.py b/Lib/lib-old/find.py new file mode 100644 index 00000000000..ccd9fdb9651 --- /dev/null +++ b/Lib/lib-old/find.py @@ -0,0 +1,26 @@ +import fnmatch +import os + +_debug = 0 + +_prune = ['(*)'] + +def find(pattern, dir = os.curdir): + list = [] + names = os.listdir(dir) + names.sort() + for name in names: + if name in (os.curdir, os.pardir): + continue + fullname = os.path.join(dir, name) + if fnmatch.fnmatch(name, pattern): + list.append(fullname) + if os.path.isdir(fullname) and not os.path.islink(fullname): + for p in _prune: + if fnmatch.fnmatch(name, p): + if _debug: print "skip", `fullname` + break + else: + if _debug: print "descend into", `fullname` + list = list + find(pattern, fullname) + return list diff --git a/Lib/macpath.py b/Lib/macpath.py index 0e32ab2168d..2eddf5aeeec 100644 --- a/Lib/macpath.py +++ b/Lib/macpath.py @@ -100,6 +100,13 @@ def isdir(s): return S_ISDIR(st[ST_MODE]) +# Return true if the pathname refers to a symbolic link. +# (Always false on the Mac, until we understand Aliases.) + +def islink(s): + return 0 + + # Return true if the pathname refers to an existing regular file. def isfile(s): diff --git a/Lib/py_compile.py b/Lib/py_compile.py index ed54f47a705..98c5db082ee 100644 --- a/Lib/py_compile.py +++ b/Lib/py_compile.py @@ -14,12 +14,17 @@ def compile(file, cfile = None): import os, marshal, __builtin__ f = open(file) codestring = f.read() - timestamp = os.fstat(f.fileno())[8] f.close() + timestamp = os.stat(file)[8] codeobject = __builtin__.compile(codestring, file, 'exec') if not cfile: cfile = file + 'c' - fc = open(cfile, 'w') + fc = open(cfile, 'wb') wr_long(fc, MAGIC) wr_long(fc, timestamp) marshal.dump(codeobject, fc) + fc.close() + if os.name == 'mac': + import MacOS + MacOS.SetFileType(cfile, 'PYC ', 'PYTH') + MacOS.SetFileType(file, 'TEXT', 'PYTH')