]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
changes for the Mac
authorGuido van Rossum <guido@python.org>
Fri, 27 Jan 1995 02:41:45 +0000 (02:41 +0000)
committerGuido van Rossum <guido@python.org>
Fri, 27 Jan 1995 02:41:45 +0000 (02:41 +0000)
Lib/find.py [new file with mode: 0644]
Lib/fnmatch.py
Lib/lib-old/find.py [new file with mode: 0644]
Lib/macpath.py
Lib/py_compile.py

diff --git a/Lib/find.py b/Lib/find.py
new file mode 100644 (file)
index 0000000..ccd9fdb
--- /dev/null
@@ -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
index 5c1bc711c11500cf280f70929d1c1cf10d334df9..9b31856546f5d763f91ed9434648ffb40a97cfef 100644 (file)
@@ -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 (file)
index 0000000..ccd9fdb
--- /dev/null
@@ -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
index 0e32ab2168d58e79c06777ac3c73f31da2473949..2eddf5aeeec65fd6c1f092a1b00eeaeba424a3e4 100644 (file)
@@ -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):
index ed54f47a7051ca3488f6267f07aebb5b65ade6e2..98c5db082ee807bc5c151f667cc823483f981c95 100644 (file)
@@ -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')