From 8e7fd39cea577aaeb5c343146d522d9b21e8d3c5 Mon Sep 17 00:00:00 2001 From: Seth House Date: Fri, 23 Dec 2011 16:06:25 -0700 Subject: [PATCH] Abstracted parsing yum output --- salt/exceptions.py | 7 +++++++ salt/modules/yumpkg5.py | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/salt/exceptions.py b/salt/exceptions.py index 1e5e42ddcc..2c6ac295af 100644 --- a/salt/exceptions.py +++ b/salt/exceptions.py @@ -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 diff --git a/salt/modules/yumpkg5.py b/salt/modules/yumpkg5.py index 37f592dbff..4fecbffd1c 100644 --- a/salt/modules/yumpkg5.py +++ b/salt/modules/yumpkg5.py @@ -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