New elog modules for dismerging messages into a single file/mail
authorMarius Mauch <genone@gentoo.org>
Tue, 19 Dec 2006 14:56:17 +0000 (14:56 -0000)
committerMarius Mauch <genone@gentoo.org>
Tue, 19 Dec 2006 14:56:17 +0000 (14:56 -0000)
svn path=/main/trunk/; revision=5325

NEWS
cnf/make.conf
pym/elog_modules/mod_mail_summary.py [new file with mode: 0644]
pym/elog_modules/mod_save_summary.py [new file with mode: 0644]
pym/portage.py

diff --git a/NEWS b/NEWS
index 5ee0bf3f1dc206c0f09f7d0fe8f5a74486f5dd3d..d80c27c39b31c9588ba1b1051e20c0a3248a458f 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -19,6 +19,11 @@ portage-2.1.2
 * DEPEND atoms support SLOT dependencies of the form ${CATEGORY}/${PN}:${SLOT}.
 * Development: Extend PYTHONPATH support to allow overriding the hardcoded 
   /usr/lib/portage/pym for development/testing purposes
+* New "finalize" hook for elog modules enabling modules to perform actions on 
+  shutdown (like dispatching collected messages).
+* New elog modules mail_summary and save_summary that act like the mail/save
+  modules except that they merge messages from multiple packages in a single
+  file/mail.
 
 portage-2.1.1
 ------------
index 6c3f415fc07a2afd321f5e7261930efa403c6d6b..a3e5b1438fb1175e046c632161a6106499325938 100644 (file)
@@ -325,6 +325,12 @@ PORTAGE_ELOG_CLASSES="warn error log"
 #                          syslog (sends all messages to syslog)
 #                          mail (send all messages to the mailserver defined 
 #                                in $PORTAGE_ELOG_MAILURI)
+#                          save_summary (like "save" but merges all messages
+#                                        in $PORT_LOGDIR/elog/summary.log,
+#                                        /var/log/portage/elog/summary.log if
+#                                        $PORT_LOGDIR is unset)
+#                          mail_summary (like "mail" but sends all messages in
+#                                        a single mail when emerge exits)
 #                      To use elog you should enable at least one module
 #PORTAGE_ELOG_SYSTEM="save mail"
 
diff --git a/pym/elog_modules/mod_mail_summary.py b/pym/elog_modules/mod_mail_summary.py
new file mode 100644 (file)
index 0000000..53628b9
--- /dev/null
@@ -0,0 +1,39 @@
+# portage.py -- core Portage functionality
+# Copyright 1998-2004 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+# $Id: mod_mail.py 3484 2006-06-10 22:38:44Z genone $
+
+import portage_mail, socket, os, time
+from email.MIMEText import MIMEText as TextMessage
+
+_items = {}
+def process(mysettings, cpv, logentries, fulltext):
+       header = ">>> Messages generated for package %s by process %d on %s:\n\n" % \
+               (cpv, os.getpid(), time.strftime("%Y%m%d-%H%M%S", time.gmtime(time.time())))
+       _items[cpv] = header + fulltext
+
+def finalize(mysettings):
+       if len(_items) == 0:
+               return
+       elif len(_items) == 1:
+               count = "one package"
+       else:
+               count = "multiple packages"
+       if mysettings.has_key("PORTAGE_ELOG_MAILURI"):
+               myrecipient = mysettings["PORTAGE_ELOG_MAILURI"].split()[0]
+       else:
+               myrecipient = "root@localhost"
+       
+       myfrom = mysettings["PORTAGE_ELOG_MAILFROM"]
+       mysubject = mysettings["PORTAGE_ELOG_MAILSUBJECT"]
+       mysubject = mysubject.replace("${PACKAGE}", count)
+       mysubject = mysubject.replace("${HOST}", socket.getfqdn())
+
+       mybody = "elog messages for the following packages generated by process %d on host %s:\n" % (count, os.getpid(), socket.getfqdn())
+       for cpv in _items.keys():
+                mybody += "- %s\n" % cpv
+
+       mymessage = portage_mail.create_message(myfrom, myrecipient, mysubject, mybody, attachments=_items.values())
+       portage_mail.send_mail(mysettings, mymessage)
+
+       return
diff --git a/pym/elog_modules/mod_save_summary.py b/pym/elog_modules/mod_save_summary.py
new file mode 100644 (file)
index 0000000..ed25079
--- /dev/null
@@ -0,0 +1,23 @@
+import os. time
+from portage_data import portage_uid, portage_gid
+
+def process(mysettings, cpv, logentries, fulltext):
+       if mysettings["PORT_LOGDIR"] != "":
+               elogdir = os.path.join(mysettings["PORT_LOGDIR"], "elog")
+       else:
+               elogdir = os.path.join(os.sep, "var", "log", "portage", "elog")
+       if not os.path.exists(elogdir):
+               os.makedirs(elogdir)
+       os.chown(elogdir, portage_uid, portage_gid)
+       os.chmod(elogdir, 02770)
+
+       # TODO: Locking
+       elogfilename = elogdir+"/summary.log"
+       elogfile = open(elogfilename, "a")
+       elogfile.write(">>> Messages generated by process %d on %s:\n\n" % \
+                       (os.getpid(), time.strftime("%Y%m%d-%H%M%S", time.gmtime(time.time()))))
+       elogfile.write(fulltext)
+       elogfile.write("\n")
+       elogfile.close()
+
+       return elogfilename
index 984be52731c672fdd768823e39c3c8a8a2c78ca8..af6607722aef5bc46b940d25113603f7ee38bdd8 100644 (file)
@@ -467,7 +467,7 @@ class digraph:
                                print "(%s)" % self.nodes[node][0][child]
 
 
-
+_elog_atexit_handlers = []
 def elog_process(cpv, mysettings):
        mylogfiles = listdir(mysettings["T"]+"/logging/")
        # shortcut for packages without any messages
@@ -513,6 +513,9 @@ def elog_process(cpv, mysettings):
                        logmodule = __import__("elog_modules.mod_"+s)
                        m = getattr(logmodule, "mod_"+s)
                        m.process(mysettings, cpv, mylogentries, fulllog)
+                       if hasattr(m, "finalize") and not m.finalize in _elog_atexit_handlers:
+                               _elog_atexit_handlers.append(m.finalize)
+                               atexit_register(m.finalize, mysettings)
                except (ImportError, AttributeError), e:
                        print "!!! Error while importing logging modules while loading \"mod_%s\":" % s
                        print e