_common.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import os
  2. import pathlib
  3. import tempfile
  4. import functools
  5. import contextlib
  6. import types
  7. import importlib
  8. import inspect
  9. import warnings
  10. import itertools
  11. from typing import Union, Optional, cast
  12. from .abc import ResourceReader, Traversable
  13. from ._adapters import wrap_spec
  14. Package = Union[types.ModuleType, str]
  15. Anchor = Package
  16. def package_to_anchor(func):
  17. """
  18. Replace 'package' parameter as 'anchor' and warn about the change.
  19. Other errors should fall through.
  20. >>> files('a', 'b')
  21. Traceback (most recent call last):
  22. TypeError: files() takes from 0 to 1 positional arguments but 2 were given
  23. """
  24. undefined = object()
  25. @functools.wraps(func)
  26. def wrapper(anchor=undefined, package=undefined):
  27. if package is not undefined:
  28. if anchor is not undefined:
  29. return func(anchor, package)
  30. warnings.warn(
  31. "First parameter to files is renamed to 'anchor'",
  32. DeprecationWarning,
  33. stacklevel=2,
  34. )
  35. return func(package)
  36. elif anchor is undefined:
  37. return func()
  38. return func(anchor)
  39. return wrapper
  40. @package_to_anchor
  41. def files(anchor: Optional[Anchor] = None) -> Traversable:
  42. """
  43. Get a Traversable resource for an anchor.
  44. """
  45. return from_package(resolve(anchor))
  46. def get_resource_reader(package: types.ModuleType) -> Optional[ResourceReader]:
  47. """
  48. Return the package's loader if it's a ResourceReader.
  49. """
  50. # We can't use
  51. # a issubclass() check here because apparently abc.'s __subclasscheck__()
  52. # hook wants to create a weak reference to the object, but
  53. # zipimport.zipimporter does not support weak references, resulting in a
  54. # TypeError. That seems terrible.
  55. spec = package.__spec__
  56. reader = getattr(spec.loader, 'get_resource_reader', None) # type: ignore
  57. if reader is None:
  58. return None
  59. return reader(spec.name) # type: ignore
  60. @functools.singledispatch
  61. def resolve(cand: Optional[Anchor]) -> types.ModuleType:
  62. return cast(types.ModuleType, cand)
  63. @resolve.register
  64. def _(cand: str) -> types.ModuleType:
  65. return importlib.import_module(cand)
  66. @resolve.register
  67. def _(cand: None) -> types.ModuleType:
  68. return resolve(_infer_caller().f_globals['__name__'])
  69. def _infer_caller():
  70. """
  71. Walk the stack and find the frame of the first caller not in this module.
  72. """
  73. def is_this_file(frame_info):
  74. return frame_info.filename == stack[0].filename
  75. def is_wrapper(frame_info):
  76. return frame_info.function == 'wrapper'
  77. stack = inspect.stack()
  78. not_this_file = itertools.filterfalse(is_this_file, stack)
  79. # also exclude 'wrapper' due to singledispatch in the call stack
  80. callers = itertools.filterfalse(is_wrapper, not_this_file)
  81. return next(callers).frame
  82. def from_package(package: types.ModuleType):
  83. """
  84. Return a Traversable object for the given package.
  85. """
  86. spec = wrap_spec(package)
  87. reader = spec.loader.get_resource_reader(spec.name)
  88. return reader.files()
  89. @contextlib.contextmanager
  90. def _tempfile(
  91. reader,
  92. suffix='',
  93. # gh-93353: Keep a reference to call os.remove() in late Python
  94. # finalization.
  95. *,
  96. _os_remove=os.remove,
  97. ):
  98. # Not using tempfile.NamedTemporaryFile as it leads to deeper 'try'
  99. # blocks due to the need to close the temporary file to work on Windows
  100. # properly.
  101. fd, raw_path = tempfile.mkstemp(suffix=suffix)
  102. try:
  103. try:
  104. os.write(fd, reader())
  105. finally:
  106. os.close(fd)
  107. del reader
  108. yield pathlib.Path(raw_path)
  109. finally:
  110. try:
  111. _os_remove(raw_path)
  112. except FileNotFoundError:
  113. pass
  114. def _temp_file(path):
  115. return _tempfile(path.read_bytes, suffix=path.name)
  116. def _is_present_dir(path: Traversable) -> bool:
  117. """
  118. Some Traversables implement ``is_dir()`` to raise an
  119. exception (i.e. ``FileNotFoundError``) when the
  120. directory doesn't exist. This function wraps that call
  121. to always return a boolean and only return True
  122. if there's a dir and it exists.
  123. """
  124. with contextlib.suppress(FileNotFoundError):
  125. return path.is_dir()
  126. return False
  127. @functools.singledispatch
  128. def as_file(path):
  129. """
  130. Given a Traversable object, return that object as a
  131. path on the local file system in a context manager.
  132. """
  133. return _temp_dir(path) if _is_present_dir(path) else _temp_file(path)
  134. @as_file.register(pathlib.Path)
  135. @contextlib.contextmanager
  136. def _(path):
  137. """
  138. Degenerate behavior for pathlib.Path objects.
  139. """
  140. yield path
  141. @contextlib.contextmanager
  142. def _temp_path(dir: tempfile.TemporaryDirectory):
  143. """
  144. Wrap tempfile.TemporyDirectory to return a pathlib object.
  145. """
  146. with dir as result:
  147. yield pathlib.Path(result)
  148. @contextlib.contextmanager
  149. def _temp_dir(path):
  150. """
  151. Given a traversable dir, recursively replicate the whole tree
  152. to the file system in a context manager.
  153. """
  154. assert path.is_dir()
  155. with _temp_path(tempfile.TemporaryDirectory()) as temp_dir:
  156. yield _write_contents(temp_dir, path)
  157. def _write_contents(target, source):
  158. child = target.joinpath(source.name)
  159. if source.is_dir():
  160. child.mkdir()
  161. for item in source.iterdir():
  162. _write_contents(child, item)
  163. else:
  164. child.write_bytes(source.read_bytes())
  165. return child