_common.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 == __file__
  75. def is_wrapper(frame_info):
  76. return frame_info.function == 'wrapper'
  77. not_this_file = itertools.filterfalse(is_this_file, inspect.stack())
  78. # also exclude 'wrapper' due to singledispatch in the call stack
  79. callers = itertools.filterfalse(is_wrapper, not_this_file)
  80. return next(callers).frame
  81. def from_package(package: types.ModuleType):
  82. """
  83. Return a Traversable object for the given package.
  84. """
  85. spec = wrap_spec(package)
  86. reader = spec.loader.get_resource_reader(spec.name)
  87. return reader.files()
  88. @contextlib.contextmanager
  89. def _tempfile(
  90. reader,
  91. suffix='',
  92. # gh-93353: Keep a reference to call os.remove() in late Python
  93. # finalization.
  94. *,
  95. _os_remove=os.remove,
  96. ):
  97. # Not using tempfile.NamedTemporaryFile as it leads to deeper 'try'
  98. # blocks due to the need to close the temporary file to work on Windows
  99. # properly.
  100. fd, raw_path = tempfile.mkstemp(suffix=suffix)
  101. try:
  102. try:
  103. os.write(fd, reader())
  104. finally:
  105. os.close(fd)
  106. del reader
  107. yield pathlib.Path(raw_path)
  108. finally:
  109. try:
  110. _os_remove(raw_path)
  111. except FileNotFoundError:
  112. pass
  113. def _temp_file(path):
  114. return _tempfile(path.read_bytes, suffix=path.name)
  115. def _is_present_dir(path: Traversable) -> bool:
  116. """
  117. Some Traversables implement ``is_dir()`` to raise an
  118. exception (i.e. ``FileNotFoundError``) when the
  119. directory doesn't exist. This function wraps that call
  120. to always return a boolean and only return True
  121. if there's a dir and it exists.
  122. """
  123. with contextlib.suppress(FileNotFoundError):
  124. return path.is_dir()
  125. return False
  126. @functools.singledispatch
  127. def as_file(path):
  128. """
  129. Given a Traversable object, return that object as a
  130. path on the local file system in a context manager.
  131. """
  132. return _temp_dir(path) if _is_present_dir(path) else _temp_file(path)
  133. @as_file.register(pathlib.Path)
  134. @contextlib.contextmanager
  135. def _(path):
  136. """
  137. Degenerate behavior for pathlib.Path objects.
  138. """
  139. yield path
  140. @contextlib.contextmanager
  141. def _temp_path(dir: tempfile.TemporaryDirectory):
  142. """
  143. Wrap tempfile.TemporyDirectory to return a pathlib object.
  144. """
  145. with dir as result:
  146. yield pathlib.Path(result)
  147. @contextlib.contextmanager
  148. def _temp_dir(path):
  149. """
  150. Given a traversable dir, recursively replicate the whole tree
  151. to the file system in a context manager.
  152. """
  153. assert path.is_dir()
  154. with _temp_path(tempfile.TemporaryDirectory()) as temp_dir:
  155. yield _write_contents(temp_dir, path)
  156. def _write_contents(target, source):
  157. child = target.joinpath(source.name)
  158. if source.is_dir():
  159. child.mkdir()
  160. for item in source.iterdir():
  161. _write_contents(child, item)
  162. else:
  163. child.write_bytes(source.read_bytes())
  164. return child