blob: 04364be7dfb7898def3c7464bef57e59d2d023dd [file] [log] [blame]
Guido van Rossumab096c91997-04-02 05:47:11 +00001"""Filename globbing utility."""
Guido van Rossum65a96201991-01-01 18:17:49 +00002
Georg Brandl71ff6462007-03-07 08:31:51 +00003import sys
Guido van Rossumbba77af1992-01-12 23:26:24 +00004import os
Guido van Rossum9694fca1997-10-22 21:00:49 +00005import re
Georg Brandl71ff6462007-03-07 08:31:51 +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):
Georg Brandl3ade7612009-04-01 17:46:01 +000019 """Return an iterator which yields the paths matching a pathname pattern.
Johannes Gijsbers836f5432005-01-08 13:13:19 +000020
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
Georg Brandl71ff6462007-03-07 08:31:51 +000052 if isinstance(pattern, unicode) and not isinstance(dirname, unicode):
Georg Brandlb54a8092007-03-20 23:05:14 +000053 dirname = unicode(dirname, sys.getfilesystemencoding() or
54 sys.getdefaultencoding())
Tim Peters07e99cb2001-01-14 23:47:14 +000055 try:
56 names = os.listdir(dirname)
57 except os.error:
58 return []
Georg Brandl71ff6462007-03-07 08:31:51 +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.
Neal Norwitza31bf182006-04-09 03:35:43 +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