blob: b3d9ec1b1f8a36162d69c908f6a6948de94def7d [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
Martin v. Löwised11a5d2012-05-20 10:42:17 +02008try:
9 _unicode = unicode
10except NameError:
11 # If Python is built without Unicode support, the unicode type
12 # will not exist. Fake one.
13 class _unicode(object):
14 pass
15
Johannes Gijsbers836f5432005-01-08 13:13:19 +000016__all__ = ["glob", "iglob"]
Guido van Rossumbba77af1992-01-12 23:26:24 +000017
Guido van Rossum65a96201991-01-01 18:17:49 +000018def glob(pathname):
Tim Peters07e99cb2001-01-14 23:47:14 +000019 """Return a list of paths matching a pathname pattern.
Guido van Rossumab096c91997-04-02 05:47:11 +000020
Petri Lehtinen23427842013-02-23 19:53:03 +010021 The pattern may contain simple shell-style wildcards a la
22 fnmatch. However, unlike fnmatch, filenames starting with a
23 dot are special cases that are not matched by '*' and '?'
24 patterns.
Guido van Rossumab096c91997-04-02 05:47:11 +000025
Tim Peters07e99cb2001-01-14 23:47:14 +000026 """
Johannes Gijsbers836f5432005-01-08 13:13:19 +000027 return list(iglob(pathname))
28
29def iglob(pathname):
Georg Brandl3ade7612009-04-01 17:46:01 +000030 """Return an iterator which yields the paths matching a pathname pattern.
Johannes Gijsbers836f5432005-01-08 13:13:19 +000031
Petri Lehtinen23427842013-02-23 19:53:03 +010032 The pattern may contain simple shell-style wildcards a la
33 fnmatch. However, unlike fnmatch, filenames starting with a
34 dot are special cases that are not matched by '*' and '?'
35 patterns.
Johannes Gijsbers836f5432005-01-08 13:13:19 +000036
37 """
Tim Peters07e99cb2001-01-14 23:47:14 +000038 dirname, basename = os.path.split(pathname)
Serhiy Storchaka3fdffc92014-08-12 12:54:55 +030039 if not has_magic(pathname):
40 if basename:
41 if os.path.lexists(pathname):
42 yield pathname
43 else:
44 # Patterns ending with a slash should match only directories
45 if os.path.isdir(dirname):
46 yield pathname
47 return
Martin v. Löwisb5d4d2a2001-06-06 06:24:38 +000048 if not dirname:
Johannes Gijsbers836f5432005-01-08 13:13:19 +000049 for name in glob1(os.curdir, basename):
50 yield name
51 return
Antoine Pitrou124ee8b2012-12-16 13:55:47 +010052 # `os.path.split()` returns the argument itself as a dirname if it is a
53 # drive or UNC path. Prevent an infinite recursion if a drive or UNC path
54 # contains magic characters (i.e. r'\\?\C:').
55 if dirname != pathname and has_magic(dirname):
Johannes Gijsbers836f5432005-01-08 13:13:19 +000056 dirs = iglob(dirname)
Tim Peters07e99cb2001-01-14 23:47:14 +000057 else:
Johannes Gijsbers836f5432005-01-08 13:13:19 +000058 dirs = [dirname]
59 if has_magic(basename):
60 glob_in_dir = glob1
Tim Peters07e99cb2001-01-14 23:47:14 +000061 else:
Johannes Gijsbers836f5432005-01-08 13:13:19 +000062 glob_in_dir = glob0
63 for dirname in dirs:
64 for name in glob_in_dir(dirname, basename):
65 yield os.path.join(dirname, name)
66
67# These 2 helper functions non-recursively glob inside a literal directory.
68# They return a list of basenames. `glob1` accepts a pattern while `glob0`
69# takes a literal basename (so it only has to check for its existence).
Guido van Rossum65a96201991-01-01 18:17:49 +000070
71def glob1(dirname, pattern):
Johannes Gijsbers836f5432005-01-08 13:13:19 +000072 if not dirname:
73 dirname = os.curdir
Martin v. Löwised11a5d2012-05-20 10:42:17 +020074 if isinstance(pattern, _unicode) and not isinstance(dirname, unicode):
Georg Brandlb54a8092007-03-20 23:05:14 +000075 dirname = unicode(dirname, sys.getfilesystemencoding() or
76 sys.getdefaultencoding())
Tim Peters07e99cb2001-01-14 23:47:14 +000077 try:
78 names = os.listdir(dirname)
79 except os.error:
80 return []
Georg Brandl71ff6462007-03-07 08:31:51 +000081 if pattern[0] != '.':
82 names = filter(lambda x: x[0] != '.', names)
83 return fnmatch.filter(names, pattern)
Guido van Rossum65a96201991-01-01 18:17:49 +000084
Johannes Gijsbers836f5432005-01-08 13:13:19 +000085def glob0(dirname, basename):
86 if basename == '':
87 # `os.path.split()` returns an empty basename for paths ending with a
88 # directory separator. 'q*x/' should match only directories.
Neal Norwitza31bf182006-04-09 03:35:43 +000089 if os.path.isdir(dirname):
Johannes Gijsbers836f5432005-01-08 13:13:19 +000090 return [basename]
91 else:
92 if os.path.lexists(os.path.join(dirname, basename)):
93 return [basename]
94 return []
95
Guido van Rossumc2ef5c21992-01-12 23:32:11 +000096
Guido van Rossum9694fca1997-10-22 21:00:49 +000097magic_check = re.compile('[*?[]')
Guido van Rossumc2ef5c21992-01-12 23:32:11 +000098
Guido van Rossum65a96201991-01-01 18:17:49 +000099def has_magic(s):
Tim Peters07e99cb2001-01-14 23:47:14 +0000100 return magic_check.search(s) is not None