annotate Tools/scripts/mailerdaemon.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents 8a7a2f1f88f0
children 46625e6122e9 7fb0abf928e0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1 """mailerdaemon - classes to parse mailer-daemon messages"""
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
2
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
3 import rfc822
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
4 import calendar
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
5 import re
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
6 import os
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
7 import sys
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
8
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
9 Unparseable = 'mailerdaemon.Unparseable'
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
10
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
11 class ErrorMessage(rfc822.Message):
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
12 def __init__(self, fp):
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
13 rfc822.Message.__init__(self, fp)
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
14 self.sub = ''
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
15
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
16 def is_warning(self):
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
17 sub = self.getheader('Subject')
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
18 if not sub:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
19 return 0
25219
8a7a2f1f88f0 Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents: 15930
diff changeset
20 sub = sub.lower()
8a7a2f1f88f0 Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents: 15930
diff changeset
21 if sub.startswith('waiting mail'): return 1
8a7a2f1f88f0 Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents: 15930
diff changeset
22 if 'warning' in sub: return 1
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
23 self.sub = sub
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
24 return 0
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
25
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
26 def get_errors(self):
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
27 for p in EMPARSERS:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
28 self.rewindbody()
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
29 try:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
30 return p(self.fp, self.sub)
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
31 except Unparseable:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
32 pass
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
33 raise Unparseable
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
34
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
35 # List of re's or tuples of re's.
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
36 # If a re, it should contain at least a group (?P<email>...) which
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
37 # should refer to the email address. The re can also contain a group
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
38 # (?P<reason>...) which should refer to the reason (error message).
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
39 # If no reason is present, the emparse_list_reason list is used to
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
40 # find a reason.
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
41 # If a tuple, the tuple should contain 2 re's. The first re finds a
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
42 # location, the second re is repeated one or more times to find
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
43 # multiple email addresses. The second re is matched (not searched)
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
44 # where the previous match ended.
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
45 # The re's are compiled using the re module.
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
46 emparse_list_list = [
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
47 'error: (?P<reason>unresolvable): (?P<email>.+)',
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
48 ('----- The following addresses had permanent fatal errors -----\n',
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
49 '(?P<email>[^ \n].*)\n( .*\n)?'),
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
50 'remote execution.*\n.*rmail (?P<email>.+)',
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
51 ('The following recipients did not receive your message:\n\n',
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
52 ' +(?P<email>.*)\n(The following recipients did not receive your message:\n\n)?'),
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
53 '------- Failure Reasons --------\n\n(?P<reason>.*)\n(?P<email>.*)',
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
54 '^<(?P<email>.*)>:\n(?P<reason>.*)',
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
55 '^(?P<reason>User mailbox exceeds allowed size): (?P<email>.+)',
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
56 '^5\\d{2} <(?P<email>[^\n>]+)>\\.\\.\\. (?P<reason>.+)',
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
57 '^Original-Recipient: rfc822;(?P<email>.*)',
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
58 '^did not reach the following recipient\\(s\\):\n\n(?P<email>.*) on .*\n +(?P<reason>.*)',
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
59 '^ <(?P<email>[^\n>]+)> \\.\\.\\. (?P<reason>.*)',
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
60 '^Report on your message to: (?P<email>.*)\nReason: (?P<reason>.*)',
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
61 '^Your message was not delivered to +(?P<email>.*)\n +for the following reason:\n +(?P<reason>.*)',
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
62 '^ was not +(?P<email>[^ \n].*?) *\n.*\n.*\n.*\n because:.*\n +(?P<reason>[^ \n].*?) *\n',
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
63 ]
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
64 # compile the re's in the list and store them in-place.
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
65 for i in range(len(emparse_list_list)):
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
66 x = emparse_list_list[i]
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
67 if type(x) is type(''):
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
68 x = re.compile(x, re.MULTILINE)
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
69 else:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
70 xl = []
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
71 for x in x:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
72 xl.append(re.compile(x, re.MULTILINE))
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
73 x = tuple(xl)
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
74 del xl
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
75 emparse_list_list[i] = x
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
76 del x
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
77 del i
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
78
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
79 # list of re's used to find reasons (error messages).
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
80 # if a string, "<>" is replaced by a copy of the email address.
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
81 # The expressions are searched for in order. After the first match,
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
82 # no more expressions are searched for. So, order is important.
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
83 emparse_list_reason = [
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
84 r'^5\d{2} <>\.\.\. (?P<reason>.*)',
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
85 '<>\.\.\. (?P<reason>.*)',
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
86 re.compile(r'^<<< 5\d{2} (?P<reason>.*)', re.MULTILINE),
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
87 re.compile('===== stderr was =====\nrmail: (?P<reason>.*)'),
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
88 re.compile('^Diagnostic-Code: (?P<reason>.*)', re.MULTILINE),
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
89 ]
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
90 emparse_list_from = re.compile('^From:', re.IGNORECASE|re.MULTILINE)
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
91 def emparse_list(fp, sub):
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
92 data = fp.read()
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
93 res = emparse_list_from.search(data)
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
94 if res is None:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
95 from_index = len(data)
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
96 else:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
97 from_index = res.start(0)
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
98 errors = []
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
99 emails = []
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
100 reason = None
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
101 for regexp in emparse_list_list:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
102 if type(regexp) is type(()):
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
103 res = regexp[0].search(data, 0, from_index)
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
104 if res is not None:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
105 try:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
106 reason = res.group('reason')
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
107 except IndexError:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
108 pass
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
109 while 1:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
110 res = regexp[1].match(data, res.end(0), from_index)
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
111 if res is None:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
112 break
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
113 emails.append(res.group('email'))
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
114 break
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
115 else:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
116 res = regexp.search(data, 0, from_index)
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
117 if res is not None:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
118 emails.append(res.group('email'))
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
119 try:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
120 reason = res.group('reason')
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
121 except IndexError:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
122 pass
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
123 break
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
124 if not emails:
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
125 raise Unparseable
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
126 if not reason:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
127 reason = sub
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
128 if reason[:15] == 'returned mail: ':
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
129 reason = reason[15:]
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
130 for regexp in emparse_list_reason:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
131 if type(regexp) is type(''):
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
132 for i in range(len(emails)-1,-1,-1):
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
133 email = emails[i]
25219
8a7a2f1f88f0 Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents: 15930
diff changeset
134 exp = re.compile(re.escape(email).join(regexp.split('<>')), re.MULTILINE)
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
135 res = exp.search(data)
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
136 if res is not None:
25219
8a7a2f1f88f0 Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents: 15930
diff changeset
137 errors.append(' '.join((email.strip()+': '+res.group('reason')).split()))
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
138 del emails[i]
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
139 continue
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
140 res = regexp.search(data)
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
141 if res is not None:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
142 reason = res.group('reason')
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
143 break
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
144 for email in emails:
25219
8a7a2f1f88f0 Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents: 15930
diff changeset
145 errors.append(' '.join((email.strip()+': '+reason).split()))
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
146 return errors
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
147
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
148 EMPARSERS = [emparse_list, ]
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
149
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
150 def sort_numeric(a, b):
25219
8a7a2f1f88f0 Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents: 15930
diff changeset
151 a = int(a)
8a7a2f1f88f0 Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents: 15930
diff changeset
152 b = int(b)
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
153 if a < b: return -1
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
154 elif a > b: return 1
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
155 else: return 0
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
156
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
157 def parsedir(dir, modify):
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
158 os.chdir(dir)
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
159 pat = re.compile('^[0-9]*$')
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
160 errordict = {}
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
161 errorfirst = {}
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
162 errorlast = {}
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
163 nok = nwarn = nbad = 0
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
164
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
165 # find all numeric file names and sort them
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
166 files = filter(lambda fn, pat=pat: pat.match(fn) is not None, os.listdir('.'))
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
167 files.sort(sort_numeric)
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11878
diff changeset
168
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
169 for fn in files:
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
170 # Lets try to parse the file.
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
171 fp = open(fn)
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
172 m = ErrorMessage(fp)
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
173 sender = m.getaddr('From')
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
174 print '%s\t%-40s\t'%(fn, sender[1]),
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
175
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
176 if m.is_warning():
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
177 fp.close()
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
178 print 'warning only'
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
179 nwarn = nwarn + 1
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
180 if modify:
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
181 os.rename(fn, ','+fn)
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
182 ## os.unlink(fn)
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
183 continue
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
184
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
185 try:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
186 errors = m.get_errors()
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
187 except Unparseable:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
188 print '** Not parseable'
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
189 nbad = nbad + 1
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
190 fp.close()
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
191 continue
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
192 print len(errors), 'errors'
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
193
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
194 # Remember them
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
195 for e in errors:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
196 try:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
197 mm, dd = m.getdate('date')[1:1+2]
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
198 date = '%s %02d' % (calendar.month_abbr[mm], dd)
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
199 except:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
200 date = '??????'
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
201 if not errordict.has_key(e):
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
202 errordict[e] = 1
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
203 errorfirst[e] = '%s (%s)' % (fn, date)
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
204 else:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
205 errordict[e] = errordict[e] + 1
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
206 errorlast[e] = '%s (%s)' % (fn, date)
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
207
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
208 fp.close()
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
209 nok = nok + 1
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
210 if modify:
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
211 os.rename(fn, ','+fn)
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
212 ## os.unlink(fn)
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
213
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
214 print '--------------'
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
215 print nok, 'files parsed,',nwarn,'files warning-only,',
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
216 print nbad,'files unparseable'
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
217 print '--------------'
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
218 list = []
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
219 for e in errordict.keys():
11878
3d73ffc105d0 Fix multi-arg append().
Guido van Rossum <guido@python.org>
parents: 9316
diff changeset
220 list.append((errordict[e], errorfirst[e], errorlast[e], e))
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
221 list.sort()
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
222 for num, first, last, e in list:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
223 print '%d %s - %s\t%s' % (num, first, last, e)
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
224
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
225 def main():
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
226 modify = 0
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
227 if len(sys.argv) > 1 and sys.argv[1] == '-d':
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
228 modify = 1
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
229 del sys.argv[1]
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
230 if len(sys.argv) > 1:
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
231 for folder in sys.argv[1:]:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
232 parsedir(folder, modify)
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
233 else:
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
234 parsedir('/ufs/jack/Mail/errorsinbox', modify)
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
235
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
236 if __name__ == '__main__' or sys.argv[0] == __name__:
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
237 main()