comparison lib/python3.8/site-packages/pip/_internal/utils/packaging.py @ 1:64071f2a4cf0 draft default tip

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