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 | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 4 | import fnmatch |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 5 | import re |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | bba77af | 1992-01-12 23:26:24 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 8 | def glob(pathname): |
Guido van Rossum | ab096c9 | 1997-04-02 05:47:11 +0000 | [diff] [blame] | 9 | """Return a list of paths matching a pathname pattern. |
| 10 | |
| 11 | The pattern may contain simple shell-style wildcards a la fnmatch. |
| 12 | |
| 13 | """ |
Guido van Rossum | c2ef5c2 | 1992-01-12 23:32:11 +0000 | [diff] [blame] | 14 | if not has_magic(pathname): |
| 15 | if os.path.exists(pathname): |
| 16 | return [pathname] |
| 17 | else: |
| 18 | return [] |
Guido van Rossum | bba77af | 1992-01-12 23:26:24 +0000 | [diff] [blame] | 19 | dirname, basename = os.path.split(pathname) |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 20 | if has_magic(dirname): |
| 21 | list = glob(dirname) |
| 22 | else: |
| 23 | list = [dirname] |
| 24 | if not has_magic(basename): |
| 25 | result = [] |
| 26 | for dirname in list: |
Guido van Rossum | bba77af | 1992-01-12 23:26:24 +0000 | [diff] [blame] | 27 | if basename or os.path.isdir(dirname): |
| 28 | name = os.path.join(dirname, basename) |
| 29 | if os.path.exists(name): |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 30 | result.append(name) |
| 31 | else: |
| 32 | result = [] |
| 33 | for dirname in list: |
| 34 | sublist = glob1(dirname, basename) |
| 35 | for name in sublist: |
Guido van Rossum | bba77af | 1992-01-12 23:26:24 +0000 | [diff] [blame] | 36 | result.append(os.path.join(dirname, name)) |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 37 | return result |
| 38 | |
| 39 | def glob1(dirname, pattern): |
Guido van Rossum | bba77af | 1992-01-12 23:26:24 +0000 | [diff] [blame] | 40 | if not dirname: dirname = os.curdir |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 41 | try: |
Guido van Rossum | bba77af | 1992-01-12 23:26:24 +0000 | [diff] [blame] | 42 | names = os.listdir(dirname) |
| 43 | except os.error: |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 44 | return [] |
| 45 | result = [] |
| 46 | for name in names: |
Guido van Rossum | c2ef5c2 | 1992-01-12 23:32:11 +0000 | [diff] [blame] | 47 | if name[0] != '.' or pattern[0] == '.': |
| 48 | if fnmatch.fnmatch(name, pattern): |
| 49 | result.append(name) |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 50 | return result |
| 51 | |
Guido van Rossum | c2ef5c2 | 1992-01-12 23:32:11 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 53 | magic_check = re.compile('[*?[]') |
Guido van Rossum | c2ef5c2 | 1992-01-12 23:32:11 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 55 | def has_magic(s): |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 56 | return magic_check.search(s) is not None |