[node-patches] Change in ovirt-node[node-3.0]: utils: Better errors for NVR parsing

fabiand at fedoraproject.org fabiand at fedoraproject.org
Wed Feb 19 14:15:46 UTC 2014


Fabian Deutsch has uploaded a new change for review.

Change subject: utils: Better errors for NVR parsing
......................................................................

utils: Better errors for NVR parsing

Change-Id: I280f18235374da5604747499f2491669d75b9970
Signed-off-by: Fabian Deutsch <fabiand at redhat.com>
---
M src/ovirt/node/utils/system.py
1 file changed, 73 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/75/24775/1

diff --git a/src/ovirt/node/utils/system.py b/src/ovirt/node/utils/system.py
index 29e08d5..c5d288d 100644
--- a/src/ovirt/node/utils/system.py
+++ b/src/ovirt/node/utils/system.py
@@ -157,6 +157,79 @@
                                                                       f))
 
 
+class NVR(object):
+    """Simple clas to parse and compare NVRs
+
+    >>> nvr = NVR.parse("ovirt-node-1.2.3-4.el6")
+    >>> nvr.name
+    'ovirt-node'
+    >>> nvr.version
+    '1.2.3'
+    >>> nvr.release
+    '4.el6'
+
+    >>> nvr_new = NVR.parse("ovirt-node-1.2.3-5.el6")
+    >>> nvr < nvr_new
+    True
+
+    >>> nvr_new = NVR.parse("ovirt-node-2.2.3-4.el6")
+    >>> nvr < nvr_new
+    True
+    """
+    name = None
+    version = None
+    release = None
+
+    @staticmethod
+    def parse(nvr):
+        if not nvr.strip():
+            raise RuntimeError("No package NVR to parse: %s" % nvr)
+        o = NVR()
+        try:
+            nvrtuple = re.match("^(^.*)-([^-]*)-([^-]*)$", nvr).groups()
+        except:
+            raise RuntimeError("Failed to parse NVR: %s" % nvr)
+        if not nvrtuple:
+            raise RuntimeError("Failed to parse nvr: %s" % nvr)
+        o.name, o.version, o.release = nvrtuple
+        return o
+
+    def __cmp__(self, other):
+        if not self.name == other.name:
+            raise RuntimeError("NVRs for different names: %s %s"
+                               % (self.name, other.name))
+        this_version = (None, self.version, self.release)
+        other_version = (None, other.version, other.release)
+        return rpm.labelCompare(this_version,  # @UndefinedVariable
+                                other_version)
+
+    def __str__(self):
+        return "%s-%s-%s" % (self.name, self.version, self.release)
+
+
+class RpmPackage(base.Base):
+    """Provide access to some rpm meta informations
+    """
+
+    name = None
+
+    def __init__(self, name):
+        super(RpmPackage, self).__init__()
+        self.name = name
+
+    def _raw_nvr(self):
+        cmd = ["rpm", "-q", self.name]
+        nvr = process.pipe(cmd).strip().split("\n")
+        self.logger.debug("Found build: %s" % nvr)
+        if len(nvr) != 1:
+            raise RuntimeError("Failed to retrieve nvr for %s: %s" %
+                               (self.name, nvr))
+        return nvr[0]
+
+    def nvr(self):
+        return NVR.parse(self._raw_nvr())
+
+
 class SystemRelease(base.Base):
     """Informations about the OS based on /etc/system-release-cpe
 


-- 
To view, visit http://gerrit.ovirt.org/24775
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I280f18235374da5604747499f2491669d75b9970
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-node
Gerrit-Branch: node-3.0
Gerrit-Owner: Fabian Deutsch <fabiand at fedoraproject.org>



More information about the node-patches mailing list