annotate Tools/scripts/mailerdaemon.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 7fb0abf928e0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
71588
7fb0abf928e0 Fix missing or shebangs and executable bits for scripts (#10318)
Éric Araujo <merwok@netwok.org>
parents: 25219
diff changeset
1 #!/usr/bin/env python
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
2 """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
3
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
4 import rfc822
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
5 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
6 import re
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
7 import os
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
8 import sys
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
9
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
10 Unparseable = 'mailerdaemon.Unparseable'
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
11
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
12 class ErrorMessage(rfc822.Message):
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
13 def __init__(self, fp):
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
14 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
15 self.sub = ''
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
16
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
17 def is_warning(self):
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
18 sub = self.getheader('Subject')
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
19 if not sub:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
20 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
21 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
22 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
23 if 'warning' in sub: return 1
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
24 self.sub = sub
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
25 return 0
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
26
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
27 def get_errors(self):
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
28 for p in EMPARSERS:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
29 self.rewindbody()
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
30 try:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
31 return p(self.fp, self.sub)
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
32 except Unparseable:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
33 pass
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
34 raise Unparseable
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
35
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
36 # 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
37 # 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
38 # 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
39 # (?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
40 # 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
41 # 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
42 # 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
43 # 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
44 # 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
45 # 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
46 # 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
47 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
48 '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
49 ('----- 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
50 '(?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
51 '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
52 ('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 ' +(?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
54 '------- 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
55 '^<(?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
56 '^(?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
57 '^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
58 '^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
59 '^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
60 '^ <(?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
61 '^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
62 '^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
63 '^ 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
64 ]
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
65 # 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
66 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
67 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
68 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
69 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
70 else:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
71 xl = []
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
72 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
73 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
74 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
75 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
76 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
77 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
78 del i
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
79
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
80 # 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
81 # 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
82 # 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
83 # 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
84 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
85 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
86 '<>\.\.\. (?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
87 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
88 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
89 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
90 ]
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
91 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
92 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
93 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
94 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
95 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
96 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
97 else:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
98 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
99 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
100 emails = []
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
101 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
102 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
103 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
104 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
105 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
106 try:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
107 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
108 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
109 pass
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
110 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
111 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
112 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
113 break
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
114 emails.append(res.group('email'))
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
115 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
116 else:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
117 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
118 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
119 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
120 try:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
121 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
122 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
123 pass
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
124 break
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
125 if not emails:
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
126 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
127 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
128 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
129 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
130 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
131 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
132 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
133 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
134 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
135 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
136 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
137 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
138 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
139 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
140 continue
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
141 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
142 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
143 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
144 break
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
145 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
146 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
147 return errors
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
148
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
149 EMPARSERS = [emparse_list, ]
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
150
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
151 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
152 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
153 b = int(b)
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
154 if a < b: return -1
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
155 elif a > b: return 1
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
156 else: return 0
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
157
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
158 def parsedir(dir, modify):
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
159 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
160 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
161 errordict = {}
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
162 errorfirst = {}
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
163 errorlast = {}
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
164 nok = nwarn = nbad = 0
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
165
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
166 # 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
167 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
168 files.sort(sort_numeric)
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11878
diff changeset
169
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
170 for fn in files:
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
171 # Lets try to parse the file.
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
172 fp = open(fn)
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
173 m = ErrorMessage(fp)
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
174 sender = m.getaddr('From')
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
175 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
176
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
177 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
178 fp.close()
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
179 print 'warning only'
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
180 nwarn = nwarn + 1
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
181 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
182 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
183 ## os.unlink(fn)
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
184 continue
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
185
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
186 try:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
187 errors = m.get_errors()
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
188 except Unparseable:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
189 print '** Not parseable'
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
190 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
191 fp.close()
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
192 continue
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
193 print len(errors), 'errors'
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
194
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
195 # Remember them
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
196 for e in errors:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
197 try:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
198 mm, dd = m.getdate('date')[1:1+2]
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
199 date = '%s %02d' % (calendar.month_abbr[mm], dd)
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
200 except:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
201 date = '??????'
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
202 if not errordict.has_key(e):
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
203 errordict[e] = 1
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
204 errorfirst[e] = '%s (%s)' % (fn, date)
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
205 else:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
206 errordict[e] = errordict[e] + 1
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
207 errorlast[e] = '%s (%s)' % (fn, date)
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
208
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
209 fp.close()
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
210 nok = nok + 1
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
211 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
212 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
213 ## os.unlink(fn)
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
214
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
215 print '--------------'
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
216 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
217 print nbad,'files unparseable'
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
218 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
219 list = []
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
220 for e in errordict.keys():
11878
3d73ffc105d0 Fix multi-arg append().
Guido van Rossum <guido@python.org>
parents: 9316
diff changeset
221 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
222 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
223 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
224 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
225
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
226 def main():
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
227 modify = 0
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
228 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
229 modify = 1
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
230 del sys.argv[1]
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
231 if len(sys.argv) > 1:
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
232 for folder in sys.argv[1:]:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
233 parsedir(folder, modify)
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
234 else:
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
235 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
236
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
237 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
238 main()