blob: c9f811720a4b55a3f8aea0c0716716e4e1e77010 [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
Tim Peters07e99cb2001-01-14 23:47:14 +000012 The pattern may contain simple shell-style wildcards a la fnmatch.
Guido van Rossumab096c91997-04-02 05:47:11 +000013
Tim Peters07e99cb2001-01-14 23:47:14 +000014 """
Johannes Gijsbers836f5432005-01-08 13:13:19 +000015 return list(iglob(pathname))
16
17def iglob(pathname):
Benjamin Petersond23f8222009-04-05 19:13:16 +000018 """Return an iterator which yields the paths matching a pathname pattern.
Johannes Gijsbers836f5432005-01-08 13:13:19 +000019
20 The pattern may contain simple shell-style wildcards a la fnmatch.
21
22 """
Tim Peters07e99cb2001-01-14 23:47:14 +000023 if not has_magic(pathname):
Johannes Gijsbersae882f72004-08-30 10:19:56 +000024 if os.path.lexists(pathname):
Johannes Gijsbers836f5432005-01-08 13:13:19 +000025 yield pathname
26 return
Tim Golden9b3fb0c2012-11-06 15:33:30 +000027 dirname, basename = os.path.split(pathname)
28 if not dirname:
29 yield from glob1(None, basename)
30 return
Antoine Pitrou3d068b22012-12-16 13:49:37 +010031 # `os.path.split()` returns the argument itself as a dirname if it is a
32 # drive or UNC path. Prevent an infinite recursion if a drive or UNC path
33 # contains magic characters (i.e. r'\\?\C:').
34 if dirname != pathname and has_magic(dirname):
Tim Golden9b3fb0c2012-11-06 15:33:30 +000035 dirs = iglob(dirname)
36 else:
37 dirs = [dirname]
38 if has_magic(basename):
39 glob_in_dir = glob1
40 else:
41 glob_in_dir = glob0
42 for dirname in dirs:
43 for name in glob_in_dir(dirname, basename):
44 yield os.path.join(dirname, name)
Johannes Gijsbers836f5432005-01-08 13:13:19 +000045
46# These 2 helper functions non-recursively glob inside a literal directory.
47# They return a list of basenames. `glob1` accepts a pattern while `glob0`
48# takes a literal basename (so it only has to check for its existence).
Guido van Rossum65a96201991-01-01 18:17:49 +000049
50def glob1(dirname, pattern):
Johannes Gijsbers836f5432005-01-08 13:13:19 +000051 if not dirname:
Guido van Rossumf0af3e32008-10-02 18:55:37 +000052 if isinstance(pattern, bytes):
53 dirname = bytes(os.curdir, 'ASCII')
54 else:
55 dirname = os.curdir
Tim Peters07e99cb2001-01-14 23:47:14 +000056 try:
57 names = os.listdir(dirname)
Andrew Svetlovad28c7f2012-12-18 22:02:39 +020058 except OSError:
Tim Peters07e99cb2001-01-14 23:47:14 +000059 return []
Hynek Schlawacke26568f2012-12-27 10:10:11 +010060 if not _ishidden(pattern):
61 names = [x for x in names if not _ishidden(x)]
Guido van Rossumd8faa362007-04-27 19:54:29 +000062 return fnmatch.filter(names, pattern)
Guido van Rossum65a96201991-01-01 18:17:49 +000063
Johannes Gijsbers836f5432005-01-08 13:13:19 +000064def glob0(dirname, basename):
Antoine Pitrou54615582012-12-16 16:03:01 +010065 if not basename:
Johannes Gijsbers836f5432005-01-08 13:13:19 +000066 # `os.path.split()` returns an empty basename for paths ending with a
67 # directory separator. 'q*x/' should match only directories.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000068 if os.path.isdir(dirname):
Johannes Gijsbers836f5432005-01-08 13:13:19 +000069 return [basename]
70 else:
71 if os.path.lexists(os.path.join(dirname, basename)):
72 return [basename]
73 return []
74
Guido van Rossumc2ef5c21992-01-12 23:32:11 +000075
Tim Golden9b3fb0c2012-11-06 15:33:30 +000076magic_check = re.compile('[*?[]')
77magic_check_bytes = re.compile(b'[*?[]')
78
Guido van Rossum65a96201991-01-01 18:17:49 +000079def has_magic(s):
Guido van Rossumf0af3e32008-10-02 18:55:37 +000080 if isinstance(s, bytes):
81 match = magic_check_bytes.search(s)
82 else:
83 match = magic_check.search(s)
84 return match is not None
Hynek Schlawacke26568f2012-12-27 10:10:11 +010085
86def _ishidden(path):
87 return path[0] in ('.', b'.'[0])