Guido van Rossum | ab096c9 | 1997-04-02 05:47:11 +0000 | [diff] [blame] | 1 | """Filename globbing utility.""" |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | bba77af | 1992-01-12 23:26:24 +0000 | [diff] [blame] | 3 | import os |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 4 | import re |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5 | import fnmatch |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 6 | |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 7 | __all__ = ["glob", "iglob"] |
Guido van Rossum | bba77af | 1992-01-12 23:26:24 +0000 | [diff] [blame] | 8 | |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 9 | def glob(pathname, *, recursive=False): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 10 | """Return a list of paths matching a pathname pattern. |
Guido van Rossum | ab096c9 | 1997-04-02 05:47:11 +0000 | [diff] [blame] | 11 | |
Petri Lehtinen | ee4a20b | 2013-02-23 19:53:03 +0100 | [diff] [blame] | 12 | 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 Rossum | ab096c9 | 1997-04-02 05:47:11 +0000 | [diff] [blame] | 16 | |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 17 | If recursive is true, the pattern '**' will match any files and |
| 18 | zero or more directories and subdirectories. |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 19 | """ |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 20 | return list(iglob(pathname, recursive=recursive)) |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 21 | |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 22 | def iglob(pathname, *, recursive=False): |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 23 | """Return an iterator which yields the paths matching a pathname pattern. |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 24 | |
Petri Lehtinen | ee4a20b | 2013-02-23 19:53:03 +0100 | [diff] [blame] | 25 | The pattern may contain simple shell-style wildcards a la |
| 26 | fnmatch. However, unlike fnmatch, filenames starting with a |
| 27 | dot are special cases that are not matched by '*' and '?' |
| 28 | patterns. |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 29 | |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 30 | If recursive is true, the pattern '**' will match any files and |
| 31 | zero or more directories and subdirectories. |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 32 | """ |
Tim Golden | 9b3fb0c | 2012-11-06 15:33:30 +0000 | [diff] [blame] | 33 | dirname, basename = os.path.split(pathname) |
Serhiy Storchaka | 6f20170 | 2014-08-12 12:55:12 +0300 | [diff] [blame] | 34 | if not has_magic(pathname): |
| 35 | if basename: |
| 36 | if os.path.lexists(pathname): |
| 37 | yield pathname |
| 38 | else: |
| 39 | # Patterns ending with a slash should match only directories |
| 40 | if os.path.isdir(dirname): |
| 41 | yield pathname |
| 42 | return |
Tim Golden | 9b3fb0c | 2012-11-06 15:33:30 +0000 | [diff] [blame] | 43 | if not dirname: |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 44 | if recursive and _isrecursive(basename): |
| 45 | yield from glob2(dirname, basename) |
| 46 | else: |
| 47 | yield from glob1(dirname, basename) |
Tim Golden | 9b3fb0c | 2012-11-06 15:33:30 +0000 | [diff] [blame] | 48 | return |
Antoine Pitrou | 3d068b2 | 2012-12-16 13:49:37 +0100 | [diff] [blame] | 49 | # `os.path.split()` returns the argument itself as a dirname if it is a |
| 50 | # drive or UNC path. Prevent an infinite recursion if a drive or UNC path |
| 51 | # contains magic characters (i.e. r'\\?\C:'). |
| 52 | if dirname != pathname and has_magic(dirname): |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 53 | dirs = iglob(dirname, recursive=recursive) |
Tim Golden | 9b3fb0c | 2012-11-06 15:33:30 +0000 | [diff] [blame] | 54 | else: |
| 55 | dirs = [dirname] |
| 56 | if has_magic(basename): |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 57 | if recursive and _isrecursive(basename): |
| 58 | glob_in_dir = glob2 |
| 59 | else: |
| 60 | glob_in_dir = glob1 |
Tim Golden | 9b3fb0c | 2012-11-06 15:33:30 +0000 | [diff] [blame] | 61 | else: |
| 62 | 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) |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 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 Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 70 | |
| 71 | def glob1(dirname, pattern): |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 72 | if not dirname: |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 73 | if isinstance(pattern, bytes): |
| 74 | dirname = bytes(os.curdir, 'ASCII') |
| 75 | else: |
| 76 | dirname = os.curdir |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 77 | try: |
| 78 | names = os.listdir(dirname) |
Andrew Svetlov | ad28c7f | 2012-12-18 22:02:39 +0200 | [diff] [blame] | 79 | except OSError: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 80 | return [] |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 81 | if not _ishidden(pattern): |
| 82 | names = [x for x in names if not _ishidden(x)] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 83 | return fnmatch.filter(names, pattern) |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 84 | |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 85 | def glob0(dirname, basename): |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 86 | if not basename: |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 87 | # `os.path.split()` returns an empty basename for paths ending with a |
| 88 | # directory separator. 'q*x/' should match only directories. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 89 | if os.path.isdir(dirname): |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 90 | return [basename] |
| 91 | else: |
| 92 | if os.path.lexists(os.path.join(dirname, basename)): |
| 93 | return [basename] |
| 94 | return [] |
| 95 | |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 96 | # This helper function recursively yields relative pathnames inside a literal |
| 97 | # directory. |
| 98 | |
| 99 | def glob2(dirname, pattern): |
| 100 | assert _isrecursive(pattern) |
| 101 | if dirname: |
| 102 | yield pattern[:0] |
| 103 | yield from _rlistdir(dirname) |
| 104 | |
| 105 | # Recursively yields relative pathnames inside a literal directory. |
| 106 | |
| 107 | def _rlistdir(dirname): |
| 108 | if not dirname: |
| 109 | if isinstance(dirname, bytes): |
| 110 | dirname = bytes(os.curdir, 'ASCII') |
| 111 | else: |
| 112 | dirname = os.curdir |
| 113 | try: |
| 114 | names = os.listdir(dirname) |
| 115 | except os.error: |
| 116 | return |
| 117 | for x in names: |
| 118 | if not _ishidden(x): |
| 119 | yield x |
| 120 | path = os.path.join(dirname, x) if dirname else x |
| 121 | for y in _rlistdir(path): |
| 122 | yield os.path.join(x, y) |
| 123 | |
Guido van Rossum | c2ef5c2 | 1992-01-12 23:32:11 +0000 | [diff] [blame] | 124 | |
Serhiy Storchaka | fd32fff | 2013-11-18 13:06:43 +0200 | [diff] [blame] | 125 | magic_check = re.compile('([*?[])') |
| 126 | magic_check_bytes = re.compile(b'([*?[])') |
Tim Golden | 9b3fb0c | 2012-11-06 15:33:30 +0000 | [diff] [blame] | 127 | |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 128 | def has_magic(s): |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 129 | if isinstance(s, bytes): |
| 130 | match = magic_check_bytes.search(s) |
| 131 | else: |
| 132 | match = magic_check.search(s) |
| 133 | return match is not None |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 134 | |
| 135 | def _ishidden(path): |
| 136 | return path[0] in ('.', b'.'[0]) |
Serhiy Storchaka | fd32fff | 2013-11-18 13:06:43 +0200 | [diff] [blame] | 137 | |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 138 | def _isrecursive(pattern): |
| 139 | if isinstance(pattern, bytes): |
| 140 | return pattern == b'**' |
| 141 | else: |
| 142 | return pattern == '**' |
| 143 | |
Serhiy Storchaka | fd32fff | 2013-11-18 13:06:43 +0200 | [diff] [blame] | 144 | def escape(pathname): |
| 145 | """Escape all special characters. |
| 146 | """ |
| 147 | # Escaping is done by wrapping any of "*?[" between square brackets. |
| 148 | # Metacharacters do not work in the drive part and shouldn't be escaped. |
| 149 | drive, pathname = os.path.splitdrive(pathname) |
| 150 | if isinstance(pathname, bytes): |
| 151 | pathname = magic_check_bytes.sub(br'[\1]', pathname) |
| 152 | else: |
| 153 | pathname = magic_check.sub(r'[\1]', pathname) |
| 154 | return drive + pathname |