Merge pull request #430 from whiteinge/fix-el5pkgname

Fix el5pkgname
This commit is contained in:
Thomas S Hatch 2012-01-04 15:39:42 -08:00
commit f7125e8682
2 changed files with 37 additions and 23 deletions

View File

@ -44,3 +44,10 @@ class SaltInvocationError(SaltException):
or invalid arguments are specified on the command line
'''
pass
class PkgParseError(SaltException):
'''
Used when of the pkg modules cannot correctly parse the output from the CLI
tool (pacman, yum, apt, aptitude, etc)
'''
pass

View File

@ -1,6 +1,13 @@
'''
Support for YUM
'''
import logging
from collections import namedtuple
from salt.exceptions import PkgParseError
logger = logging.getLogger(__name__)
def __virtual__():
'''
Confine this module to yum based systems
@ -21,6 +28,25 @@ def __virtual__():
return False
def _parse_yum(arg):
'''
A small helper to parse yum output; returns a list of namedtuples
'''
cmd = 'yum -q {0}'.format(arg)
out = __salt__['cmd.run_stdout'](cmd)
YumOut = namedtuple('YumOut', ('name', 'version', 'status'))
try:
results = map(YumOut._make,
[i.split() for i in out.split('\n') if len(i.split()) == 3])
except TypeError as exc:
results = ()
msg = "Could not parse yum output for: {0}".format(cmd)
logger.debug(msg, exc_info=exc)
return results
def _list_removed(old, new):
'''
List the packages which have been removed between the two package objects
@ -40,19 +66,8 @@ def available_version(name):
salt '*' pkg.available_version <package name>
'''
out = __salt__['cmd.run_stdout']('yum list {0} -q'.format(name))
for line in out.split('\n'):
if not line.strip():
continue
# Iterate through the output
comps = line.split()
if comps[0].split('.')[0] == name:
if len(comps) < 2:
continue
# found it!
return comps[1][:comps[1].rindex('.')]
# Package not available
return ''
out = _parse_yum('list {0}'.format(name))
return out[0].version if out else ''
def version(name):
@ -80,16 +95,8 @@ def list_pkgs():
salt '*' pkg.list_pkgs
'''
cmd = "rpm -qa --qf '%{NAME}:%{VERSION}-%{RELEASE};'"
ret = {}
out = __salt__['cmd.run_stdout'](cmd)
for line in out.split(';'):
if ':' not in line:
continue
comps = line.split(':')
ret[comps[0]] = comps[1]
return ret
out = _parse_yum('list installed')
return dict([(i.name, i.version) for i in out])
def refresh_db():
'''