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

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