Skip to content

Commit

Permalink
Speed up printing of installed package versions (#5127)
Browse files Browse the repository at this point in the history
The installed set of packages is already fixed and no longer changing.
It is therefore safe to reuse a single `pkg_resources.WorkingSet` for
all invocations of `get_installed_version`.
  • Loading branch information
StephanErb authored and pradyunsg committed Jun 19, 2018
1 parent c16ff34 commit cbc21a5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions news/5127.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Speed up printing of newly installed package versions
8 changes: 6 additions & 2 deletions src/pip/_internal/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import shutil
from optparse import SUPPRESS_HELP

from pip._vendor import pkg_resources

from pip._internal import cmdoptions
from pip._internal.basecommand import RequirementCommand
from pip._internal.cache import WheelCache
Expand Down Expand Up @@ -343,20 +345,22 @@ def run(self, options, args):
use_user_site=options.use_user_site,
)

possible_lib_locations = get_lib_location_guesses(
lib_locations = get_lib_location_guesses(
user=options.use_user_site,
home=target_temp_dir.path,
root=options.root_path,
prefix=options.prefix_path,
isolated=options.isolated_mode,
)
working_set = pkg_resources.WorkingSet(lib_locations)

reqs = sorted(installed, key=operator.attrgetter('name'))
items = []
for req in reqs:
item = req.name
try:
installed_version = get_installed_version(
req.name, possible_lib_locations
req.name, working_set=working_set
)
if installed_version:
item += '-' + installed_version
Expand Down
10 changes: 4 additions & 6 deletions src/pip/_internal/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,17 +821,15 @@ def __get__(self, obj, cls):
return value


def get_installed_version(dist_name, lookup_dirs=None):
def get_installed_version(dist_name, working_set=None):
"""Get the installed version of dist_name avoiding pkg_resources cache"""
# Create a requirement that we'll look for inside of setuptools.
req = pkg_resources.Requirement.parse(dist_name)

# We want to avoid having this cached, so we need to construct a new
# working set each time.
if lookup_dirs is None:
if working_set is None:
# We want to avoid having this cached, so we need to construct a new
# working set each time.
working_set = pkg_resources.WorkingSet()
else:
working_set = pkg_resources.WorkingSet(lookup_dirs)

# Get the installed distribution from our working set
dist = working_set.find(req)
Expand Down

0 comments on commit cbc21a5

Please sign in to comment.