blob: 36d493d7a8032656ce49e1531ab1e9508564220d [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 Peters07e99cb2001-01-14 23:47:14 +000027 dirname, basename = os.path.split(pathname)
Martin v. Löwisb5d4d2a2001-06-06 06:24:38 +000028 if not dirname:
Guido van Rossumf0af3e32008-10-02 18:55:37 +000029 for name in glob1(None, basename):
Johannes Gijsbers836f5432005-01-08 13:13:19 +000030 yield name
31 return
32 if has_magic(dirname):
33 dirs = iglob(dirname)
Tim Peters07e99cb2001-01-14 23:47:14 +000034 else:
Johannes Gijsbers836f5432005-01-08 13:13:19 +000035 dirs = [dirname]
36 if has_magic(basename):
37 glob_in_dir = glob1
Tim Peters07e99cb2001-01-14 23:47:14 +000038 else:
Johannes Gijsbers836f5432005-01-08 13:13:19 +000039 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 Rossum65a96201991-01-01 18:17:49 +000047
48def glob1(dirname, pattern):
Johannes Gijsbers836f5432005-01-08 13:13:19 +000049 if not dirname:
Guido van Rossumf0af3e32008-10-02 18:55:37 +000050 if isinstance(pattern, bytes):
51 dirname = bytes(os.curdir, 'ASCII')
52 else:
53 dirname = os.curdir
Tim Peters07e99cb2001-01-14 23:47:14 +000054 try:
55 names = os.listdir(dirname)
56 except os.error:
57 return []
Guido van Rossumd8faa362007-04-27 19:54:29 +000058 if pattern[0] != '.':
Alexandre Vassalotti06b1ab8ed2008-01-27 16:16:19 +000059 names = [x for x in names if x[0] != '.']
Guido van Rossumd8faa362007-04-27 19:54:29 +000060 return fnmatch.filter(names, pattern)
Guido van Rossum65a96201991-01-01 18:17:49 +000061
Johannes Gijsbers836f5432005-01-08 13:13:19 +000062def 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 Wouters49fd7fa2006-04-21 10:40:58 +000066 if os.path.isdir(dirname):
Johannes Gijsbers836f5432005-01-08 13:13:19 +000067 return [basename]
68 else:
69 if os.path.lexists(os.path.join(dirname, basename)):
70 return [basename]
71 return []
72
Guido van Rossumc2ef5c21992-01-12 23:32:11 +000073
Guido van Rossum9694fca1997-10-22 21:00:49 +000074magic_check = re.compile('[*?[]')
Guido van Rossumf0af3e32008-10-02 18:55:37 +000075magic_check_bytes = re.compile(b'[*?[]')
Guido van Rossumc2ef5c21992-01-12 23:32:11 +000076
Guido van Rossum65a96201991-01-01 18:17:49 +000077def has_magic(s):
Guido van Rossumf0af3e32008-10-02 18:55:37 +000078 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