Skip to content

Commit 554207e

Browse files
authored
Merge pull request #7180 from chrahunt/refactor/separate-get-dist-function
Factor get_dist implementation out of InstallRequirement
2 parents 2e9f89e + d25dc68 commit 554207e

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

src/pip/_internal/req/req_install.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,32 @@
7373
logger = logging.getLogger(__name__)
7474

7575

76+
def _get_dist(metadata_directory):
77+
# type: (str) -> Distribution
78+
"""Return a pkg_resources.Distribution for the provided
79+
metadata directory.
80+
"""
81+
dist_dir = metadata_directory.rstrip(os.sep)
82+
83+
# Determine the correct Distribution object type.
84+
if dist_dir.endswith(".egg-info"):
85+
dist_cls = pkg_resources.Distribution
86+
else:
87+
assert dist_dir.endswith(".dist-info")
88+
dist_cls = pkg_resources.DistInfoDistribution
89+
90+
# Build a PathMetadata object, from path to metadata. :wink:
91+
base_dir, dist_dir_name = os.path.split(dist_dir)
92+
dist_name = os.path.splitext(dist_dir_name)[0]
93+
metadata = pkg_resources.PathMetadata(base_dir, dist_dir)
94+
95+
return dist_cls(
96+
base_dir,
97+
project_name=dist_name,
98+
metadata=metadata,
99+
)
100+
101+
76102
class InstallRequirement(object):
77103
"""
78104
Represents something that may be installed later on, may have information
@@ -619,26 +645,7 @@ def metadata(self):
619645

620646
def get_dist(self):
621647
# type: () -> Distribution
622-
"""Return a pkg_resources.Distribution for this requirement"""
623-
dist_dir = self.metadata_directory.rstrip(os.sep)
624-
625-
# Determine the correct Distribution object type.
626-
if dist_dir.endswith(".egg-info"):
627-
dist_cls = pkg_resources.Distribution
628-
else:
629-
assert dist_dir.endswith(".dist-info")
630-
dist_cls = pkg_resources.DistInfoDistribution
631-
632-
# Build a PathMetadata object, from path to metadata. :wink:
633-
base_dir, dist_dir_name = os.path.split(dist_dir)
634-
dist_name = os.path.splitext(dist_dir_name)[0]
635-
metadata = pkg_resources.PathMetadata(base_dir, dist_dir)
636-
637-
return dist_cls(
638-
base_dir,
639-
project_name=dist_name,
640-
metadata=metadata,
641-
)
648+
return _get_dist(self.metadata_directory)
642649

643650
def assert_source_matches_version(self):
644651
# type: () -> None

0 commit comments

Comments
 (0)