-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Migrate to importlib-metadata #7705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
""" | ||
This package wraps the vendored importlib_metadata and pkg_resources to | ||
provide a (mostly) compatible shim. | ||
|
||
The pkg_resources implementation is used on Python 2, otherwise we use | ||
importlib_metadata. | ||
""" | ||
|
||
import sys | ||
|
||
from pip._internal.utils.typing import MYPY_CHECK_RUNNING | ||
|
||
if sys.version_info >= (3, 5): | ||
from pip._internal.metadata import _importlib_metadata as impl | ||
else: | ||
from pip._internal.metadata import _pkg_resources as impl | ||
|
||
if MYPY_CHECK_RUNNING: | ||
from typing import Union | ||
from pip._internal.metadata import _importlib_metadata as _i | ||
from pip._internal.metadata import _pkg_resources as _p | ||
|
||
Distribution = Union[_i.Distribution, _p.Distribution] | ||
|
||
|
||
get_file_lines = impl.get_file_lines | ||
|
||
get_metadata = impl.get_metadata |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from pip._internal.utils.typing import MYPY_CHECK_RUNNING | ||
|
||
if MYPY_CHECK_RUNNING: | ||
from email.message import Message | ||
from typing import Iterator, Optional | ||
from pip._vendor.importlib_metadata import Distribution | ||
|
||
|
||
def get_metadata(dist): | ||
# type: (Distribution) -> Message | ||
return dist.metadata | ||
|
||
|
||
def get_file_lines(dist, name): | ||
# type: (Distribution, str) -> Optional[Iterator[str]] | ||
content = dist.read_text("INSTALLER") | ||
if not content: | ||
return None | ||
return content.splitlines() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from __future__ import absolute_import | ||
|
||
import logging | ||
from email.parser import FeedParser | ||
|
||
from pip._vendor import pkg_resources | ||
|
||
from pip._internal.exceptions import NoneMetadataError | ||
from pip._internal.utils.misc import display_path | ||
from pip._internal.utils.typing import MYPY_CHECK_RUNNING | ||
|
||
if MYPY_CHECK_RUNNING: | ||
from typing import Iterator, Optional | ||
from email.message import Message | ||
from pip._vendor.pkg_resources import Distribution | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_metadata(dist): | ||
# type: (Distribution) -> Message | ||
""" | ||
:raises NoneMetadataError: if the distribution reports `has_metadata()` | ||
True but `get_metadata()` returns None. | ||
""" | ||
metadata_name = 'METADATA' | ||
if (isinstance(dist, pkg_resources.DistInfoDistribution) and | ||
dist.has_metadata(metadata_name)): | ||
metadata = dist.get_metadata(metadata_name) | ||
elif dist.has_metadata('PKG-INFO'): | ||
metadata_name = 'PKG-INFO' | ||
metadata = dist.get_metadata(metadata_name) | ||
else: | ||
logger.warning("No metadata found in %s", display_path(dist.location)) | ||
metadata = '' | ||
|
||
if metadata is None: | ||
raise NoneMetadataError(dist, metadata_name) | ||
|
||
feed_parser = FeedParser() | ||
# The following line errors out if with a "NoneType" TypeError if | ||
# passed metadata=None. | ||
feed_parser.feed(metadata) | ||
return feed_parser.close() | ||
|
||
|
||
def get_file_lines(dist, name): | ||
# type: (Distribution, str) -> Optional[Iterator[str]] | ||
if not dist.has_metadata(name): | ||
return None | ||
return dist.get_metadata_lines(name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.