m2m模型翻译
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.

119 lines
4.6 KiB

6 months ago
  1. import logging
  2. from collections import OrderedDict
  3. from typing import Dict, List
  4. from pip._vendor.packaging.specifiers import LegacySpecifier
  5. from pip._vendor.packaging.utils import canonicalize_name
  6. from pip._vendor.packaging.version import LegacyVersion
  7. from pip._internal.req.req_install import InstallRequirement
  8. from pip._internal.utils.deprecation import deprecated
  9. logger = logging.getLogger(__name__)
  10. class RequirementSet:
  11. def __init__(self, check_supported_wheels: bool = True) -> None:
  12. """Create a RequirementSet."""
  13. self.requirements: Dict[str, InstallRequirement] = OrderedDict()
  14. self.check_supported_wheels = check_supported_wheels
  15. self.unnamed_requirements: List[InstallRequirement] = []
  16. def __str__(self) -> str:
  17. requirements = sorted(
  18. (req for req in self.requirements.values() if not req.comes_from),
  19. key=lambda req: canonicalize_name(req.name or ""),
  20. )
  21. return " ".join(str(req.req) for req in requirements)
  22. def __repr__(self) -> str:
  23. requirements = sorted(
  24. self.requirements.values(),
  25. key=lambda req: canonicalize_name(req.name or ""),
  26. )
  27. format_string = "<{classname} object; {count} requirement(s): {reqs}>"
  28. return format_string.format(
  29. classname=self.__class__.__name__,
  30. count=len(requirements),
  31. reqs=", ".join(str(req.req) for req in requirements),
  32. )
  33. def add_unnamed_requirement(self, install_req: InstallRequirement) -> None:
  34. assert not install_req.name
  35. self.unnamed_requirements.append(install_req)
  36. def add_named_requirement(self, install_req: InstallRequirement) -> None:
  37. assert install_req.name
  38. project_name = canonicalize_name(install_req.name)
  39. self.requirements[project_name] = install_req
  40. def has_requirement(self, name: str) -> bool:
  41. project_name = canonicalize_name(name)
  42. return (
  43. project_name in self.requirements
  44. and not self.requirements[project_name].constraint
  45. )
  46. def get_requirement(self, name: str) -> InstallRequirement:
  47. project_name = canonicalize_name(name)
  48. if project_name in self.requirements:
  49. return self.requirements[project_name]
  50. raise KeyError(f"No project with the name {name!r}")
  51. @property
  52. def all_requirements(self) -> List[InstallRequirement]:
  53. return self.unnamed_requirements + list(self.requirements.values())
  54. @property
  55. def requirements_to_install(self) -> List[InstallRequirement]:
  56. """Return the list of requirements that need to be installed.
  57. TODO remove this property together with the legacy resolver, since the new
  58. resolver only returns requirements that need to be installed.
  59. """
  60. return [
  61. install_req
  62. for install_req in self.all_requirements
  63. if not install_req.constraint and not install_req.satisfied_by
  64. ]
  65. def warn_legacy_versions_and_specifiers(self) -> None:
  66. for req in self.requirements_to_install:
  67. version = req.get_dist().version
  68. if isinstance(version, LegacyVersion):
  69. deprecated(
  70. reason=(
  71. f"pip has selected the non standard version {version} "
  72. f"of {req}. In the future this version will be "
  73. f"ignored as it isn't standard compliant."
  74. ),
  75. replacement=(
  76. "set or update constraints to select another version "
  77. "or contact the package author to fix the version number"
  78. ),
  79. issue=12063,
  80. gone_in="24.1",
  81. )
  82. for dep in req.get_dist().iter_dependencies():
  83. if any(isinstance(spec, LegacySpecifier) for spec in dep.specifier):
  84. deprecated(
  85. reason=(
  86. f"pip has selected {req} {version} which has non "
  87. f"standard dependency specifier {dep}. "
  88. f"In the future this version of {req} will be "
  89. f"ignored as it isn't standard compliant."
  90. ),
  91. replacement=(
  92. "set or update constraints to select another version "
  93. "or contact the package author to fix the version number"
  94. ),
  95. issue=12063,
  96. gone_in="24.1",
  97. )