| 
									
										
										
										
											1997-04-02 05:47:11 +00:00
										 |  |  | """Filename globbing utility.""" | 
					
						
							| 
									
										
										
										
											1991-01-01 18:17:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-07 08:31:51 +00:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											1992-01-12 23:26:24 +00:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											1997-10-22 21:00:49 +00:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											2007-03-07 08:31:51 +00:00
										 |  |  | import fnmatch | 
					
						
							| 
									
										
										
										
											1991-01-01 18:17:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-01-08 13:13:19 +00:00
										 |  |  | __all__ = ["glob", "iglob"] | 
					
						
							| 
									
										
										
										
											1992-01-12 23:26:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1991-01-01 18:17:49 +00:00
										 |  |  | def glob(pathname): | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     """Return a list of paths matching a pathname pattern.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     The pattern may contain simple shell-style wildcards a la fnmatch. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-01-08 13:13:19 +00:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     return list(iglob(pathname)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def iglob(pathname): | 
					
						
							|  |  |  |     """Return a list of paths matching a pathname pattern.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     The pattern may contain simple shell-style wildcards a la fnmatch. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     if not has_magic(pathname): | 
					
						
							| 
									
										
										
										
											2004-08-30 10:19:56 +00:00
										 |  |  |         if os.path.lexists(pathname): | 
					
						
							| 
									
										
										
										
											2005-01-08 13:13:19 +00:00
										 |  |  |             yield pathname | 
					
						
							|  |  |  |         return | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     dirname, basename = os.path.split(pathname) | 
					
						
							| 
									
										
										
										
											2001-06-06 06:24:38 +00:00
										 |  |  |     if not dirname: | 
					
						
							| 
									
										
										
										
											2005-01-08 13:13:19 +00:00
										 |  |  |         for name in glob1(os.curdir, basename): | 
					
						
							|  |  |  |             yield name | 
					
						
							|  |  |  |         return | 
					
						
							|  |  |  |     if has_magic(dirname): | 
					
						
							|  |  |  |         dirs = iglob(dirname) | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2005-01-08 13:13:19 +00:00
										 |  |  |         dirs = [dirname] | 
					
						
							|  |  |  |     if has_magic(basename): | 
					
						
							|  |  |  |         glob_in_dir = glob1 | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2005-01-08 13:13:19 +00:00
										 |  |  |         glob_in_dir = glob0 | 
					
						
							|  |  |  |     for dirname in dirs: | 
					
						
							|  |  |  |         for name in glob_in_dir(dirname, basename): | 
					
						
							|  |  |  |             yield os.path.join(dirname, name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # These 2 helper functions non-recursively glob inside a literal directory. | 
					
						
							|  |  |  | # They return a list of basenames. `glob1` accepts a pattern while `glob0` | 
					
						
							|  |  |  | # takes a literal basename (so it only has to check for its existence). | 
					
						
							| 
									
										
										
										
											1991-01-01 18:17:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def glob1(dirname, pattern): | 
					
						
							| 
									
										
										
										
											2005-01-08 13:13:19 +00:00
										 |  |  |     if not dirname: | 
					
						
							|  |  |  |         dirname = os.curdir | 
					
						
							| 
									
										
										
										
											2007-03-07 08:31:51 +00:00
										 |  |  |     if isinstance(pattern, unicode) and not isinstance(dirname, unicode): | 
					
						
							| 
									
										
										
										
											2007-03-20 23:05:14 +00:00
										 |  |  |         dirname = unicode(dirname, sys.getfilesystemencoding() or | 
					
						
							|  |  |  |                                    sys.getdefaultencoding()) | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     try: | 
					
						
							|  |  |  |         names = os.listdir(dirname) | 
					
						
							|  |  |  |     except os.error: | 
					
						
							|  |  |  |         return [] | 
					
						
							| 
									
										
										
										
											2007-03-07 08:31:51 +00:00
										 |  |  |     if pattern[0] != '.': | 
					
						
							|  |  |  |         names = filter(lambda x: x[0] != '.', names) | 
					
						
							|  |  |  |     return fnmatch.filter(names, pattern) | 
					
						
							| 
									
										
										
										
											1991-01-01 18:17:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-01-08 13:13:19 +00:00
										 |  |  | def glob0(dirname, basename): | 
					
						
							|  |  |  |     if basename == '': | 
					
						
							|  |  |  |         # `os.path.split()` returns an empty basename for paths ending with a | 
					
						
							|  |  |  |         # directory separator.  'q*x/' should match only directories. | 
					
						
							| 
									
										
										
										
											2006-04-09 03:35:43 +00:00
										 |  |  |         if os.path.isdir(dirname): | 
					
						
							| 
									
										
										
										
											2005-01-08 13:13:19 +00:00
										 |  |  |             return [basename] | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         if os.path.lexists(os.path.join(dirname, basename)): | 
					
						
							|  |  |  |             return [basename] | 
					
						
							|  |  |  |     return [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-01-12 23:32:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-10-22 21:00:49 +00:00
										 |  |  | magic_check = re.compile('[*?[]') | 
					
						
							| 
									
										
										
										
											1992-01-12 23:32:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1991-01-01 18:17:49 +00:00
										 |  |  | def has_magic(s): | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     return magic_check.search(s) is not None |