Package portage :: Package elog :: Module mod_mail_summary
[hide private]

Source Code for Module portage.elog.mod_mail_summary

 1  # elog/mod_mail_summary.py - elog dispatch module 
 2  # Copyright 2006-2010 Gentoo Foundation 
 3  # Distributed under the terms of the GNU General Public License v2 
 4   
 5  import portage 
 6  from portage.exception import AlarmSignal, PortageException 
 7  from portage.localization import _ 
 8  from portage.util import writemsg 
 9  from portage import os 
10  from portage import _encodings 
11  from portage import _unicode_decode 
12   
13  import socket 
14  import time 
15   
16  _config_keys = ('PORTAGE_ELOG_MAILURI', 'PORTAGE_ELOG_MAILFROM', 
17          'PORTAGE_ELOG_MAILSUBJECT',) 
18  _items = {} 
19 -def process(mysettings, key, logentries, fulltext):
20 global _items 21 time_str = _unicode_decode( 22 time.strftime("%Y%m%d-%H%M%S %Z", time.localtime(time.time())), 23 encoding=_encodings['content'], errors='replace') 24 header = _(">>> Messages generated for package %(pkg)s by process %(pid)d on %(time)s:\n\n") % \ 25 {"pkg": key, "pid": os.getpid(), "time": time_str} 26 config_root = mysettings["PORTAGE_CONFIGROOT"] 27 28 # Copy needed variables from the config instance, 29 # since we don't need to hold a reference for the 30 # whole thing. This also makes it possible to 31 # rely on per-package variable settings that may 32 # have come from /etc/portage/package.env, since 33 # we'll be isolated from any future mutations of 34 # mysettings. 35 config_dict = {} 36 for k in _config_keys: 37 v = mysettings.get(k) 38 if v is not None: 39 config_dict[k] = v 40 41 config_dict, items = _items.setdefault(config_root, (config_dict, {})) 42 items[key] = header + fulltext
43
44 -def finalize():
45 global _items 46 for mysettings, items in _items.values(): 47 _finalize(mysettings, items) 48 _items.clear()
49
50 -def _finalize(mysettings, items):
51 if len(items) == 0: 52 return 53 elif len(items) == 1: 54 count = _("one package") 55 else: 56 count = _("multiple packages") 57 if "PORTAGE_ELOG_MAILURI" in mysettings: 58 myrecipient = mysettings["PORTAGE_ELOG_MAILURI"].split()[0] 59 else: 60 myrecipient = "root@localhost" 61 62 myfrom = mysettings.get("PORTAGE_ELOG_MAILFROM", "") 63 myfrom = myfrom.replace("${HOST}", socket.getfqdn()) 64 mysubject = mysettings.get("PORTAGE_ELOG_MAILSUBJECT", "") 65 mysubject = mysubject.replace("${PACKAGE}", count) 66 mysubject = mysubject.replace("${HOST}", socket.getfqdn()) 67 68 mybody = _("elog messages for the following packages generated by " 69 "process %(pid)d on host %(host)s:\n") % {"pid": os.getpid(), "host": socket.getfqdn()} 70 for key in items: 71 mybody += "- %s\n" % key 72 73 mymessage = portage.mail.create_message(myfrom, myrecipient, mysubject, 74 mybody, attachments=list(items.values())) 75 76 # Timeout after one minute in case send_mail() blocks indefinitely. 77 try: 78 try: 79 AlarmSignal.register(60) 80 portage.mail.send_mail(mysettings, mymessage) 81 finally: 82 AlarmSignal.unregister() 83 except AlarmSignal: 84 writemsg("Timeout in finalize() for elog system 'mail_summary'\n", 85 noiselevel=-1) 86 except PortageException as e: 87 writemsg("%s\n" % (e,), noiselevel=-1) 88 89 return
90