图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.2 KiB

  1. import contextlib
  2. import functools
  3. import os
  4. import sys
  5. from typing import TYPE_CHECKING, List, Optional, Type, cast
  6. from pip._internal.utils.misc import strtobool
  7. from .base import BaseDistribution, BaseEnvironment, FilesystemWheel, MemoryWheel, Wheel
  8. if TYPE_CHECKING:
  9. from typing import Literal, Protocol
  10. else:
  11. Protocol = object
  12. __all__ = [
  13. "BaseDistribution",
  14. "BaseEnvironment",
  15. "FilesystemWheel",
  16. "MemoryWheel",
  17. "Wheel",
  18. "get_default_environment",
  19. "get_environment",
  20. "get_wheel_distribution",
  21. "select_backend",
  22. ]
  23. def _should_use_importlib_metadata() -> bool:
  24. """Whether to use the ``importlib.metadata`` or ``pkg_resources`` backend.
  25. By default, pip uses ``importlib.metadata`` on Python 3.11+, and
  26. ``pkg_resourcess`` otherwise. This can be overridden by a couple of ways:
  27. * If environment variable ``_PIP_USE_IMPORTLIB_METADATA`` is set, it
  28. dictates whether ``importlib.metadata`` is used, regardless of Python
  29. version.
  30. * On Python 3.11+, Python distributors can patch ``importlib.metadata``
  31. to add a global constant ``_PIP_USE_IMPORTLIB_METADATA = False``. This
  32. makes pip use ``pkg_resources`` (unless the user set the aforementioned
  33. environment variable to *True*).
  34. """
  35. with contextlib.suppress(KeyError, ValueError):
  36. return bool(strtobool(os.environ["_PIP_USE_IMPORTLIB_METADATA"]))
  37. if sys.version_info < (3, 11):
  38. return False
  39. import importlib.metadata
  40. return bool(getattr(importlib.metadata, "_PIP_USE_IMPORTLIB_METADATA", True))
  41. class Backend(Protocol):
  42. NAME: 'Literal["importlib", "pkg_resources"]'
  43. Distribution: Type[BaseDistribution]
  44. Environment: Type[BaseEnvironment]
  45. @functools.lru_cache(maxsize=None)
  46. def select_backend() -> Backend:
  47. if _should_use_importlib_metadata():
  48. from . import importlib
  49. return cast(Backend, importlib)
  50. from . import pkg_resources
  51. return cast(Backend, pkg_resources)
  52. def get_default_environment() -> BaseEnvironment:
  53. """Get the default representation for the current environment.
  54. This returns an Environment instance from the chosen backend. The default
  55. Environment instance should be built from ``sys.path`` and may use caching
  56. to share instance state accorss calls.
  57. """
  58. return select_backend().Environment.default()
  59. def get_environment(paths: Optional[List[str]]) -> BaseEnvironment:
  60. """Get a representation of the environment specified by ``paths``.
  61. This returns an Environment instance from the chosen backend based on the
  62. given import paths. The backend must build a fresh instance representing
  63. the state of installed distributions when this function is called.
  64. """
  65. return select_backend().Environment.from_paths(paths)
  66. def get_directory_distribution(directory: str) -> BaseDistribution:
  67. """Get the distribution metadata representation in the specified directory.
  68. This returns a Distribution instance from the chosen backend based on
  69. the given on-disk ``.dist-info`` directory.
  70. """
  71. return select_backend().Distribution.from_directory(directory)
  72. def get_wheel_distribution(wheel: Wheel, canonical_name: str) -> BaseDistribution:
  73. """Get the representation of the specified wheel's distribution metadata.
  74. This returns a Distribution instance from the chosen backend based on
  75. the given wheel's ``.dist-info`` directory.
  76. :param canonical_name: Normalized project name of the given wheel.
  77. """
  78. return select_backend().Distribution.from_wheel(wheel, canonical_name)
  79. def get_metadata_distribution(
  80. metadata_contents: bytes,
  81. filename: str,
  82. canonical_name: str,
  83. ) -> BaseDistribution:
  84. """Get the dist representation of the specified METADATA file contents.
  85. This returns a Distribution instance from the chosen backend sourced from the data
  86. in `metadata_contents`.
  87. :param metadata_contents: Contents of a METADATA file within a dist, or one served
  88. via PEP 658.
  89. :param filename: Filename for the dist this metadata represents.
  90. :param canonical_name: Normalized project name of the given dist.
  91. """
  92. return select_backend().Distribution.from_metadata_file_contents(
  93. metadata_contents,
  94. filename,
  95. canonical_name,
  96. )