blob: 95656cc1a6dd3657de543d6ec5c479b2d4e10370 [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 Rossum65a96201991-01-01 18:17:49 +00004import fnmatch
Guido van Rossum9694fca1997-10-22 21:00:49 +00005import re
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):
18 """Return a list of paths matching a pathname pattern.
19
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:
Johannes Gijsbers836f5432005-01-08 13:13:19 +000029 for name in glob1(os.curdir, basename):
30 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:
50 dirname = os.curdir
Tim Peters07e99cb2001-01-14 23:47:14 +000051 try:
52 names = os.listdir(dirname)
53 except os.error:
54 return []
Martin v. Löwisb5d4d2a2001-06-06 06:24:38 +000055 if pattern[0]!='.':
56 names=filter(lambda x: x[0]!='.',names)
57 return fnmatch.filter(names,pattern)
Guido van Rossum65a96201991-01-01 18:17:49 +000058
Johannes Gijsbers836f5432005-01-08 13:13:19 +000059def glob0(dirname, basename):
60 if basename == '':
61 # `os.path.split()` returns an empty basename for paths ending with a
62 # directory separator. 'q*x/' should match only directories.
Neal Norwitza31bf182006-04-09 03:35:43 +000063 if os.path.isdir(dirname):
Johannes Gijsbers836f5432005-01-08 13:13:19 +000064 return [basename]
65 else:
66 if os.path.lexists(os.path.join(dirname, basename)):
67 return [basename]
68 return []
69
Guido van Rossumc2ef5c21992-01-12 23:32:11 +000070
Guido van Rossum9694fca1997-10-22 21:00:49 +000071magic_check = re.compile('[*?[]')
Guido van Rossumc2ef5c21992-01-12 23:32:11 +000072
Guido van Rossum65a96201991-01-01 18:17:49 +000073def has_magic(s):
Tim Peters07e99cb2001-01-14 23:47:14 +000074 return magic_check.search(s) is not None