logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

glob.py (7888B)


  1. """Filename globbing utility."""
  2. import contextlib
  3. import os
  4. import re
  5. import fnmatch
  6. import itertools
  7. import stat
  8. import sys
  9. __all__ = ["glob", "iglob", "escape"]
  10. def glob(pathname, *, root_dir=None, dir_fd=None, recursive=False):
  11. """Return a list of paths matching a pathname pattern.
  12. The pattern may contain simple shell-style wildcards a la
  13. fnmatch. However, unlike fnmatch, filenames starting with a
  14. dot are special cases that are not matched by '*' and '?'
  15. patterns.
  16. If recursive is true, the pattern '**' will match any files and
  17. zero or more directories and subdirectories.
  18. """
  19. return list(iglob(pathname, root_dir=root_dir, dir_fd=dir_fd, recursive=recursive))
  20. def iglob(pathname, *, root_dir=None, dir_fd=None, recursive=False):
  21. """Return an iterator which yields the paths matching a pathname pattern.
  22. The pattern may contain simple shell-style wildcards a la
  23. fnmatch. However, unlike fnmatch, filenames starting with a
  24. dot are special cases that are not matched by '*' and '?'
  25. patterns.
  26. If recursive is true, the pattern '**' will match any files and
  27. zero or more directories and subdirectories.
  28. """
  29. sys.audit("glob.glob", pathname, recursive)
  30. sys.audit("glob.glob/2", pathname, recursive, root_dir, dir_fd)
  31. if root_dir is not None:
  32. root_dir = os.fspath(root_dir)
  33. else:
  34. root_dir = pathname[:0]
  35. it = _iglob(pathname, root_dir, dir_fd, recursive, False)
  36. if not pathname or recursive and _isrecursive(pathname[:2]):
  37. try:
  38. s = next(it) # skip empty string
  39. if s:
  40. it = itertools.chain((s,), it)
  41. except StopIteration:
  42. pass
  43. return it
  44. def _iglob(pathname, root_dir, dir_fd, recursive, dironly):
  45. dirname, basename = os.path.split(pathname)
  46. if not has_magic(pathname):
  47. assert not dironly
  48. if basename:
  49. if _lexists(_join(root_dir, pathname), dir_fd):
  50. yield pathname
  51. else:
  52. # Patterns ending with a slash should match only directories
  53. if _isdir(_join(root_dir, dirname), dir_fd):
  54. yield pathname
  55. return
  56. if not dirname:
  57. if recursive and _isrecursive(basename):
  58. yield from _glob2(root_dir, basename, dir_fd, dironly)
  59. else:
  60. yield from _glob1(root_dir, basename, dir_fd, dironly)
  61. return
  62. # `os.path.split()` returns the argument itself as a dirname if it is a
  63. # drive or UNC path. Prevent an infinite recursion if a drive or UNC path
  64. # contains magic characters (i.e. r'\\?\C:').
  65. if dirname != pathname and has_magic(dirname):
  66. dirs = _iglob(dirname, root_dir, dir_fd, recursive, True)
  67. else:
  68. dirs = [dirname]
  69. if has_magic(basename):
  70. if recursive and _isrecursive(basename):
  71. glob_in_dir = _glob2
  72. else:
  73. glob_in_dir = _glob1
  74. else:
  75. glob_in_dir = _glob0
  76. for dirname in dirs:
  77. for name in glob_in_dir(_join(root_dir, dirname), basename, dir_fd, dironly):
  78. yield os.path.join(dirname, name)
  79. # These 2 helper functions non-recursively glob inside a literal directory.
  80. # They return a list of basenames. _glob1 accepts a pattern while _glob0
  81. # takes a literal basename (so it only has to check for its existence).
  82. def _glob1(dirname, pattern, dir_fd, dironly):
  83. names = _listdir(dirname, dir_fd, dironly)
  84. if not _ishidden(pattern):
  85. names = (x for x in names if not _ishidden(x))
  86. return fnmatch.filter(names, pattern)
  87. def _glob0(dirname, basename, dir_fd, dironly):
  88. if basename:
  89. if _lexists(_join(dirname, basename), dir_fd):
  90. return [basename]
  91. else:
  92. # `os.path.split()` returns an empty basename for paths ending with a
  93. # directory separator. 'q*x/' should match only directories.
  94. if _isdir(dirname, dir_fd):
  95. return [basename]
  96. return []
  97. # Following functions are not public but can be used by third-party code.
  98. def glob0(dirname, pattern):
  99. return _glob0(dirname, pattern, None, False)
  100. def glob1(dirname, pattern):
  101. return _glob1(dirname, pattern, None, False)
  102. # This helper function recursively yields relative pathnames inside a literal
  103. # directory.
  104. def _glob2(dirname, pattern, dir_fd, dironly):
  105. assert _isrecursive(pattern)
  106. yield pattern[:0]
  107. yield from _rlistdir(dirname, dir_fd, dironly)
  108. # If dironly is false, yields all file names inside a directory.
  109. # If dironly is true, yields only directory names.
  110. def _iterdir(dirname, dir_fd, dironly):
  111. try:
  112. fd = None
  113. fsencode = None
  114. if dir_fd is not None:
  115. if dirname:
  116. fd = arg = os.open(dirname, _dir_open_flags, dir_fd=dir_fd)
  117. else:
  118. arg = dir_fd
  119. if isinstance(dirname, bytes):
  120. fsencode = os.fsencode
  121. elif dirname:
  122. arg = dirname
  123. elif isinstance(dirname, bytes):
  124. arg = bytes(os.curdir, 'ASCII')
  125. else:
  126. arg = os.curdir
  127. try:
  128. with os.scandir(arg) as it:
  129. for entry in it:
  130. try:
  131. if not dironly or entry.is_dir():
  132. if fsencode is not None:
  133. yield fsencode(entry.name)
  134. else:
  135. yield entry.name
  136. except OSError:
  137. pass
  138. finally:
  139. if fd is not None:
  140. os.close(fd)
  141. except OSError:
  142. return
  143. def _listdir(dirname, dir_fd, dironly):
  144. with contextlib.closing(_iterdir(dirname, dir_fd, dironly)) as it:
  145. return list(it)
  146. # Recursively yields relative pathnames inside a literal directory.
  147. def _rlistdir(dirname, dir_fd, dironly):
  148. names = _listdir(dirname, dir_fd, dironly)
  149. for x in names:
  150. if not _ishidden(x):
  151. yield x
  152. path = _join(dirname, x) if dirname else x
  153. for y in _rlistdir(path, dir_fd, dironly):
  154. yield _join(x, y)
  155. def _lexists(pathname, dir_fd):
  156. # Same as os.path.lexists(), but with dir_fd
  157. if dir_fd is None:
  158. return os.path.lexists(pathname)
  159. try:
  160. os.lstat(pathname, dir_fd=dir_fd)
  161. except (OSError, ValueError):
  162. return False
  163. else:
  164. return True
  165. def _isdir(pathname, dir_fd):
  166. # Same as os.path.isdir(), but with dir_fd
  167. if dir_fd is None:
  168. return os.path.isdir(pathname)
  169. try:
  170. st = os.stat(pathname, dir_fd=dir_fd)
  171. except (OSError, ValueError):
  172. return False
  173. else:
  174. return stat.S_ISDIR(st.st_mode)
  175. def _join(dirname, basename):
  176. # It is common if dirname or basename is empty
  177. if not dirname or not basename:
  178. return dirname or basename
  179. return os.path.join(dirname, basename)
  180. magic_check = re.compile('([*?[])')
  181. magic_check_bytes = re.compile(b'([*?[])')
  182. def has_magic(s):
  183. if isinstance(s, bytes):
  184. match = magic_check_bytes.search(s)
  185. else:
  186. match = magic_check.search(s)
  187. return match is not None
  188. def _ishidden(path):
  189. return path[0] in ('.', b'.'[0])
  190. def _isrecursive(pattern):
  191. if isinstance(pattern, bytes):
  192. return pattern == b'**'
  193. else:
  194. return pattern == '**'
  195. def escape(pathname):
  196. """Escape all special characters.
  197. """
  198. # Escaping is done by wrapping any of "*?[" between square brackets.
  199. # Metacharacters do not work in the drive part and shouldn't be escaped.
  200. drive, pathname = os.path.splitdrive(pathname)
  201. if isinstance(pathname, bytes):
  202. pathname = magic_check_bytes.sub(br'[\1]', pathname)
  203. else:
  204. pathname = magic_check.sub(r'[\1]', pathname)
  205. return drive + pathname
  206. _dir_open_flags = os.O_RDONLY | getattr(os, 'O_DIRECTORY', 0)