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.

packaging.py 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from __future__ import absolute_import
  2. import logging
  3. from email.parser import FeedParser
  4. from pip._vendor import pkg_resources
  5. from pip._vendor.packaging import specifiers, version
  6. from pip._internal.exceptions import NoneMetadataError
  7. from pip._internal.utils.misc import display_path
  8. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  9. if MYPY_CHECK_RUNNING:
  10. from typing import Optional, Tuple
  11. from email.message import Message
  12. from pip._vendor.pkg_resources import Distribution
  13. logger = logging.getLogger(__name__)
  14. def check_requires_python(requires_python, version_info):
  15. # type: (Optional[str], Tuple[int, ...]) -> bool
  16. """
  17. Check if the given Python version matches a "Requires-Python" specifier.
  18. :param version_info: A 3-tuple of ints representing a Python
  19. major-minor-micro version to check (e.g. `sys.version_info[:3]`).
  20. :return: `True` if the given Python version satisfies the requirement.
  21. Otherwise, return `False`.
  22. :raises InvalidSpecifier: If `requires_python` has an invalid format.
  23. """
  24. if requires_python is None:
  25. # The package provides no information
  26. return True
  27. requires_python_specifier = specifiers.SpecifierSet(requires_python)
  28. python_version = version.parse('.'.join(map(str, version_info)))
  29. return python_version in requires_python_specifier
  30. def get_metadata(dist):
  31. # type: (Distribution) -> Message
  32. """
  33. :raises NoneMetadataError: if the distribution reports `has_metadata()`
  34. True but `get_metadata()` returns None.
  35. """
  36. metadata_name = 'METADATA'
  37. if (isinstance(dist, pkg_resources.DistInfoDistribution) and
  38. dist.has_metadata(metadata_name)):
  39. metadata = dist.get_metadata(metadata_name)
  40. elif dist.has_metadata('PKG-INFO'):
  41. metadata_name = 'PKG-INFO'
  42. metadata = dist.get_metadata(metadata_name)
  43. else:
  44. logger.warning("No metadata found in %s", display_path(dist.location))
  45. metadata = ''
  46. if metadata is None:
  47. raise NoneMetadataError(dist, metadata_name)
  48. feed_parser = FeedParser()
  49. # The following line errors out if with a "NoneType" TypeError if
  50. # passed metadata=None.
  51. feed_parser.feed(metadata)
  52. return feed_parser.close()
  53. def get_requires_python(dist):
  54. # type: (pkg_resources.Distribution) -> Optional[str]
  55. """
  56. Return the "Requires-Python" metadata for a distribution, or None
  57. if not present.
  58. """
  59. pkg_info_dict = get_metadata(dist)
  60. requires_python = pkg_info_dict.get('Requires-Python')
  61. if requires_python is not None:
  62. # Convert to a str to satisfy the type checker, since requires_python
  63. # can be a Header object.
  64. requires_python = str(requires_python)
  65. return requires_python
  66. def get_installer(dist):
  67. # type: (Distribution) -> str
  68. if dist.has_metadata('INSTALLER'):
  69. for line in dist.get_metadata_lines('INSTALLER'):
  70. if line.strip():
  71. return line.strip()
  72. return ''