Skip to content

Commit

Permalink
#47698 improve run-speed of pip package state checks by only loading …
Browse files Browse the repository at this point in the history
…the current package list once when checking multiple packages
  • Loading branch information
OrlandoArcapix committed May 18, 2018
1 parent 00a1376 commit 4fec8f6
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions salt/states/pip_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,19 @@ def _check_pkg_version_format(pkg):

def _check_if_installed(prefix, state_pkg_name, version_spec, ignore_installed,
force_reinstall, upgrade, user, cwd, bin_env, env_vars,
**kwargs):
pip_list=False,**kwargs):
# result: None means the command failed to run
# result: True means the package is installed
# result: False means the package is not installed
ret = {'result': False, 'comment': None}

# If we are not passed a pip list, get one:
if not pip_list:
pip_list = __salt__['pip.list'](prefix, bin_env=bin_env,
user=user, cwd=cwd,
env_vars=env_vars, **kwargs)

# Check if the requested package is already installed.
pip_list = __salt__['pip.list'](prefix, bin_env=bin_env,
user=user, cwd=cwd,
env_vars=env_vars, **kwargs)
prefix_realname = _find_key(prefix, pip_list)

# If the package was already installed, check
Expand Down Expand Up @@ -716,6 +719,13 @@ def installed(name,
# No requirements case.
# Check pre-existence of the requested packages.
else:
# Attempt to pre-cache a the current pip list
try:
pip_list = __salt__['pip.list'](bin_env=bin_env, user=user, cwd=cwd)
# If we fail, then just send False, and we'll try again in the next function call
except:
pip_list = False

for prefix, state_pkg_name, version_spec in pkgs_details:

if prefix:
Expand All @@ -724,7 +734,7 @@ def installed(name,
out = _check_if_installed(prefix, state_pkg_name, version_spec,
ignore_installed, force_reinstall,
upgrade, user, cwd, bin_env, env_vars,
**kwargs)
pip_list,**kwargs)
# If _check_if_installed result is None, something went wrong with
# the command running. This way we keep stateful output.
if out['result'] is None:
Expand Down

0 comments on commit 4fec8f6

Please sign in to comment.