blob: e388b5f9c0dd0e6bca9839cfb912e3aa4023b1af [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 Rossumbba77af1992-01-12 23:26:24 +00003import os
Guido van Rossum9694fca1997-10-22 21:00:49 +00004import re
Guido van Rossumd8faa362007-04-27 19:54:29 +00005import fnmatch
Guido van Rossum65a96201991-01-01 18:17:49 +00006
Johannes Gijsbers836f5432005-01-08 13:13:19 +00007__all__ = ["glob", "iglob"]
Guido van Rossumbba77af1992-01-12 23:26:24 +00008
Guido van Rossum65a96201991-01-01 18:17:49 +00009def glob(pathname):
Tim Peters07e99cb2001-01-14 23:47:14 +000010 """Return a list of paths matching a pathname pattern.
Guido van Rossumab096c91997-04-02 05:47:11 +000011
Petri Lehtinenee4a20b2013-02-23 19:53:03 +010012 The pattern may contain simple shell-style wildcards a la
13 fnmatch. However, unlike fnmatch, filenames starting with a
14 dot are special cases that are not matched by '*' and '?'
15 patterns.
Guido van Rossumab096c91997-04-02 05:47:11 +000016
Tim Peters07e99cb2001-01-14 23:47:14 +000017 """
Johannes Gijsbers836f5432005-01-08 13:13:19 +000018 return list(iglob(pathname))
19
20def iglob(pathname):
Benjamin Petersond23f8222009-04-05 19:13:16 +000021 """Return an iterator which yields the paths matching a pathname pattern.
Johannes Gijsbers836f5432005-01-08 13:13:19 +000022
Petri Lehtinenee4a20b2013-02-23 19:53:03 +010023 The pattern may contain simple shell-style wildcards a la
24 fnmatch. However, unlike fnmatch, filenames starting with a
25 dot are special cases that are not matched by '*' and '?'
26 patterns.
Johannes Gijsbers836f5432005-01-08 13:13:19 +000027
28 """
Tim Peters07e99cb2001-01-14 23:47:14 +000029 if not has_magic(pathname):
Johannes Gijsbersae882f72004-08-30 10:19:56 +000030 if os.path.lexists(pathname):
Johannes Gijsbers836f5432005-01-08 13:13:19 +000031 yield pathname
32 return
Tim Golden9b3fb0c2012-11-06 15:33:30 +000033 dirname, basename = os.path.split(pathname)
34 if not dirname:
35 yield from glob1(None, basename)
36 return
Antoine Pitrou3d068b22012-12-16 13:49:37 +010037 # `os.path.split()` returns the argument itself as a dirname if it is a
38 # drive or UNC path. Prevent an infinite recursion if a drive or UNC path
39 # contains magic characters (i.e. r'\\?\C:').
40 if dirname != pathname and has_magic(dirname):
Tim Golden9b3fb0c2012-11-06 15:33:30 +000041 dirs = iglob(dirname)
42 else:
43 dirs = [dirname]
44 if has_magic(basename):
45 glob_in_dir = glob1
46 else:
47 glob_in_dir = glob0
48 for dirname in dirs:
49 for name in glob_in_dir(dirname, basename):
50 yield os.path.join(dirname, name)
Johannes Gijsbers836f5432005-01-08 13:13:19 +000051
52# These 2 helper functions non-recursively glob inside a literal directory.
53# They return a list of basenames. `glob1` accepts a pattern while `glob0`
54# takes a literal basename (so it only has to check for its existence).
Guido van Rossum65a96201991-01-01 18:17:49 +000055
56def glob1(dirname, pattern):
Johannes Gijsbers836f5432005-01-08 13:13:19 +000057 if not dirname:
Guido van Rossumf0af3e32008-10-02 18:55:37 +000058 if isinstance(pattern, bytes):
59 dirname = bytes(os.curdir, 'ASCII')
60 else:
61 dirname = os.curdir
Tim Peters07e99cb2001-01-14 23:47:14 +000062 try:
63 names = os.listdir(dirname)
Andrew Svetlovad28c7f2012-12-18 22:02:39 +020064 except OSError:
Tim Peters07e99cb2001-01-14 23:47:14 +000065 return []
Hynek Schlawacke26568f2012-12-27 10:10:11 +010066 if not _ishidden(pattern):
67 names = [x for x in names if not _ishidden(x)]
Guido van Rossumd8faa362007-04-27 19:54:29 +000068 return fnmatch.filter(names, pattern)
Guido van Rossum65a96201991-01-01 18:17:49 +000069
Johannes Gijsbers836f5432005-01-08 13:13:19 +000070def glob0(dirname, basename):
Antoine Pitrou54615582012-12-16 16:03:01 +010071 if not basename:
Johannes Gijsbers836f5432005-01-08 13:13:19 +000072 # `os.path.split()` returns an empty basename for paths ending with a
73 # directory separator. 'q*x/' should match only directories.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000074 if os.path.isdir(dirname):
Johannes Gijsbers836f5432005-01-08 13:13:19 +000075 return [basename]
76 else:
77 if os.path.lexists(os.path.join(dirname, basename)):
78 return [basename]
79 return []
80
Guido van Rossumc2ef5c21992-01-12 23:32:11 +000081
Serhiy Storchakafd32fff2013-11-18 13:06:43 +020082magic_check = re.compile('([*?[])')
83magic_check_bytes = re.compile(b'([*?[])')
Tim Golden9b3fb0c2012-11-06 15:33:30 +000084
Guido van Rossum65a96201991-01-01 18:17:49 +000085def has_magic(s):
Guido van Rossumf0af3e32008-10-02 18:55:37 +000086 if isinstance(s, bytes):
87 match = magic_check_bytes.search(s)
88 else:
89 match = magic_check.search(s)
90 return match is not None
Hynek Schlawacke26568f2012-12-27 10:10:11 +010091
92def _ishidden(path):
93 return path[0] in ('.', b'.'[0])
Serhiy Storchakafd32fff2013-11-18 13:06:43 +020094
95def escape(pathname):
96 """Escape all special characters.
97 """
98 # Escaping is done by wrapping any of "*?[" between square brackets.
99 # Metacharacters do not work in the drive part and shouldn't be escaped.
100 drive, pathname = os.path.splitdrive(pathname)
101 if isinstance(pathname, bytes):
102 pathname = magic_check_bytes.sub(br'[\1]', pathname)
103 else:
104 pathname = magic_check.sub(r'[\1]', pathname)
105 return drive + pathname