diff options
Diffstat (limited to 'Tools/scripts/mailerdaemon.py')
-rwxr-xr-x | Tools/scripts/mailerdaemon.py | 62 |
1 files changed, 27 insertions, 35 deletions
diff --git a/Tools/scripts/mailerdaemon.py b/Tools/scripts/mailerdaemon.py index 635e548..a1eeaad 100755 --- a/Tools/scripts/mailerdaemon.py +++ b/Tools/scripts/mailerdaemon.py @@ -1,31 +1,26 @@ -#!/usr/bin/env python3 -"""Classes to parse mailer-daemon messages.""" +#!/usr/bin/env python +"""mailerdaemon - classes to parse mailer-daemon messages""" +import rfc822 import calendar -import email.message import re import os import sys +Unparseable = 'mailerdaemon.Unparseable' -class Unparseable(Exception): - pass - - -class ErrorMessage(email.message.Message): - def __init__(self): - email.message.Message.__init__(self) +class ErrorMessage(rfc822.Message): + def __init__(self, fp): + rfc822.Message.__init__(self, fp) self.sub = '' def is_warning(self): - sub = self.get('Subject') + sub = self.getheader('Subject') if not sub: return 0 sub = sub.lower() - if sub.startswith('waiting mail'): - return 1 - if 'warning' in sub: - return 1 + if sub.startswith('waiting mail'): return 1 + if 'warning' in sub: return 1 self.sub = sub return 0 @@ -88,7 +83,7 @@ del i # no more expressions are searched for. So, order is important. emparse_list_reason = [ r'^5\d{2} <>\.\.\. (?P<reason>.*)', - r'<>\.\.\. (?P<reason>.*)', + '<>\.\.\. (?P<reason>.*)', re.compile(r'^<<< 5\d{2} (?P<reason>.*)', re.MULTILINE), re.compile('===== stderr was =====\nrmail: (?P<reason>.*)'), re.compile('^Diagnostic-Code: (?P<reason>.*)', re.MULTILINE), @@ -151,17 +146,14 @@ def emparse_list(fp, sub): errors.append(' '.join((email.strip()+': '+reason).split())) return errors -EMPARSERS = [emparse_list] +EMPARSERS = [emparse_list, ] def sort_numeric(a, b): a = int(a) b = int(b) - if a < b: - return -1 - elif a > b: - return 1 - else: - return 0 + if a < b: return -1 + elif a > b: return 1 + else: return 0 def parsedir(dir, modify): os.chdir(dir) @@ -172,19 +164,19 @@ def parsedir(dir, modify): nok = nwarn = nbad = 0 # find all numeric file names and sort them - files = list(filter(lambda fn, pat=pat: pat.match(fn) is not None, os.listdir('.'))) + files = filter(lambda fn, pat=pat: pat.match(fn) is not None, os.listdir('.')) files.sort(sort_numeric) for fn in files: # Lets try to parse the file. fp = open(fn) - m = email.message_from_file(fp, _class=ErrorMessage) + m = ErrorMessage(fp) sender = m.getaddr('From') - print('%s\t%-40s\t'%(fn, sender[1]), end=' ') + print '%s\t%-40s\t'%(fn, sender[1]), if m.is_warning(): fp.close() - print('warning only') + print 'warning only' nwarn = nwarn + 1 if modify: os.rename(fn, ','+fn) @@ -194,11 +186,11 @@ def parsedir(dir, modify): try: errors = m.get_errors() except Unparseable: - print('** Not parseable') + print '** Not parseable' nbad = nbad + 1 fp.close() continue - print(len(errors), 'errors') + print len(errors), 'errors' # Remember them for e in errors: @@ -207,7 +199,7 @@ def parsedir(dir, modify): date = '%s %02d' % (calendar.month_abbr[mm], dd) except: date = '??????' - if e not in errordict: + if not errordict.has_key(e): errordict[e] = 1 errorfirst[e] = '%s (%s)' % (fn, date) else: @@ -220,16 +212,16 @@ def parsedir(dir, modify): os.rename(fn, ','+fn) ## os.unlink(fn) - print('--------------') - print(nok, 'files parsed,',nwarn,'files warning-only,', end=' ') - print(nbad,'files unparseable') - print('--------------') + print '--------------' + print nok, 'files parsed,',nwarn,'files warning-only,', + print nbad,'files unparseable' + print '--------------' list = [] for e in errordict.keys(): list.append((errordict[e], errorfirst[e], errorlast[e], e)) list.sort() for num, first, last, e in list: - print('%d %s - %s\t%s' % (num, first, last, e)) + print '%d %s - %s\t%s' % (num, first, last, e) def main(): modify = 0 |