mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #47734 from OrlandoArcapix/Issue47689-pip-state-performance
#47689 improve run-speed of pip package state
This commit is contained in:
commit
c8e69431ff
2 changed files with 32 additions and 10 deletions
|
@ -186,16 +186,31 @@ 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):
|
||||
# result: None means the command failed to run
|
||||
# result: True means the package is installed
|
||||
# result: False means the package is not installed
|
||||
pip_list=False, **kwargs):
|
||||
'''
|
||||
Takes a package name and version specification (if any) and checks it is
|
||||
installed
|
||||
|
||||
Keyword arguments include:
|
||||
pip_list: optional dict of installed pip packages, and their versions,
|
||||
to search through to check if the package is installed. If not
|
||||
provided, one will be generated in this function by querying the
|
||||
system.
|
||||
|
||||
Returns:
|
||||
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
|
||||
|
@ -716,6 +731,14 @@ 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 Exception as exc:
|
||||
logger.exception(exc)
|
||||
pip_list = False
|
||||
|
||||
for prefix, state_pkg_name, version_spec in pkgs_details:
|
||||
|
||||
if prefix:
|
||||
|
@ -724,7 +747,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:
|
||||
|
|
|
@ -586,8 +586,7 @@ class PipStateTest(ModuleCase, SaltReturnAssertsMixin):
|
|||
continue
|
||||
self.assertEqual(
|
||||
ret[key]['comment'],
|
||||
('Python package carbon < 1.3 was already installed\n'
|
||||
'All specified packages are already installed'))
|
||||
('All packages were successfully installed'))
|
||||
break
|
||||
else:
|
||||
raise Exception('Expected state did not run')
|
||||
|
|
Loading…
Add table
Reference in a new issue