Development of an internal social media platform with personalised dashboards for students
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 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from __future__ import absolute_import
  2. import logging
  3. import sys
  4. from email.parser import FeedParser # type: ignore
  5. from pip._vendor import pkg_resources
  6. from pip._vendor.packaging import specifiers, version
  7. from pip._internal import exceptions
  8. from pip._internal.utils.misc import display_path
  9. logger = logging.getLogger(__name__)
  10. def check_requires_python(requires_python):
  11. """
  12. Check if the python version in use match the `requires_python` specifier.
  13. Returns `True` if the version of python in use matches the requirement.
  14. Returns `False` if the version of python in use does not matches the
  15. requirement.
  16. Raises an InvalidSpecifier if `requires_python` have an invalid format.
  17. """
  18. if requires_python is None:
  19. # The package provides no information
  20. return True
  21. requires_python_specifier = specifiers.SpecifierSet(requires_python)
  22. # We only use major.minor.micro
  23. python_version = version.parse('.'.join(map(str, sys.version_info[:3])))
  24. return python_version in requires_python_specifier
  25. def get_metadata(dist):
  26. if (isinstance(dist, pkg_resources.DistInfoDistribution) and
  27. dist.has_metadata('METADATA')):
  28. metadata = dist.get_metadata('METADATA')
  29. elif dist.has_metadata('PKG-INFO'):
  30. metadata = dist.get_metadata('PKG-INFO')
  31. else:
  32. logger.warning("No metadata found in %s", display_path(dist.location))
  33. metadata = ''
  34. feed_parser = FeedParser()
  35. feed_parser.feed(metadata)
  36. return feed_parser.close()
  37. def check_dist_requires_python(dist):
  38. pkg_info_dict = get_metadata(dist)
  39. requires_python = pkg_info_dict.get('Requires-Python')
  40. try:
  41. if not check_requires_python(requires_python):
  42. raise exceptions.UnsupportedPythonVersion(
  43. "%s requires Python '%s' but the running Python is %s" % (
  44. dist.project_name,
  45. requires_python,
  46. '.'.join(map(str, sys.version_info[:3])),)
  47. )
  48. except specifiers.InvalidSpecifier as e:
  49. logger.warning(
  50. "Package %s has an invalid Requires-Python entry %s - %s",
  51. dist.project_name, requires_python, e,
  52. )
  53. return
  54. def get_installer(dist):
  55. if dist.has_metadata('INSTALLER'):
  56. for line in dist.get_metadata_lines('INSTALLER'):
  57. if line.strip():
  58. return line.strip()
  59. return ''