>From af994200599d97c2675db8f628c6cc6dcf43aa68 Mon Sep 17 00:00:00 2001
From: Jiri Moskovcak <jmoskovc@redhat.com>
Date: Thu, 28 Jul 2011 10:03:26 +0200
Subject: [PATCH] improved compatibility with anaconda rhbz#725857

---
 src/lib/create_dump_dir.c     |    9 ++-
 src/lib/dump_dir.c            |   15 ++++-
 src/lib/parse_release.c       |    6 ++
 src/report-python/__init__.py |  152 ++++++++++++++++++++++++----------------
 4 files changed, 117 insertions(+), 65 deletions(-)

diff --git a/src/lib/create_dump_dir.c b/src/lib/create_dump_dir.c
index 13c42e4..27e3761 100644
--- a/src/lib/create_dump_dir.c
+++ b/src/lib/create_dump_dir.c
@@ -23,8 +23,6 @@ static struct dump_dir *try_dd_create(const char *base_dir_name, const char *dir
 {
     char *path = concat_path_file(base_dir_name, dir_name);
     struct dump_dir *dd = dd_create(path, (uid_t)-1L, 0640);
-    if (dd)
-        dd_create_basic_files(dd, (uid_t)-1L);
     free(path);
     return dd;
 }
@@ -81,5 +79,12 @@ struct dump_dir *create_dump_dir_from_problem_data(problem_data_t *problem_data,
  next: ;
     }
 
+    /* need to create basic files AFTER we save the pd to dump_dir
+     * otherwise we can't skip already created files like in case when
+     * reporting from anaconda where we can't read /etc/{system,redhat}-release
+     * and os_release is taken from anaconda
+    */
+    dd_create_basic_files(dd, (uid_t)-1L);
+
     return dd;
 }
diff --git a/src/lib/dump_dir.c b/src/lib/dump_dir.c
index e4e7fad..daac0aa 100644
--- a/src/lib/dump_dir.c
+++ b/src/lib/dump_dir.c
@@ -492,8 +492,19 @@ void dd_create_basic_files(struct dump_dir *dd, uid_t uid)
     dd_save_text(dd, FILENAME_ARCHITECTURE, buf.machine);
     dd_save_text(dd, FILENAME_HOSTNAME, buf.nodename);
 
-    char *release = load_text_file("/etc/system-release",
-                DD_LOAD_TEXT_RETURN_NULL_ON_FAILURE);
+    /* if release exists in dumpdir don't create it, but don't warn
+     * if it doesn't
+     * i.e: anaconda doesn't have /etc/{fedora,redhat}-release and trying to load it
+     * results in errors: rhbz#725857
+    */
+    char *release = dd_load_text_ext(dd, FILENAME_OS_RELEASE,
+                    DD_LOAD_TEXT_RETURN_NULL_ON_FAILURE);
+
+    if (release)
+        return;
+
+    release = load_text_file("/etc/system-release",
+            DD_LOAD_TEXT_RETURN_NULL_ON_FAILURE);
     if (!release)
         release = load_text_file("/etc/redhat-release", /*flags:*/ 0);
     dd_save_text(dd, FILENAME_OS_RELEASE, release);
diff --git a/src/lib/parse_release.c b/src/lib/parse_release.c
index 2cc11ba..889f539 100644
--- a/src/lib/parse_release.c
+++ b/src/lib/parse_release.c
@@ -40,6 +40,12 @@ static void parse_release(const char *release, char** product, char** version, b
         strbuf_append_str(buf_product, release);
     }
 
+    /* examples of release strings:
+     * installed system: Red Hat Enterprise Linux Server release 6.2 Beta (Santiago)
+     * anaconda: Red Hat Enterprise Linux 6.2
+       * ^ note missing "release"
+     * so the following parsing would fail, workaround is in python bindings
+     */
     const char *r = strstr(release, "release");
     const char *space = r ? strchr(r, ' ') : NULL;
 
diff --git a/src/report-python/__init__.py b/src/report-python/__init__.py
index 796d469..44d7135 100644
--- a/src/report-python/__init__.py
+++ b/src/report-python/__init__.py
@@ -16,108 +16,130 @@ from _pyreport import *
 
 
 #Compatibility with report package:
+# Author(s): Gavin Romig-Koch <gavin@redhat.com>
+# ABRT Team
 
 import os
 
 SYSTEM_RELEASE_PATHS = ["/etc/system-release","/etc/redhat-release"]
-####SYSTEM_RELEASE_DEPS = ["system-release", "redhat-release"]
+SYSTEM_RELEASE_DEPS = ["system-release", "redhat-release"]
 
 _hardcoded_default_product = ""
 _hardcoded_default_version = ""
 
-####def getProduct_fromPRODUCT():
-####    try:
-####        import product
-####        return product.productName
-####    except:
-####        return ""
-
-####def getVersion_fromPRODUCT():
-####    try:
-####        import product
-####        return product.productVersion
-####    except:
-####        return ""
-
-####def getProduct_fromRPM():
-####    try:
-####        import rpm
-####        ts = rpm.TransactionSet()
-####        for each_dep in SYSTEM_RELEASE_DEPS:
-####            mi = ts.dbMatch('provides', each_dep)
-####            for h in mi:
-####                if h['name']:
-####                    return h['name'].split("-")[0].capitalize()
-####
-####        return ""
-####    except:
-####        return ""
-
-####def getVersion_fromRPM():
-####    try:
-####        import rpm
-####        ts = rpm.TransactionSet()
-####        for each_dep in SYSTEM_RELEASE_DEPS:
-####            mi = ts.dbMatch('provides', each_dep)
-####            for h in mi:
-####                if h['version']:
-####                    return str(h['version'])
-####        return ""
-####    except:
-####        return ""
+def getProduct_fromRPM():
+    try:
+        import rpm
+        ts = rpm.TransactionSet()
+        for each_dep in SYSTEM_RELEASE_DEPS:
+            mi = ts.dbMatch('provides', each_dep)
+            for h in mi:
+                if h['name']:
+                    return h['name'].split("-")[0].capitalize()
+
+        return ""
+    except:
+        return ""
+
+def getProduct_fromPRODUCT():
+    try:
+        from pyanaconda import product
+        return product.productName
+    except:
+        try:
+            import product
+            return product.productName
+        except:
+            return ""
 
 def getProduct_fromFILE():
     for each_path in SYSTEM_RELEASE_PATHS:
-        try:
+        if os.path.exists(each_path):
             file = open(each_path, "r")
             content = file.read()
             if content.startswith("Red Hat Enterprise Linux"):
                 return "Red Hat Enterprise Linux"
+
             if content.startswith("Fedora"):
                 return "Fedora"
+
             i = content.find(" release")
             if i > -1:
                 return content[0:i]
-        except:
-            pass
+
     return ""
 
+def getVersion_fromRPM():
+    try:
+        import rpm
+        ts = rpm.TransactionSet()
+        for each_dep in SYSTEM_RELEASE_DEPS:
+            mi = ts.dbMatch('provides', each_dep)
+            for h in mi:
+                if h['version']:
+                    return str(h['version'])
+
+        return ""
+    except:
+        return ""
+
 def getVersion_fromFILE():
     for each_path in SYSTEM_RELEASE_PATHS:
-        try:
+        if os.path.exists(each_path):
             file = open(each_path, "r")
             content = file.read()
             if content.find("Rawhide") > -1:
                 return "rawhide"
+
             clist = content.split(" ")
             i = clist.index("release")
             return clist[i+1]
+        else:
+            return ""
+
+def getVersion_fromPRODUCT():
+    try:
+        from pyanaconda import product
+        return product.productVersion
+    except:
+        try:
+            import product
+            return product.productVersion
         except:
-            pass
-    return ""
+            return ""
+
 
 def getProduct():
-    ####product = getProduct_fromPRODUCT()
-    ####if product:
-    ####    return product
+    """Attempt to determine the product of the running system by first asking
+       rpm, and then falling back on a hardcoded default.
+    """
+    product = getProduct_fromPRODUCT()
+    if product:
+        return product
     product = getProduct_fromFILE()
     if product:
         return product
-    ####product = getProduct_fromRPM()
-    ####if product:
-    ####    return product
+    product = getProduct_fromRPM()
+    if product:
+        return product
+
     return _hardcoded_default_product
 
 def getVersion():
-    ####version = getVersion_fromPRODUCT()
-    ####if version:
-    ####    return version
+    """Attempt to determine the version of the running system by first asking
+       rpm, and then falling back on a hardcoded default.  Always return as
+       a string.
+    """
+    version = getVersion_fromPRODUCT()
+    if version:
+        return version
     version = getVersion_fromFILE()
     if version:
         return version
-    ####version = getVersion_fromRPM()
-    ####if version:
-    ####    return version
+    version = getVersion_fromRPM()
+    if version:
+        return version
+
     return _hardcoded_default_version
 
 def createAlertSignature(component, hashmarkername, hashvalue, summary, alertSignature):
@@ -140,8 +162,16 @@ def createPythonUnhandledExceptionSignature(component, hashmarkername, hashvalue
     pd.add("duphash", hashvalue)
     pd.add("reason", summary)
     pd.add("description", description)
-    #pd.add("product", getProduct())
-    #pd.add("version", getVersion())
+    product = getProduct()
+    if product:
+        pd.add("product", product)
+    version = getVersion()
+    if version:
+        pd.add("version", version)
+    #libreport expect the os_release as in /etc/redhat-release
+    if (version and product):
+        # need to add "release", parse_release() expects format "<product> release <version>"
+        pd.add("os_release", product +" release "+ version)
     pd.add_basics() # adds product and version + some other required field
     # FIXME: how to handle files out of dump dir??
     #1 = flag BIN
-- 
1.7.6

