Blame SOURCES/0055-report-parse-release-version-from-os-release.patch

4b6aa8
From 2ca8777fa697d5beed91a7b7f2bdc2bb9b5578ce Mon Sep 17 00:00:00 2001
4b6aa8
From: Jakub Filak <jfilak@redhat.com>
4b6aa8
Date: Wed, 23 Jul 2014 10:59:55 +0200
4b6aa8
Subject: [LIBREPORT PATCH 55/93] report: parse release/version from os-release
4b6aa8
4b6aa8
Related to rhbz#1101240
4b6aa8
4b6aa8
Signed-off-by: Jakub Filak <jfilak@redhat.com>
4b6aa8
---
4b6aa8
 src/report-python/__init__.py | 69 ++++++++++++++++++++++++++++++++++++++-----
4b6aa8
 1 file changed, 61 insertions(+), 8 deletions(-)
4b6aa8
4b6aa8
diff --git a/src/report-python/__init__.py b/src/report-python/__init__.py
4b6aa8
index c11b000..b434672 100644
4b6aa8
--- a/src/report-python/__init__.py
4b6aa8
+++ b/src/report-python/__init__.py
4b6aa8
@@ -23,6 +23,9 @@ import os
4b6aa8
 
4b6aa8
 SYSTEM_RELEASE_PATHS = ["/etc/system-release","/etc/redhat-release"]
4b6aa8
 SYSTEM_RELEASE_DEPS = ["system-release", "redhat-release"]
4b6aa8
+SYSTEM_OS_RELEASE_FILE = "/etc/os-release"
4b6aa8
+OS_RELEASE_PRODUCT_FIELDS = ["REDHAT_BUGZILLA_PRODUCT", "REDHAT_SUPPORT_PRODUCT", "NAME"]
4b6aa8
+OS_RELEASE_VERSION_FIELDS = ["REDHAT_BUGZILLA_VERSION", "REDHAT_SUPPORT_VERSION", "NAME"]
4b6aa8
 
4b6aa8
 _hardcoded_default_product = ""
4b6aa8
 _hardcoded_default_version = ""
4b6aa8
@@ -57,6 +60,57 @@ def getVersion_fromRPM():
4b6aa8
         return ""
4b6aa8
 """
4b6aa8
 
4b6aa8
+def parse_os_release_lines(osreleaselines):
4b6aa8
+    osrel = {}
4b6aa8
+
4b6aa8
+    for line in osreleaselines:
4b6aa8
+        kvp = line.split('=')
4b6aa8
+        if len(kvp) < 2:
4b6aa8
+            continue
4b6aa8
+
4b6aa8
+        key = kvp[0]
4b6aa8
+        value = kvp[1]
4b6aa8
+        if len(kvp) > 2:
4b6aa8
+            value = "=".join(kvp[1:])
4b6aa8
+
4b6aa8
+        if value:
4b6aa8
+            osrel[key] = value.strip('"')
4b6aa8
+        else:
4b6aa8
+            osrel[key] = value
4b6aa8
+
4b6aa8
+    return osrel
4b6aa8
+
4b6aa8
+# /etc/os-release file parser
4b6aa8
+# see man os-release
4b6aa8
+def parse_os_release_file(filepath):
4b6aa8
+    osrel = {}
4b6aa8
+    try:
4b6aa8
+        with open(filepath) as osrelfil:
4b6aa8
+            osrel = parse_os_release_lines(osrelfil)
4b6aa8
+    except IOError as ex:
4b6aa8
+        # I am sorry, but we do not support logging here :(
4b6aa8
+        pass
4b6aa8
+
4b6aa8
+    return osrel
4b6aa8
+
4b6aa8
+def getProduct_fromOSRELEASE(file_path=SYSTEM_OS_RELEASE_FILE):
4b6aa8
+    osrel = parse_os_release_file(file_path)
4b6aa8
+
4b6aa8
+    for pf in OS_RELEASE_PRODUCT_FIELDS:
4b6aa8
+        if pf in osrel:
4b6aa8
+            return osrel[pf]
4b6aa8
+
4b6aa8
+    return None
4b6aa8
+
4b6aa8
+def getVersion_fromOSRELEASE(file_path=SYSTEM_OS_RELEASE_FILE):
4b6aa8
+    osrel = parse_os_release_file(file_path)
4b6aa8
+
4b6aa8
+    for vf in OS_RELEASE_VERSION_FIELDS:
4b6aa8
+        if vf in osrel:
4b6aa8
+            return osrel[vf]
4b6aa8
+
4b6aa8
+    return None
4b6aa8
+
4b6aa8
 def getProduct_fromFILE():
4b6aa8
     for each_path in SYSTEM_RELEASE_PATHS:
4b6aa8
         if os.path.exists(each_path):
4b6aa8
@@ -69,7 +123,6 @@ def getProduct_fromFILE():
4b6aa8
             content = file.read()
4b6aa8
             if content.startswith("Red Hat Enterprise Linux"):
4b6aa8
                 return "Red Hat Enterprise Linux"
4b6aa8
-
4b6aa8
             if content.startswith("Fedora"):
4b6aa8
                 return "Fedora"
4b6aa8
 
4b6aa8
@@ -92,11 +145,11 @@ def getVersion_fromFILE():
4b6aa8
             if content.find("Rawhide") > -1:
4b6aa8
                 return "rawhide"
4b6aa8
 
4b6aa8
-            clist = content.split(" ")
4b6aa8
-            i = clist.index("release")
4b6aa8
-            return clist[i+1]
4b6aa8
-        else:
4b6aa8
-            return ""
4b6aa8
+            i = content.find(" release")
4b6aa8
+            if i > -1:
4b6aa8
+                return content[i + len(" release"):]
4b6aa8
+
4b6aa8
+    return ""
4b6aa8
 
4b6aa8
 def getProduct_fromPRODUCT():
4b6aa8
     try:
4b6aa8
@@ -127,7 +180,7 @@ def getProduct():
4b6aa8
        asking anaconda
4b6aa8
        Always return as a string.
4b6aa8
     """
4b6aa8
-    for getter in (getProduct_fromFILE, getProduct_fromPRODUCT):
4b6aa8
+    for getter in (getProduct_fromOSRELEASE, getProduct_fromFILE, getProduct_fromPRODUCT):
4b6aa8
         product = getter()
4b6aa8
         if product:
4b6aa8
             return product
4b6aa8
@@ -140,7 +193,7 @@ def getVersion():
4b6aa8
        asking anaconda
4b6aa8
        Always return as a string.
4b6aa8
     """
4b6aa8
-    for getter in (getVersion_fromFILE, getVersion_fromPRODUCT):
4b6aa8
+    for getter in (getVersion_fromOSRELEASE, getVersion_fromFILE, getVersion_fromPRODUCT):
4b6aa8
         version = getter()
4b6aa8
         if version:
4b6aa8
             return version
4b6aa8
-- 
4b6aa8
1.8.3.1
4b6aa8