blob: 0e2bc1e7342ad2920d4decec5a878a8b91a80b87 [file] [log] [blame]
Guido van Rossumab096c91997-04-02 05:47:11 +00001"""Filename globbing utility."""
Guido van Rossum65a96201991-01-01 18:17:49 +00002
Guido van Rossumd8faa362007-04-27 19:54:29 +00003import sys
Guido van Rossumbba77af1992-01-12 23:26:24 +00004import os
Guido van Rossum9694fca1997-10-22 21:00:49 +00005import re
Guido van Rossumd8faa362007-04-27 19:54:29 +00006import fnmatch
Guido van Rossum65a96201991-01-01 18:17:49 +00007
Johannes Gijsbers836f5432005-01-08 13:13:19 +00008__all__ = ["glob", "iglob"]
Guido van Rossumbba77af1992-01-12 23:26:24 +00009
Guido van Rossum65a96201991-01-01 18:17:49 +000010def glob(pathname):
Tim Peters07e99cb2001-01-14 23:47:14 +000011 """Return a list of paths matching a pathname pattern.
Guido van Rossumab096c91997-04-02 05:47:11 +000012
Tim Peters07e99cb2001-01-14 23:47:14 +000013 The pattern may contain simple shell-style wildcards a la fnmatch.
Guido van Rossumab096c91997-04-02 05:47:11 +000014
Tim Peters07e99cb2001-01-14 23:47:14 +000015 """
Johannes Gijsbers836f5432005-01-08 13:13:19 +000016 return list(iglob(pathname))
17
18def iglob(pathname):
19 """Return a list of paths matching a pathname pattern.
20
21 The pattern may contain simple shell-style wildcards a la fnmatch.
22
23 """
Tim Peters07e99cb2001-01-14 23:47:14 +000024 if not has_magic(pathname):
Johannes Gijsbersae882f72004-08-30 10:19:56 +000025 if os.path.lexists(pathname):
Johannes Gijsbers836f5432005-01-08 13:13:19 +000026 yield pathname
27 return
Tim Peters07e99cb2001-01-14 23:47:14 +000028 dirname, basename = os.path.split(pathname)
Martin v. Löwisb5d4d2a2001-06-06 06:24:38 +000029 if not dirname:
Johannes Gijsbers836f5432005-01-08 13:13:19 +000030 for name in glob1(os.curdir, basename):
31 yield name
32 return
33 if has_magic(dirname):
34 dirs = iglob(dirname)
Tim Peters07e99cb2001-01-14 23:47:14 +000035 else:
Johannes Gijsbers836f5432005-01-08 13:13:19 +000036 dirs = [dirname]
37 if has_magic(basename):
38 glob_in_dir = glob1
Tim Peters07e99cb2001-01-14 23:47:14 +000039 else:
Johannes Gijsbers836f5432005-01-08 13:13:19 +000040 glob_in_dir = glob0
41 for dirname in dirs:
42 for name in glob_in_dir(dirname, basename):
43 yield os.path.join(dirname, name)
44
45# These 2 helper functions non-recursively glob inside a literal directory.
46# They return a list of basenames. `glob1` accepts a pattern while `glob0`
47# takes a literal basename (so it only has to check for its existence).
Guido van Rossum65a96201991-01-01 18:17:49 +000048
49def glob1(dirname, pattern):
Johannes Gijsbers836f5432005-01-08 13:13:19 +000050 if not dirname:
51 dirname = os.curdir
Guido van Rossumef87d6e2007-05-02 19:09:54 +000052 if isinstance(pattern, str) and not isinstance(dirname, str):
53 dirname = str(dirname, sys.getfilesystemencoding() or
Guido van Rossumd8faa362007-04-27 19:54:29 +000054 sys.getdefaultencoding())
Tim Peters07e99cb2001-01-14 23:47:14 +000055 try:
56 names = os.listdir(dirname)
57 except os.error:
58 return []
Guido van Rossumd8faa362007-04-27 19:54:29 +000059 if pattern[0] != '.':
60 names = filter(lambda x: x[0] != '.', names)
61 return fnmatch.filter(names, pattern)
Guido van Rossum65a96201991-01-01 18:17:49 +000062
Johannes Gijsbers836f5432005-01-08 13:13:19 +000063def glob0(dirname, basename):
64 if basename == '':
65 # `os.path.split()` returns an empty basename for paths ending with a
66 # directory separator. 'q*x/' should match only directories.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000067 if os.path.isdir(dirname):
Johannes Gijsbers836f5432005-01-08 13:13:19 +000068 return [basename]
69 else:
70 if os.path.lexists(os.path.join(dirname, basename)):
71 return [basename]
72 return []
73
Guido van Rossumc2ef5c21992-01-12 23:32:11 +000074
Guido van Rossum9694fca1997-10-22 21:00:49 +000075magic_check = re.compile('[*?[]')
Guido van Rossumc2ef5c21992-01-12 23:32:11 +000076
Guido van Rossum65a96201991-01-01 18:17:49 +000077def has_magic(s):
Tim Peters07e99cb2001-01-14 23:47:14 +000078 return magic_check.search(s) is not None