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 | |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 9 | def glob(pathname): |
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 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 17 | """ |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 18 | return list(iglob(pathname)) |
| 19 | |
| 20 | def iglob(pathname): |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 21 | """Return an iterator which yields the paths matching a pathname pattern. |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 22 | |
Petri Lehtinen | ee4a20b | 2013-02-23 19:53:03 +0100 | [diff] [blame] | 23 | 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 Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 27 | |
| 28 | """ |
Tim Golden | 9b3fb0c | 2012-11-06 15:33:30 +0000 | [diff] [blame] | 29 | dirname, basename = os.path.split(pathname) |
Serhiy Storchaka | 6f20170 | 2014-08-12 12:55:12 +0300 | [diff] [blame] | 30 | if not has_magic(pathname): |
| 31 | if basename: |
| 32 | if os.path.lexists(pathname): |
| 33 | yield pathname |
| 34 | else: |
| 35 | # Patterns ending with a slash should match only directories |
| 36 | if os.path.isdir(dirname): |
| 37 | yield pathname |
| 38 | return |
Tim Golden | 9b3fb0c | 2012-11-06 15:33:30 +0000 | [diff] [blame] | 39 | if not dirname: |
| 40 | yield from glob1(None, basename) |
| 41 | return |
Antoine Pitrou | 3d068b2 | 2012-12-16 13:49:37 +0100 | [diff] [blame] | 42 | # `os.path.split()` returns the argument itself as a dirname if it is a |
| 43 | # drive or UNC path. Prevent an infinite recursion if a drive or UNC path |
| 44 | # contains magic characters (i.e. r'\\?\C:'). |
| 45 | if dirname != pathname and has_magic(dirname): |
Tim Golden | 9b3fb0c | 2012-11-06 15:33:30 +0000 | [diff] [blame] | 46 | dirs = iglob(dirname) |
| 47 | else: |
| 48 | dirs = [dirname] |
| 49 | if has_magic(basename): |
| 50 | glob_in_dir = glob1 |
| 51 | else: |
| 52 | glob_in_dir = glob0 |
| 53 | for dirname in dirs: |
| 54 | for name in glob_in_dir(dirname, basename): |
| 55 | yield os.path.join(dirname, name) |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 56 | |
| 57 | # These 2 helper functions non-recursively glob inside a literal directory. |
| 58 | # They return a list of basenames. `glob1` accepts a pattern while `glob0` |
| 59 | # 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] | 60 | |
| 61 | def glob1(dirname, pattern): |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 62 | if not dirname: |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 63 | if isinstance(pattern, bytes): |
| 64 | dirname = bytes(os.curdir, 'ASCII') |
| 65 | else: |
| 66 | dirname = os.curdir |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 67 | try: |
| 68 | names = os.listdir(dirname) |
Andrew Svetlov | ad28c7f | 2012-12-18 22:02:39 +0200 | [diff] [blame] | 69 | except OSError: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 70 | return [] |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 71 | if not _ishidden(pattern): |
| 72 | names = [x for x in names if not _ishidden(x)] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 73 | return fnmatch.filter(names, pattern) |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 74 | |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 75 | def glob0(dirname, basename): |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 76 | if not basename: |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 77 | # `os.path.split()` returns an empty basename for paths ending with a |
| 78 | # directory separator. 'q*x/' should match only directories. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 79 | if os.path.isdir(dirname): |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 80 | return [basename] |
| 81 | else: |
| 82 | if os.path.lexists(os.path.join(dirname, basename)): |
| 83 | return [basename] |
| 84 | return [] |
| 85 | |
Guido van Rossum | c2ef5c2 | 1992-01-12 23:32:11 +0000 | [diff] [blame] | 86 | |
Serhiy Storchaka | fd32fff | 2013-11-18 13:06:43 +0200 | [diff] [blame] | 87 | magic_check = re.compile('([*?[])') |
| 88 | magic_check_bytes = re.compile(b'([*?[])') |
Tim Golden | 9b3fb0c | 2012-11-06 15:33:30 +0000 | [diff] [blame] | 89 | |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 90 | def has_magic(s): |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 91 | if isinstance(s, bytes): |
| 92 | match = magic_check_bytes.search(s) |
| 93 | else: |
| 94 | match = magic_check.search(s) |
| 95 | return match is not None |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 96 | |
| 97 | def _ishidden(path): |
| 98 | return path[0] in ('.', b'.'[0]) |
Serhiy Storchaka | fd32fff | 2013-11-18 13:06:43 +0200 | [diff] [blame] | 99 | |
| 100 | def escape(pathname): |
| 101 | """Escape all special characters. |
| 102 | """ |
| 103 | # Escaping is done by wrapping any of "*?[" between square brackets. |
| 104 | # Metacharacters do not work in the drive part and shouldn't be escaped. |
| 105 | drive, pathname = os.path.splitdrive(pathname) |
| 106 | if isinstance(pathname, bytes): |
| 107 | pathname = magic_check_bytes.sub(br'[\1]', pathname) |
| 108 | else: |
| 109 | pathname = magic_check.sub(r'[\1]', pathname) |
| 110 | return drive + pathname |