Update get_dist to directly use metadata_directory

This commit is contained in:
Pradyun Gedam 2019-09-27 02:22:25 +05:30
parent 26fd8f24b0
commit b01f301c75
No known key found for this signature in database
GPG Key ID: DA17C4B29CB32E4B
2 changed files with 10 additions and 10 deletions

View File

@ -688,16 +688,16 @@ class InstallRequirement(object):
def get_dist(self):
# type: () -> Distribution
"""Return a pkg_resources.Distribution for this requirement"""
if self.metadata_directory:
dist_dir = self.metadata_directory
dist_cls = pkg_resources.DistInfoDistribution
else:
dist_dir = self.egg_info_path.rstrip(os.path.sep)
# https://github.com/python/mypy/issues/1174
dist_cls = pkg_resources.Distribution # type: ignore
dist_dir = self.metadata_directory.rstrip(os.sep)
# dist_dir_name can be of the form "<project>.dist-info" or
# e.g. "<project>.egg-info".
# Determine the correct Distribution object type.
if dist_dir.endswith(".egg-info"):
dist_cls = pkg_resources.Distribution
else:
assert dist_dir.endswith(".dist-info")
dist_cls = pkg_resources.DistInfoDistribution
# Build a PathMetadata object, from path to metadata. :wink:
base_dir, dist_dir_name = os.path.split(dist_dir)
dist_name = os.path.splitext(dist_dir_name)[0]
metadata = pkg_resources.PathMetadata(base_dir, dist_dir)

View File

@ -438,7 +438,7 @@ class TestInstallRequirement(object):
))
def test_get_dist(self, path):
req = install_req_from_line('foo')
req._egg_info_path = path
req.metadata_directory = path
dist = req.get_dist()
assert isinstance(dist, pkg_resources.Distribution)
assert dist.project_name == 'foo'