Author: gnichols
Date: 2010-02-01 13:49:28 +0000 (Mon, 01 Feb 2010)
New Revision: 359
Added:
trunk/v7/redhatrelease.py
Log:
560635 - info test fails due to difficulties in parsing /etc/redhat-release
Added: trunk/v7/redhatrelease.py
===================================================================
--- trunk/v7/redhatrelease.py (rev 0)
+++ trunk/v7/redhatrelease.py 2010-02-01 13:49:28 UTC (rev 359)
@@ -0,0 +1,94 @@
+ # Copyright (c) 2006 Red Hat, Inc. All rights reserved. This copyrighted material
+# is made available to anyone wishing to use, modify, copy, or
+# redistribute it subject to the terms and conditions of the GNU General
+# Public License v.2.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE. See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# Author: Greg Nichols
+#
+import string, os, sys, re
+
+class RedHatRelease:
+
+ def __init__(self):
+ self.product = None
+ self.version = None
+ self.update = None
+ self.codeName = None
+ self._read()
+ self._parse()
+
+ def _read(self):
+ f=open("/etc/redhat-release")
+ self.text=f.readline()
+ f.close()
+
+ def _parse(self):
+ if self.text:
+ pattern = re.compile("^(?P<product>[a-zA-Z\ ]+)release
(?P<number>[1-9\.]+) \((?P<name>[a-zA-Z0-9\ ]+)\)")
+ match = pattern.match(self.text)
+ if match.group("product"):
+ self.product = match.group("product").strip()
+ if match.group("name"):
+ self.codeName = match.group("name").strip()
+ if match.group("number"):
+ majorMinor = match.group("number").split('.')
+ if len(majorMinor)>0:
+ self.version = int(majorMinor[0])
+ if len(majorMinor)>1:
+ self.update = int(majorMinor[1])
+ else:
+ nameParts = self.codeName.split(' ')
+ if len(nameParts) == 3:
+ self.update = int(nameParts[2])
+
+
+ def isValid(self):
+ return self.product and self.version and self.codeName
+
+
+ def getProduct(self): return self.product
+ def getVersion(self): return self.version
+ def getUpdate(self): return self.update
+ def getCodeName(self): return self.codeName
+
+ def getVersionPointUpdate(self):
+ if self.update:
+ return "%u.%u" % (self.version, self.update)
+ else:
+ return "%u" % self.version
+
+ def dump(self):
+ print ""
+ print self.text
+ print "-------------------------------"
+ print "Product: \"" + self.getProduct() + "\""
+ print "Version: \"" + str(self.getVersion()) +
"\""
+ print "Update: \"" + str(self.getUpdate()) + "\""
+ print "Version.Update \"" + self.getVersionPointUpdate() +
"\""
+ print "Code Name: \"" + self.getCodeName() + "\""
+ if not self.isValid():
+ print "Not Valid"
+
+if __name__ == "__main__":
+ redHatRelease = RedHatRelease()
+ redHatRelease.dump()
+
+ redHatRelease.text = "Red Hat Enterprise Linux Server release 5.4
(Tikanga)"
+ redHatRelease._parse()
+ redHatRelease.dump()
+
+ redHatRelease.text = "Red Hat Enterprise Linux release 4 (Bogus Update 3)"
+ redHatRelease._parse()
+ redHatRelease.dump()
+
+
+
+
\ No newline at end of file
Show replies by date