mirror of
https://github.com/python/cpython.git
synced 2024-12-01 05:45:40 +08:00
49fd7fa443
number of tests, all because of the codecs/_multibytecodecs issue described here (it's not a Py3K issue, just something Py3K discovers): http://mail.python.org/pipermail/python-dev/2006-April/064051.html Hye-Shik Chang promised to look for a fix, so no need to fix it here. The tests that are expected to break are: test_codecencodings_cn test_codecencodings_hk test_codecencodings_jp test_codecencodings_kr test_codecencodings_tw test_codecs test_multibytecodec This merge fixes an actual test failure (test_weakref) in this branch, though, so I believe merging is the right thing to do anyway.
75 lines
2.0 KiB
Python
75 lines
2.0 KiB
Python
"""Filename globbing utility."""
|
|
|
|
import os
|
|
import fnmatch
|
|
import re
|
|
|
|
__all__ = ["glob", "iglob"]
|
|
|
|
def glob(pathname):
|
|
"""Return a list of paths matching a pathname pattern.
|
|
|
|
The pattern may contain simple shell-style wildcards a la fnmatch.
|
|
|
|
"""
|
|
return list(iglob(pathname))
|
|
|
|
def iglob(pathname):
|
|
"""Return a list of paths matching a pathname pattern.
|
|
|
|
The pattern may contain simple shell-style wildcards a la fnmatch.
|
|
|
|
"""
|
|
if not has_magic(pathname):
|
|
if os.path.lexists(pathname):
|
|
yield pathname
|
|
return
|
|
dirname, basename = os.path.split(pathname)
|
|
if not dirname:
|
|
for name in glob1(os.curdir, basename):
|
|
yield name
|
|
return
|
|
if has_magic(dirname):
|
|
dirs = iglob(dirname)
|
|
else:
|
|
dirs = [dirname]
|
|
if has_magic(basename):
|
|
glob_in_dir = glob1
|
|
else:
|
|
glob_in_dir = glob0
|
|
for dirname in dirs:
|
|
for name in glob_in_dir(dirname, basename):
|
|
yield os.path.join(dirname, name)
|
|
|
|
# These 2 helper functions non-recursively glob inside a literal directory.
|
|
# They return a list of basenames. `glob1` accepts a pattern while `glob0`
|
|
# takes a literal basename (so it only has to check for its existence).
|
|
|
|
def glob1(dirname, pattern):
|
|
if not dirname:
|
|
dirname = os.curdir
|
|
try:
|
|
names = os.listdir(dirname)
|
|
except os.error:
|
|
return []
|
|
if pattern[0]!='.':
|
|
names=filter(lambda x: x[0]!='.',names)
|
|
return fnmatch.filter(names,pattern)
|
|
|
|
def glob0(dirname, basename):
|
|
if basename == '':
|
|
# `os.path.split()` returns an empty basename for paths ending with a
|
|
# directory separator. 'q*x/' should match only directories.
|
|
if os.path.isdir(dirname):
|
|
return [basename]
|
|
else:
|
|
if os.path.lexists(os.path.join(dirname, basename)):
|
|
return [basename]
|
|
return []
|
|
|
|
|
|
magic_check = re.compile('[*?[]')
|
|
|
|
def has_magic(s):
|
|
return magic_check.search(s) is not None
|