blob: f34534b53c44825f010fd757aaf68cd93132cd5a [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 if not has_magic(pathname):
Johannes Gijsbersae882f72004-08-30 10:19:56 +000039 if os.path.lexists(pathname):
Johannes Gijsbers836f5432005-01-08 13:13:19 +000040 yield pathname
41 return
Tim Peters07e99cb2001-01-14 23:47:14 +000042 dirname, basename = os.path.split(pathname)
Martin v. Löwisb5d4d2a2001-06-06 06:24:38 +000043 if not dirname:
Johannes Gijsbers836f5432005-01-08 13:13:19 +000044 for name in glob1(os.curdir, basename):
45 yield name
46 return
Antoine Pitrou124ee8b2012-12-16 13:55:47 +010047 # `os.path.split()` returns the argument itself as a dirname if it is a
48 # drive or UNC path. Prevent an infinite recursion if a drive or UNC path
49 # contains magic characters (i.e. r'\\?\C:').
50 if dirname != pathname and has_magic(dirname):
Johannes Gijsbers836f5432005-01-08 13:13:19 +000051 dirs = iglob(dirname)
Tim Peters07e99cb2001-01-14 23:47:14 +000052 else:
Johannes Gijsbers836f5432005-01-08 13:13:19 +000053 dirs = [dirname]
54 if has_magic(basename):
55 glob_in_dir = glob1
Tim Peters07e99cb2001-01-14 23:47:14 +000056 else:
Johannes Gijsbers836f5432005-01-08 13:13:19 +000057 glob_in_dir = glob0
58 for dirname in dirs:
59 for name in glob_in_dir(dirname, basename):
60 yield os.path.join(dirname, name)
61
62# These 2 helper functions non-recursively glob inside a literal directory.
63# They return a list of basenames. `glob1` accepts a pattern while `glob0`
64# takes a literal basename (so it only has to check for its existence).
Guido van Rossum65a96201991-01-01 18:17:49 +000065
66def glob1(dirname, pattern):
Johannes Gijsbers836f5432005-01-08 13:13:19 +000067 if not dirname:
68 dirname = os.curdir
Martin v. Löwised11a5d2012-05-20 10:42:17 +020069 if isinstance(pattern, _unicode) and not isinstance(dirname, unicode):
Georg Brandlb54a8092007-03-20 23:05:14 +000070 dirname = unicode(dirname, sys.getfilesystemencoding() or
71 sys.getdefaultencoding())
Tim Peters07e99cb2001-01-14 23:47:14 +000072 try:
73 names = os.listdir(dirname)
74 except os.error:
75 return []
Georg Brandl71ff6462007-03-07 08:31:51 +000076 if pattern[0] != '.':
77 names = filter(lambda x: x[0] != '.', names)
78 return fnmatch.filter(names, pattern)
Guido van Rossum65a96201991-01-01 18:17:49 +000079
Johannes Gijsbers836f5432005-01-08 13:13:19 +000080def glob0(dirname, basename):
81 if basename == '':
82 # `os.path.split()` returns an empty basename for paths ending with a
83 # directory separator. 'q*x/' should match only directories.
Neal Norwitza31bf182006-04-09 03:35:43 +000084 if os.path.isdir(dirname):
Johannes Gijsbers836f5432005-01-08 13:13:19 +000085 return [basename]
86 else:
87 if os.path.lexists(os.path.join(dirname, basename)):
88 return [basename]
89 return []
90
Guido van Rossumc2ef5c21992-01-12 23:32:11 +000091
Guido van Rossum9694fca1997-10-22 21:00:49 +000092magic_check = re.compile('[*?[]')
Guido van Rossumc2ef5c21992-01-12 23:32:11 +000093
Guido van Rossum65a96201991-01-01 18:17:49 +000094def has_magic(s):
Tim Peters07e99cb2001-01-14 23:47:14 +000095 return magic_check.search(s) is not None