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

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