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 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 12 | The pattern may contain simple shell-style wildcards a la fnmatch. |
Guido van Rossum | ab096c9 | 1997-04-02 05:47:11 +0000 | [diff] [blame] | 13 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 14 | """ |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 15 | return list(iglob(pathname)) |
| 16 | |
| 17 | def iglob(pathname): |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 18 | """Return an iterator which yields the paths matching a pathname pattern. |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 19 | |
| 20 | The pattern may contain simple shell-style wildcards a la fnmatch. |
| 21 | |
| 22 | """ |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 23 | if not has_magic(pathname): |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 24 | if os.path.lexists(pathname): |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 25 | yield pathname |
| 26 | return |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 27 | dirname, basename = os.path.split(pathname) |
Martin v. Löwis | b5d4d2a | 2001-06-06 06:24:38 +0000 | [diff] [blame] | 28 | if not dirname: |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 29 | for name in glob1(None, basename): |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 30 | yield name |
| 31 | return |
| 32 | if has_magic(dirname): |
| 33 | dirs = iglob(dirname) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 34 | else: |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 35 | dirs = [dirname] |
| 36 | if has_magic(basename): |
| 37 | glob_in_dir = glob1 |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 38 | else: |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 39 | glob_in_dir = glob0 |
| 40 | for dirname in dirs: |
| 41 | for name in glob_in_dir(dirname, basename): |
| 42 | yield os.path.join(dirname, name) |
| 43 | |
| 44 | # These 2 helper functions non-recursively glob inside a literal directory. |
| 45 | # They return a list of basenames. `glob1` accepts a pattern while `glob0` |
| 46 | # 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] | 47 | |
| 48 | def glob1(dirname, pattern): |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 49 | if not dirname: |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 50 | if isinstance(pattern, bytes): |
| 51 | dirname = bytes(os.curdir, 'ASCII') |
| 52 | else: |
| 53 | dirname = os.curdir |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 54 | try: |
| 55 | names = os.listdir(dirname) |
| 56 | except os.error: |
| 57 | return [] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 58 | if pattern[0] != '.': |
Alexandre Vassalotti | 06b1ab8ed | 2008-01-27 16:16:19 +0000 | [diff] [blame] | 59 | names = [x for x in names if x[0] != '.'] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 60 | return fnmatch.filter(names, pattern) |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 61 | |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 62 | def glob0(dirname, basename): |
| 63 | if basename == '': |
| 64 | # `os.path.split()` returns an empty basename for paths ending with a |
| 65 | # directory separator. 'q*x/' should match only directories. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 66 | if os.path.isdir(dirname): |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 67 | return [basename] |
| 68 | else: |
| 69 | if os.path.lexists(os.path.join(dirname, basename)): |
| 70 | return [basename] |
| 71 | return [] |
| 72 | |
Guido van Rossum | c2ef5c2 | 1992-01-12 23:32:11 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 74 | magic_check = re.compile('[*?[]') |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 75 | magic_check_bytes = re.compile(b'[*?[]') |
Guido van Rossum | c2ef5c2 | 1992-01-12 23:32:11 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 77 | def has_magic(s): |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 78 | if isinstance(s, bytes): |
| 79 | match = magic_check_bytes.search(s) |
| 80 | else: |
| 81 | match = magic_check.search(s) |
| 82 | return match is not None |