From c511c8cf4289c1a561102cfa78ea2827c517991f Mon Sep 17 00:00:00 2001
From: Matej Habrnal <mhabrnal@redhat.com>
Date: Thu, 1 Jun 2017 09:59:53 +0200
Subject: [PATCH] abrt addon python3 fixes

Signed-off-by: Matej Habrnal <mhabrnal@redhat.com>
---
 configure.ac                            | 37 ++++++++++++++++++++++++
 doc/Makefile.am                         |  2 ++
 doc/abrt-python3.conf.txt               | 24 ++++++++++++++++
 doc/python3_event.conf.5                |  1 +
 src/hooks/Makefile.am                   |  2 +-
 src/hooks/abrt_exception_handler3.py.in | 50 ++++++++++++++++++++-------------
 src/plugins/Makefile.am                 |  1 +
 src/plugins/python3_event.conf          | 32 +++++++++++++++++++++
 8 files changed, 128 insertions(+), 21 deletions(-)
 create mode 100644 doc/abrt-python3.conf.txt
 create mode 100644 doc/python3_event.conf.5
 create mode 100644 src/plugins/python3_event.conf

diff --git a/configure.ac b/configure.ac
index 02d7e0e..90316e6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -92,6 +92,43 @@ PYTHON_LIBS=`python-config --libs 2> /dev/null`
 AC_SUBST(PYTHON_CFLAGS)
 AC_SUBST(PYTHON_LIBS)
 
+AM_CONDITIONAL(BUILD_PYTHON3, true)
+AC_PATH_PROG([PYTHON3], [python3], [no])
+[if test "$PYTHON3" == "no"]
+[then]
+    [echo "The python3 program was not found in the search path. Please ensure"]
+    [echo "that it is installed and its directory is included in the search path or"]
+    [echo "pass --without-python3 to ./configure."]
+    [echo "Then run configure again before attempting to build ABRT."]
+    [exit 1]
+[fi]
+
+AC_PATH_PROG([PYTHON3_CONFIG], [python3-config], [no])
+[if test "$PYTHON3_CONFIG" = "no"]
+[then]
+    [echo "The python3-config program was not found in the search path. Please ensure"]
+    [echo "that it is installed and its directory is included in the search path or"]
+    [echo "pass --without-python3 to ./configure."]
+    [echo "Then run configure again before attempting to build ABRT."]
+    [exit 1]
+[fi]
+
+PYTHON3_CFLAGS=`python3-config --cflags 2> /dev/null`
+PYTHON3_LIBS=`python3-config --libs 2> /dev/null`
+
+AC_SUBST([PYTHON3_PREFIX], ['${prefix}'])
+AC_SUBST([PYTHON3_EXEC_PREFIX], ['${exec_prefix}'])
+
+PYTHON3_DIR=`$PYTHON3 -c "import distutils.sysconfig; \
+    print(distutils.sysconfig.get_python_lib(0,0,prefix='$PYTHON3_PREFIX'))"`
+PYTHON3_EXECDIR=`$PYTHON3 -c "import distutils.sysconfig; \
+    print(distutils.sysconfig.get_python_lib(1,0,prefix='$PYTHON3_EXEC_PREFIX'))"`
+
+AC_SUBST(PYTHON3_CFLAGS)
+AC_SUBST(PYTHON3_LIBS)
+AC_SUBST(python3dir, $PYTHON3_DIR)
+AC_SUBST(py3execdir, $PYTHON3_EXECDIR)
+
 PKG_CHECK_MODULES([XICE], [ice])
 PKG_CHECK_MODULES([XSMP], [sm])
 PKG_CHECK_MODULES([GTK], [gtk+-3.0])
diff --git a/doc/Makefile.am b/doc/Makefile.am
index d95d9fb..ae05327 100644
--- a/doc/Makefile.am
+++ b/doc/Makefile.am
@@ -46,6 +46,7 @@ MAN5_TXT += abrt.conf.txt
 MAN5_TXT += abrt-action-save-package-data.conf.txt
 MAN5_TXT += abrt-xorg.conf.txt
 MAN5_TXT += abrt-python.conf.txt
+MAN5_TXT += abrt-python3.conf.txt
 MAN5_TXT += abrt-CCpp.conf.txt
 MAN5_TXT += abrt-oops.conf.txt
 MAN5_TXT += gpg_keys.conf.txt
@@ -58,6 +59,7 @@ MAN5_PREFORMATTED += ccpp_retrace_event.conf.5
 MAN5_PREFORMATTED += gconf_event.conf.5
 MAN5_PREFORMATTED += koops_event.conf.5
 MAN5_PREFORMATTED += python_event.conf.5
+MAN5_PREFORMATTED += python3_event.conf.5
 MAN5_PREFORMATTED += smart_event.conf.5
 MAN5_PREFORMATTED += vimrc_event.conf.5
 MAN5_PREFORMATTED += vmcore_event.conf.5
diff --git a/doc/abrt-python3.conf.txt b/doc/abrt-python3.conf.txt
new file mode 100644
index 0000000..a8f3913
--- /dev/null
+++ b/doc/abrt-python3.conf.txt
@@ -0,0 +1,24 @@
+abrt-python3.conf(5)
+===================
+
+NAME
+----
+abrt-python3.conf - Configuration file for ABRT's python 3 crash hook
+
+DESCRIPTION
+-----------
+Currently, only one item exists:
+
+RequireAbsolutePath = 'yes' / 'no' ...::
+   If set to 'no', unhandled python 3 exceptions will be caught
+   and saved even in scripts which are run without full path
+   in sys.argv[0].
+   Default is 'yes': do not save them.
+
+SEE ALSO
+--------
+abrt.conf(5)
+
+AUTHORS
+-------
+* ABRT team
diff --git a/doc/python3_event.conf.5 b/doc/python3_event.conf.5
new file mode 100644
index 0000000..71c3fcb
--- /dev/null
+++ b/doc/python3_event.conf.5
@@ -0,0 +1 @@
+.so man5/report_event.conf.5
diff --git a/src/hooks/Makefile.am b/src/hooks/Makefile.am
index 85502a0..091e322 100644
--- a/src/hooks/Makefile.am
+++ b/src/hooks/Makefile.am
@@ -7,7 +7,7 @@ pluginsconfdir = $(PLUGINS_CONF_DIR)
 dist_pluginsconf_DATA = \
     CCpp.conf \
     python.conf \
-    oops.conf
+    oops.conf \
     python3.conf \
     vmcore.conf
 
diff --git a/src/hooks/abrt_exception_handler3.py.in b/src/hooks/abrt_exception_handler3.py.in
index b20e6f5..e80a7ac 100644
--- a/src/hooks/abrt_exception_handler3.py.in
+++ b/src/hooks/abrt_exception_handler3.py.in
@@ -27,16 +27,12 @@ import os
 def syslog(msg):
     """Log message to system logger (journal)"""
 
-    from systemd import journal
-
-    # required as a workaround for rhbz#1023041
-    # where journal tries to log into non-existent log
-    # and fails (during %check in mock)
-    #
-    # try/except block should be removed when the bug is fixed
+    import syslog
 
     try:
-        journal.send(msg)
+        syslog.openlog(ident="python3")
+        syslog.syslog(syslog.LOG_ERR, msg)
+        syslog.closelog()
     except:
         pass
 
@@ -51,11 +47,17 @@ def send(data):
         s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
         s.settimeout(5)
         s.connect(@VAR_RUN@ + "/abrt/abrt.socket")
-        pre = "POST / HTTP/1.1\r\n\r\ntype=Python\0"
+        pre = "POST / HTTP/1.1\r\n\r\n"
+        pre += "type=Python3\0"
+        # Using analyzer=Python intentionally.
+        # This causes the some workflow as for Python
+        # will be used so we don't need to add a new
+        # workflow for Python 3 into libreport due to added
+        # abrt-addon-python3 package to epel7
+        pre += "analyzer=Python\0"
         s.sendall(pre.encode())
         s.sendall(data.encode())
 
-        s.sendall("\0".encode())
         s.shutdown(socket.SHUT_WR)
 
         while True:
@@ -89,9 +91,11 @@ def write_dump(tb_text, tb):
     data += "reason={0}\0".format(tb_text.splitlines()[0])
     data += "backtrace={0}\0".format(tb_text)
 
+    # add environ element
     data += "environ="
-    for k, v in os.environ.items():
-        data += "{0}={1}\n".format(k, v)
+    for k,v in os.environ.items():
+        data += "{0}={1}\n".format(k,v)
+    data += "\0"
 
     response = send(data)
     parts = response.split()
@@ -102,14 +106,20 @@ def write_dump(tb_text, tb):
         syslog("error sending data to ABRT daemon: {0}".format(response))
 
 
-def conf_enabled(var_name):
+def require_abs_path():
+    """
+    Return True if absolute path requirement is enabled
+    in configuration
+    """
+
     import problem
+
     try:
         conf = problem.load_plugin_conf_file("python3.conf")
-    except:
-        return -1
-    else:
-        conf.get(var_name, -1)
+    except OsError:
+        return False
+
+    return conf.get("RequireAbsolutePath", "yes") == "yes"
 
 
 def handle_exception(etype, value, tb):
@@ -158,7 +168,7 @@ def handle_exception(etype, value, tb):
                .format(sys.argv[0]))
 
         if sys.argv[0][0] != "/":
-            if conf_enabled("RequireAbsolutePath") != 0:
+            if require_abs_path():
                 raise Exception
 
         import traceback
@@ -167,8 +177,8 @@ def handle_exception(etype, value, tb):
 
         if tb is not None and etype != IndentationError:
             tblast = traceback.extract_tb(tb, limit=None)
-            if len(tblast):
-                tblast = tblast[len(tblast) - 1]
+            if tblast:
+                tblast = tuple(tblast[-1])
             extxt = traceback.format_exception_only(etype, value)
             if tblast and len(tblast) > 3:
                 ll = []
diff --git a/src/plugins/Makefile.am b/src/plugins/Makefile.am
index 6dde4b7..557d218 100644
--- a/src/plugins/Makefile.am
+++ b/src/plugins/Makefile.am
@@ -65,6 +65,7 @@ dist_eventsconf_DATA = \
     xorg_event.conf \
     vmcore_event.conf \
     python_event.conf \
+    python3_event.conf \
     smart_event.conf \
     gconf_event.conf \
     vimrc_event.conf
diff --git a/src/plugins/python3_event.conf b/src/plugins/python3_event.conf
new file mode 100644
index 0000000..9d1e955
--- /dev/null
+++ b/src/plugins/python3_event.conf
@@ -0,0 +1,32 @@
+EVENT=post-create type=Python3 remote!=1
+        # (For now this has no effect since python hook doesn't save environ)
+        if grep -q ^ABRT_IGNORE_ALL=1 environ \
+        || grep -q ^ABRT_IGNORE_PYTHON=1 environ \
+        ; then
+            echo "ABRT_IGNORE variable is 1 - not saving the crash"
+            # abrtd will delete the problem directory when we exit nonzero:
+            exit 1
+        fi
+        abrt-action-analyze-python
+
+EVENT=report_Bugzilla type=Python3 component!=anaconda
+        test -f component || abrt-action-save-package-data
+        reporter-bugzilla -b \
+                -c /etc/libreport/plugins/bugzilla.conf \
+                -F /etc/libreport/plugins/bugzilla_format.conf \
+                -A /etc/libreport/plugins/bugzilla_formatdup.conf
+
+# Send micro report
+EVENT=report_uReport type=Python3
+        /usr/libexec/abrt-action-ureport
+
+# update ABRT database after successful report to bugzilla
+EVENT=post_report type=Python3
+        reporter-ureport -A -B
+
+# Reporting of python exceptions
+EVENT=report-gui type=Python3 component!=anaconda
+        report-gtk -- "$DUMP_DIR"
+
+EVENT=report-cli type=Python3 component!=anaconda
+        report-cli -- "$DUMP_DIR"
-- 
1.8.3.1

