Add changes suggested by contributer

This commit is contained in:
twangboy 2017-03-22 12:46:22 -06:00
parent 07d374a73e
commit ba9622945e
2 changed files with 61 additions and 19 deletions

View File

@ -39,6 +39,7 @@ import logging
import os
import re
import time
from functools import cmp_to_key
# Import third party libs
import salt.ext.six as six
@ -53,6 +54,7 @@ import salt.utils
import salt.syspaths
import salt.payload
from salt.exceptions import MinionError
from salt.utils.versions import LooseVersion
log = logging.getLogger(__name__)
@ -106,21 +108,24 @@ def latest_version(*names, **kwargs):
saltenv = kwargs.get('saltenv', 'base')
# Refresh before looking for the latest version available
refresh = salt.utils.is_true(kwargs.get('refresh', True))
# no need to call _refresh_db_conditional as list_pkgs will do it
installed_pkgs = list_pkgs(versions_as_list=True, saltenv=saltenv, refresh=refresh)
# no need to call _refresh_db_conditional as list_pkgs will do it
installed_pkgs = list_pkgs(
versions_as_list=True, saltenv=saltenv, refresh=refresh)
log.trace('List of installed packages: {0}'.format(installed_pkgs))
# iterate over all requested package names
for name in names:
latest_installed = '0'
latest_available = '0'
# get latest installed version of package
if name in installed_pkgs:
log.trace('Determining latest installed version of %s', name)
try:
latest_installed = sorted(installed_pkgs[name]).pop()
latest_installed = sorted(
installed_pkgs[name],
key=cmp_to_key(_reverse_cmp_pkg_versions)
).pop()
except IndexError:
log.warning(
'%s was empty in pkg.list_pkgs return data, this is '
@ -156,12 +161,14 @@ def latest_version(*names, **kwargs):
return ret
def upgrade_available(name, refresh=True, saltenv='base'):
def upgrade_available(name, **kwargs):
'''
Check whether or not an upgrade is available for a given package
Args:
name (str): The name of a single package
Kwargs:
refresh (bool): Refresh package metadata. Default ``True``
saltenv (str): The salt environment. Default ``base``
@ -174,9 +181,10 @@ def upgrade_available(name, refresh=True, saltenv='base'):
salt '*' pkg.upgrade_available <package name>
'''
saltenv = kwargs.get('saltenv', 'base')
# Refresh before looking for the latest version available,
# same default as latest_version
refresh = salt.utils.is_true(refresh)
refresh = salt.utils.is_true(kwargs.get('refresh', True))
current = version(name, saltenv=saltenv, refresh=refresh).get(name)
latest = latest_version(name, saltenv=saltenv, refresh=False)
@ -184,12 +192,14 @@ def upgrade_available(name, refresh=True, saltenv='base'):
return compare_versions(latest, '>', current)
def list_upgrades(refresh=True, saltenv='base'):
def list_upgrades(refresh=True, **kwargs):
'''
List all available package upgrades on this system
Args:
refresh (bool): Refresh package metadata. Default ``True``
Kwargs:
saltenv (str): Salt environment. Default ``base``
Returns:
@ -201,15 +211,16 @@ def list_upgrades(refresh=True, saltenv='base'):
salt '*' pkg.list_upgrades
'''
saltenv = kwargs.get('saltenv', 'base')
refresh = salt.utils.is_true(refresh)
_refresh_db_conditional(saltenv, force=refresh)
installed_pkgs = list_pkgs(refresh=False)
installed_pkgs = list_pkgs(refresh=False, saltenv=saltenv)
available_pkgs = get_repo_data(saltenv).get('repo')
pkgs = {}
for pkg in installed_pkgs:
if pkg in available_pkgs:
latest_ver = latest_version(pkg, refresh=False)
latest_ver = latest_version(pkg, refresh=False, saltenv=saltenv)
install_ver = installed_pkgs[pkg]
if compare_versions(latest_ver, '>', install_ver):
pkgs[pkg] = latest_ver
@ -251,22 +262,29 @@ def list_available(*names, **kwargs):
return ''
saltenv = kwargs.get('saltenv', 'base')
refresh = salt.utils.is_true(kwargs.get('refresh', False))
_refresh_db_conditional(saltenv, force=refresh)
refresh = salt.utils.is_true(kwargs.get('refresh', True))
return_dict_always = \
salt.utils.is_true(kwargs.get('return_dict_always', False))
_refresh_db_conditional(saltenv, force=refresh)
if len(names) == 1 and not return_dict_always:
pkginfo = _get_package_info(names[0], saltenv=saltenv)
if not pkginfo:
return ''
versions = sorted(list(pkginfo.keys()))
versions = sorted(
list(pkginfo.keys()),
key=cmp_to_key(_reverse_cmp_pkg_versions)
)
else:
versions = {}
for name in names:
pkginfo = _get_package_info(name, saltenv=saltenv)
if not pkginfo:
continue
verlist = sorted(list(pkginfo.keys())) if pkginfo else []
verlist = sorted(
list(pkginfo.keys()) if pkginfo else [],
key=cmp_to_key(_reverse_cmp_pkg_versions)
)
versions[name] = verlist
return versions
@ -448,6 +466,17 @@ def _refresh_db_conditional(saltenv, **kwargs):
Args:
saltenv (str): Salt environment
Kwargs:
force (bool):
Force a refresh if the minimum age has been reached. Default is
False.
failhard (bool):
If ``True``, an error will be raised if any repo SLS files failed to
process. If ``False``, no error will be raised, and a dictionary
containing the full results will be returned.
Returns:
bool: True Fetched or Cache uptodate, False to indicate an issue
@ -564,7 +593,7 @@ def refresh_db(**kwargs):
)
# Cache repo-ng locally
cached_files = __salt__['cp.cache_dir'](
__salt__['cp.cache_dir'](
repo_details.winrepo_source_dir,
saltenv,
include_pat='*.sls'
@ -600,7 +629,7 @@ def _get_repo_details(saltenv):
)
else:
log.error(
'minion cofiguration option \'winrepo_cachefile\' has been '
'minion configuration option \'winrepo_cachefile\' has been '
'ignored as its value (%s) is invalid. Please ensure this '
'option is set to a valid filename.',
__opts__['winrepo_cachefile']
@ -707,7 +736,7 @@ def genrepo(**kwargs):
successful_verbose
)
serial = salt.payload.Serial(__opts__)
mode = 'wb+' if six.PY3 else 'w+'
mode = 'w+' if six.PY2 else 'wb+'
with salt.utils.fopen(repo_details.winrepo_file, mode) as repo_cache:
repo_cache.write(serial.dumps(ret))
# save reading it back again. ! this breaks due to utf8 issues
@ -1032,7 +1061,7 @@ def install(name=None, refresh=False, pkgs=None, **kwargs):
# Check if the version is already installed
if version_num in old.get(pkg_name, '').split(',') \
or (pkg_name in old and old.get(pkg_name) is None):
or (old.get(pkg_name) == 'Not Found'):
# Desired version number already installed
ret[pkg_name] = {'current': version_num}
continue
@ -1594,7 +1623,7 @@ def _get_name_map(saltenv='base'):
u_name_map = {}
name_map = get_repo_data(saltenv).get('name_map', {})
if six.PY3:
if not six.PY2:
return name_map
for k in name_map:
@ -1610,11 +1639,21 @@ def _get_package_info(name, saltenv='base'):
return get_repo_data(saltenv).get('repo', {}).get(name, {})
def _reverse_cmp_pkg_versions(pkg1, pkg2):
'''
Compare software package versions
'''
return 1 if LooseVersion(pkg1) > LooseVersion(pkg2) else -1
def _get_latest_pkg_version(pkginfo):
if len(pkginfo) == 1:
return next(six.iterkeys(pkginfo))
try:
return sorted(list(pkginfo.keys())).pop()
return sorted(
pkginfo,
key=cmp_to_key(_reverse_cmp_pkg_versions)
).pop()
except IndexError:
return ''

View File

@ -115,6 +115,7 @@ if salt.utils.is_windows():
from salt.modules.win_pkg import genrepo
from salt.modules.win_pkg import _repo_process_pkg_sls
from salt.modules.win_pkg import _get_latest_pkg_version
from salt.modules.win_pkg import _reverse_cmp_pkg_versions
_get_package_info = _namespaced_function(_get_package_info, globals())
get_repo_data = _namespaced_function(get_repo_data, globals())
_get_repo_details = \
@ -127,6 +128,8 @@ if salt.utils.is_windows():
_namespaced_function(_repo_process_pkg_sls, globals())
_get_latest_pkg_version = \
_namespaced_function(_get_latest_pkg_version, globals())
_reverse_cmp_pkg_versions = \
_namespaced_function(_reverse_cmp_pkg_versions, globals())
# The following imports are used by the namespaced win_pkg funcs
# and need to be included in their globals.
# pylint: disable=import-error,unused-import