annotate Tools/scripts/mailerdaemon.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 65d5ecefd50d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
71557
65d5ecefd50d Fix missing or wrong shebangs and missing executable bits for scripts (#10318)
Éric Araujo <merwok@netwok.org>
parents: 71556
diff changeset
1 #!/usr/bin/env python3
71556
94b1771dbe4b Fix string exception and a few style issues in mailerdaemon script
Éric Araujo <merwok@netwok.org>
parents: 47657
diff changeset
2 """Classes to parse mailer-daemon messages."""
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
3
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
4 import calendar
47657
109e9748c7f4 Remove traces of rfc822.
Georg Brandl <georg@python.org>
parents: 47635
diff changeset
5 import email.message
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
71556
94b1771dbe4b Fix string exception and a few style issues in mailerdaemon script
Éric Araujo <merwok@netwok.org>
parents: 47657
diff changeset
10
94b1771dbe4b Fix string exception and a few style issues in mailerdaemon script
Éric Araujo <merwok@netwok.org>
parents: 47657
diff changeset
11 class Unparseable(Exception):
94b1771dbe4b Fix string exception and a few style issues in mailerdaemon script
Éric Araujo <merwok@netwok.org>
parents: 47657
diff changeset
12 pass
94b1771dbe4b Fix string exception and a few style issues in mailerdaemon script
Éric Araujo <merwok@netwok.org>
parents: 47657
diff changeset
13
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
14
47657
109e9748c7f4 Remove traces of rfc822.
Georg Brandl <georg@python.org>
parents: 47635
diff changeset
15 class ErrorMessage(email.message.Message):
109e9748c7f4 Remove traces of rfc822.
Georg Brandl <georg@python.org>
parents: 47635
diff changeset
16 def __init__(self):
109e9748c7f4 Remove traces of rfc822.
Georg Brandl <georg@python.org>
parents: 47635
diff changeset
17 email.message.Message.__init__(self)
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
18 self.sub = ''
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
19
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
20 def is_warning(self):
47635
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47100
diff changeset
21 sub = self.get('Subject')
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
22 if not sub:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
23 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
24 sub = sub.lower()
71556
94b1771dbe4b Fix string exception and a few style issues in mailerdaemon script
Éric Araujo <merwok@netwok.org>
parents: 47657
diff changeset
25 if sub.startswith('waiting mail'):
94b1771dbe4b Fix string exception and a few style issues in mailerdaemon script
Éric Araujo <merwok@netwok.org>
parents: 47657
diff changeset
26 return 1
94b1771dbe4b Fix string exception and a few style issues in mailerdaemon script
Éric Araujo <merwok@netwok.org>
parents: 47657
diff changeset
27 if 'warning' in sub:
94b1771dbe4b Fix string exception and a few style issues in mailerdaemon script
Éric Araujo <merwok@netwok.org>
parents: 47657
diff changeset
28 return 1
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
29 self.sub = sub
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
30 return 0
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
31
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
32 def get_errors(self):
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
33 for p in EMPARSERS:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
34 self.rewindbody()
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
35 try:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
36 return p(self.fp, self.sub)
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
37 except Unparseable:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
38 pass
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
39 raise Unparseable
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
40
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
41 # 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
42 # 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
43 # 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
44 # (?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
45 # 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
46 # 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
47 # 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
48 # 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
49 # 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
50 # 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
51 # 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
52 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
53 '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
54 ('----- 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
55 '(?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
56 '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
57 ('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
58 ' +(?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
59 '------- 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
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 '^(?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
62 '^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
63 '^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
64 '^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
65 '^ <(?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
66 '^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
67 '^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
68 '^ 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
69 ]
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
70 # 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
71 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
72 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
73 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
74 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
75 else:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
76 xl = []
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
77 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
78 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
79 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
80 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
81 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
82 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
83 del i
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
84
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
85 # 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
86 # 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
87 # 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
88 # 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
89 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
90 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
91 '<>\.\.\. (?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
92 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
93 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
94 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
95 ]
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
96 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
97 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
98 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
99 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
100 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
101 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
102 else:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
103 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
104 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
105 emails = []
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 = None
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
107 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
108 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
109 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
110 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
111 try:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
112 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
113 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
114 pass
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
115 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
116 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
117 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
118 break
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'))
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
120 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
121 else:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
122 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
123 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
124 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
125 try:
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
126 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
127 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
128 pass
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
129 break
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
130 if not emails:
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
131 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
132 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
133 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
134 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
135 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
136 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
137 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
138 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
139 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
140 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
141 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
142 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
143 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
144 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
145 continue
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
146 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
147 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
148 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
149 break
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
150 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
151 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
152 return errors
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
153
71556
94b1771dbe4b Fix string exception and a few style issues in mailerdaemon script
Éric Araujo <merwok@netwok.org>
parents: 47657
diff changeset
154 EMPARSERS = [emparse_list]
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
155
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
156 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
157 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
158 b = int(b)
71556
94b1771dbe4b Fix string exception and a few style issues in mailerdaemon script
Éric Araujo <merwok@netwok.org>
parents: 47657
diff changeset
159 if a < b:
94b1771dbe4b Fix string exception and a few style issues in mailerdaemon script
Éric Araujo <merwok@netwok.org>
parents: 47657
diff changeset
160 return -1
94b1771dbe4b Fix string exception and a few style issues in mailerdaemon script
Éric Araujo <merwok@netwok.org>
parents: 47657
diff changeset
161 elif a > b:
94b1771dbe4b Fix string exception and a few style issues in mailerdaemon script
Éric Araujo <merwok@netwok.org>
parents: 47657
diff changeset
162 return 1
94b1771dbe4b Fix string exception and a few style issues in mailerdaemon script
Éric Araujo <merwok@netwok.org>
parents: 47657
diff changeset
163 else:
94b1771dbe4b Fix string exception and a few style issues in mailerdaemon script
Éric Araujo <merwok@netwok.org>
parents: 47657
diff changeset
164 return 0
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
165
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
166 def parsedir(dir, modify):
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
167 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
168 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
169 errordict = {}
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
170 errorfirst = {}
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
171 errorlast = {}
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
172 nok = nwarn = nbad = 0
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
173
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
174 # find all numeric file names and sort them
47100
893a4dc17a91 Ran 2to3 over scripts directory.
Georg Brandl <georg@python.org>
parents: 42427
diff changeset
175 files = list(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
176 files.sort(sort_numeric)
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11878
diff changeset
177
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
178 for fn in files:
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
179 # Lets try to parse the file.
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
180 fp = open(fn)
47657
109e9748c7f4 Remove traces of rfc822.
Georg Brandl <georg@python.org>
parents: 47635
diff changeset
181 m = email.message_from_file(fp, _class=ErrorMessage)
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
182 sender = m.getaddr('From')
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 25219
diff changeset
183 print('%s\t%-40s\t'%(fn, sender[1]), end=' ')
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
184
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
185 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
186 fp.close()
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 25219
diff changeset
187 print('warning only')
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
188 nwarn = nwarn + 1
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
189 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
190 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
191 ## os.unlink(fn)
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
192 continue
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 try:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
195 errors = m.get_errors()
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
196 except Unparseable:
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 25219
diff changeset
197 print('** Not parseable')
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
198 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
199 fp.close()
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
200 continue
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 25219
diff changeset
201 print(len(errors), 'errors')
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
202
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
203 # Remember them
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
204 for e in errors:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
205 try:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
206 mm, dd = m.getdate('date')[1:1+2]
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
207 date = '%s %02d' % (calendar.month_abbr[mm], dd)
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
208 except:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
209 date = '??????'
47100
893a4dc17a91 Ran 2to3 over scripts directory.
Georg Brandl <georg@python.org>
parents: 42427
diff changeset
210 if e not in errordict:
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
211 errordict[e] = 1
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
212 errorfirst[e] = '%s (%s)' % (fn, date)
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
213 else:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
214 errordict[e] = errordict[e] + 1
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
215 errorlast[e] = '%s (%s)' % (fn, date)
3780
ac114a5fc8ff More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents: 3251
diff changeset
216
9316
f33b394e5d7f New version that is much more compact and easier to maintain. (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 7735
diff changeset
217 fp.close()
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
218 nok = nok + 1
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
219 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
220 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
221 ## os.unlink(fn)
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
222
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 25219
diff changeset
223 print('--------------')
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 25219
diff changeset
224 print(nok, 'files parsed,',nwarn,'files warning-only,', end=' ')
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 25219
diff changeset
225 print(nbad,'files unparseable')
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 25219
diff changeset
226 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
227 list = []
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
228 for e in errordict.keys():
11878
3d73ffc105d0 Fix multi-arg append().
Guido van Rossum <guido@python.org>
parents: 9316
diff changeset
229 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
230 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
231 for num, first, last, e in list:
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 25219
diff changeset
232 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
233
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
234 def main():
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
235 modify = 0
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
236 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
237 modify = 1
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
238 del sys.argv[1]
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
239 if len(sys.argv) > 1:
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
240 for folder in sys.argv[1:]:
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
241 parsedir(folder, modify)
3222
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
242 else:
7735
7a9afaba88d8 give in to tab police
Guido van Rossum <guido@python.org>
parents: 3780
diff changeset
243 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
244
0f38cec56539 Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
245 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
246 main()