Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/mailerdaemon.py @ 9281:809dcc1bbc21 legacy-trunk
Added #! line
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 07 Oct 1998 14:46:09 +0000 |
| parents | 7a9afaba88d8 |
| children | f33b394e5d7f |
| 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 string |
|
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 |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
6 import regex |
|
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 | 14 rfc822.Message.__init__(self, fp) |
|
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 | 17 sub = self.getheader('Subject') |
| 18 if not sub: | |
| 19 return 0 | |
| 20 sub = string.lower(sub) | |
| 21 if sub[:12] == 'waiting mail': return 1 | |
| 22 if string.find(sub, 'warning') >= 0: return 1 | |
| 23 self.sub = sub | |
| 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 | 27 for p in EMPARSERS: |
| 28 self.rewindbody() | |
| 29 try: | |
| 30 return p(self.fp, self.sub) | |
| 31 except Unparseable: | |
| 32 pass | |
| 33 raise Unparseable | |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
34 |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
35 sendmail_pattern = regex.compile('[0-9][0-9][0-9] ') |
|
3780
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
36 def emparse_sendmail(fp, sub): |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
37 while 1: |
| 7735 | 38 line = fp.readline() |
| 39 if not line: | |
| 40 raise Unparseable | |
| 41 line = line[:-1] | |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
42 |
| 7735 | 43 # Check that we're not in the returned message yet |
| 44 if string.lower(line)[:5] == 'from:': | |
| 45 raise Unparseable | |
| 46 line = string.split(line) | |
| 47 if len(line) > 3 and \ | |
| 48 ((line[0] == '-----' and line[1] == 'Transcript') or | |
| 49 (line[0] == '---' and line[1] == 'The' and | |
| 50 line[2] == 'transcript') or | |
| 51 (line[0] == 'While' and | |
| 52 line[1] == 'talking' and | |
| 53 line[2] == 'to')): | |
| 54 # Yes, found it! | |
| 55 break | |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
56 |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
57 errors = [] |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
58 found_a_line = 0 |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
59 warnings = 0 |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
60 while 1: |
| 7735 | 61 line = fp.readline() |
| 62 if not line: | |
| 63 break | |
| 64 line = line[:-1] | |
| 65 if not line: | |
| 66 continue | |
| 67 if sendmail_pattern.match(line) == 4: | |
| 68 # Yes, an error/warning line. Ignore 4, remember 5, stop on rest | |
| 69 if line[0] == '5': | |
| 70 errors.append(line) | |
| 71 elif line[0] == '4': | |
| 72 warnings = 1 | |
| 73 else: | |
| 74 raise Unparseable | |
| 75 line = string.split(line) | |
| 76 if line and line[0][:3] == '---': | |
| 77 break | |
| 78 found_a_line = 1 | |
|
3780
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
79 # special case for CWI sendmail |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
80 if len(line) > 1 and line[1] == 'Undelivered': |
| 7735 | 81 while 1: |
| 82 line = fp.readline() | |
| 83 if not line: | |
| 84 break | |
| 85 line = string.strip(line) | |
| 86 if not line: | |
| 87 break | |
| 88 errors.append(line + ': ' + sub) | |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
89 # Empty transcripts are ok, others without an error are not. |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
90 if found_a_line and not (errors or warnings): |
| 7735 | 91 raise Unparseable |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
92 return errors |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
93 |
|
3780
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
94 def emparse_cts(fp, sub): |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
95 while 1: |
| 7735 | 96 line = fp.readline() |
| 97 if not line: | |
| 98 raise Unparseable | |
| 99 line = line[:-1] | |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
100 |
| 7735 | 101 # Check that we're not in the returned message yet |
| 102 if string.lower(line)[:5] == 'from:': | |
| 103 raise Unparseable | |
| 104 line = string.split(line) | |
| 105 if len(line) > 3 and line[0][:2] == '|-' and line[1] == 'Failed' \ | |
| 106 and line[2] == 'addresses': | |
| 107 # Yes, found it! | |
| 108 break | |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
109 |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
110 errors = [] |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
111 while 1: |
| 7735 | 112 line = fp.readline() |
| 113 if not line: | |
| 114 break | |
| 115 line = line[:-1] | |
| 116 if not line: | |
| 117 continue | |
| 118 if line[:2] == '|-': | |
| 119 break | |
| 120 errors.append(line) | |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
121 return errors |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
122 |
|
3780
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
123 def emparse_aol(fp, sub): |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
124 while 1: |
| 7735 | 125 line = fp.readline() |
| 126 if not line: | |
| 127 raise Unparseable | |
| 128 line = line[:-1] | |
| 129 if line: | |
| 130 break | |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
131 exp = 'The mail you sent could not be delivered to:' |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
132 if line[:len(exp)] != exp: |
| 7735 | 133 raise Unparseable |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
134 errors = [] |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
135 while 1: |
| 7735 | 136 line = fp.readline() |
| 137 if sendmail_pattern.match(line) == 4: | |
| 138 # Yes, an error/warning line. Ignore 4, remember 5, stop on rest | |
| 139 if line[0] == '5': | |
| 140 errors.append(line) | |
| 141 elif line[0] != '4': | |
| 142 raise Unparseable | |
| 143 elif line == '\n': | |
| 144 break | |
| 145 else: | |
| 146 raise Unparseable | |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
147 return errors |
|
3238
d3510f1118b6
Added (limited) compuserve error parsing
Jack Jansen <jack.jansen@cwi.nl>
parents:
3222
diff
changeset
|
148 |
|
3780
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
149 def emparse_compuserve(fp, sub): |
|
3238
d3510f1118b6
Added (limited) compuserve error parsing
Jack Jansen <jack.jansen@cwi.nl>
parents:
3222
diff
changeset
|
150 while 1: |
| 7735 | 151 line = fp.readline() |
| 152 if not line: | |
| 153 raise Unparseable | |
| 154 line = line[:-1] | |
| 155 if line: | |
| 156 break | |
|
3238
d3510f1118b6
Added (limited) compuserve error parsing
Jack Jansen <jack.jansen@cwi.nl>
parents:
3222
diff
changeset
|
157 exp = 'Your message could not be delivered for the following reason:' |
|
d3510f1118b6
Added (limited) compuserve error parsing
Jack Jansen <jack.jansen@cwi.nl>
parents:
3222
diff
changeset
|
158 if line[:len(exp)] != exp: |
| 7735 | 159 raise Unparseable |
|
3238
d3510f1118b6
Added (limited) compuserve error parsing
Jack Jansen <jack.jansen@cwi.nl>
parents:
3222
diff
changeset
|
160 errors = [] |
|
d3510f1118b6
Added (limited) compuserve error parsing
Jack Jansen <jack.jansen@cwi.nl>
parents:
3222
diff
changeset
|
161 while 1: |
| 7735 | 162 line = fp.readline() |
| 163 if not line: break | |
| 164 if line[:3] == '---': break | |
| 165 line = line[:-1] | |
| 166 if not line: continue | |
| 167 if line == 'Please resend your message at a later time.': | |
| 168 continue | |
| 169 line = 'Compuserve: ' + line | |
| 170 errors.append(line) | |
|
3238
d3510f1118b6
Added (limited) compuserve error parsing
Jack Jansen <jack.jansen@cwi.nl>
parents:
3222
diff
changeset
|
171 return errors |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
172 |
|
3239
285dba4e0e51
Added providence mailer support
Jack Jansen <jack.jansen@cwi.nl>
parents:
3238
diff
changeset
|
173 prov_pattern = regex.compile('.* | \(.*\)') |
|
3780
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
174 def emparse_providence(fp, sub): |
|
3239
285dba4e0e51
Added providence mailer support
Jack Jansen <jack.jansen@cwi.nl>
parents:
3238
diff
changeset
|
175 while 1: |
| 7735 | 176 line = fp.readline() |
| 177 if not line: | |
| 178 raise Unparseable | |
| 179 line = line[:-1] | |
|
3239
285dba4e0e51
Added providence mailer support
Jack Jansen <jack.jansen@cwi.nl>
parents:
3238
diff
changeset
|
180 |
| 7735 | 181 # Check that we're not in the returned message yet |
| 182 if string.lower(line)[:5] == 'from:': | |
| 183 raise Unparseable | |
| 184 exp = 'The following errors occurred' | |
| 185 if line[:len(exp)] == exp: | |
| 186 break | |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
187 |
|
3239
285dba4e0e51
Added providence mailer support
Jack Jansen <jack.jansen@cwi.nl>
parents:
3238
diff
changeset
|
188 errors = [] |
|
285dba4e0e51
Added providence mailer support
Jack Jansen <jack.jansen@cwi.nl>
parents:
3238
diff
changeset
|
189 while 1: |
| 7735 | 190 line = fp.readline() |
| 191 if not line: | |
| 192 break | |
| 193 line = line[:-1] | |
| 194 if not line: | |
| 195 continue | |
| 196 if line[:4] == '----': | |
| 197 break | |
| 198 if prov_pattern.match(line) > 0: | |
| 199 errors.append(prov_pattern.group(1)) | |
|
3239
285dba4e0e51
Added providence mailer support
Jack Jansen <jack.jansen@cwi.nl>
parents:
3238
diff
changeset
|
200 |
|
285dba4e0e51
Added providence mailer support
Jack Jansen <jack.jansen@cwi.nl>
parents:
3238
diff
changeset
|
201 if not errors: |
| 7735 | 202 raise Unparseable |
|
3239
285dba4e0e51
Added providence mailer support
Jack Jansen <jack.jansen@cwi.nl>
parents:
3238
diff
changeset
|
203 return errors |
|
285dba4e0e51
Added providence mailer support
Jack Jansen <jack.jansen@cwi.nl>
parents:
3238
diff
changeset
|
204 |
|
3780
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
205 def emparse_x400(fp, sub): |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
206 exp = 'This report relates to your message:' |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
207 while 1: |
| 7735 | 208 line = fp.readline() |
| 209 if not line: | |
| 210 raise Unparseable | |
| 211 line = line[:-1] | |
|
3780
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
212 |
| 7735 | 213 # Check that we're not in the returned message yet |
| 214 if string.lower(line)[:5] == 'from:': | |
| 215 raise Unparseable | |
| 216 if line[:len(exp)] == exp: | |
| 217 break | |
|
3780
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
218 |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
219 errors = [] |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
220 exp = 'Your message was not delivered to' |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
221 while 1: |
| 7735 | 222 line = fp.readline() |
| 223 if not line: | |
| 224 break | |
| 225 line = line[:-1] | |
| 226 if not line: | |
| 227 continue | |
| 228 if line[:len(exp)] == exp: | |
| 229 error = string.strip(line[len(exp):]) | |
| 230 sep = ': ' | |
| 231 while 1: | |
| 232 line = fp.readline() | |
| 233 if not line: | |
| 234 break | |
| 235 line = line[:-1] | |
| 236 if not line: | |
| 237 break | |
| 238 if line[0] == ' ' and line[-1] != ':': | |
| 239 error = error + sep + string.strip(line) | |
| 240 sep = '; ' | |
| 241 errors.append(error) | |
| 242 return errors | |
|
3780
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
243 raise Unparseable |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
244 |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
245 def emparse_passau(fp, sub): |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
246 exp = 'Unable to deliver message because' |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
247 while 1: |
| 7735 | 248 line = fp.readline() |
| 249 if not line: | |
| 250 raise Unparseable | |
| 251 if string.lower(line)[:5] == 'from:': | |
| 252 raise Unparseable | |
| 253 if line[:len(exp)] == exp: | |
| 254 break | |
|
3780
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
255 |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
256 errors = [] |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
257 exp = 'Returned Text follows' |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
258 while 1: |
| 7735 | 259 line = fp.readline() |
| 260 if not line: | |
| 261 raise Unparseable | |
| 262 line = line[:-1] | |
| 263 # Check that we're not in the returned message yet | |
| 264 if string.lower(line)[:5] == 'from:': | |
| 265 raise Unparseable | |
| 266 if not line: | |
| 267 continue | |
| 268 if line[:len(exp)] == exp: | |
| 269 return errors | |
| 270 errors.append(string.strip(line)) | |
|
3780
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
271 |
|
3239
285dba4e0e51
Added providence mailer support
Jack Jansen <jack.jansen@cwi.nl>
parents:
3238
diff
changeset
|
272 EMPARSERS = [emparse_sendmail, emparse_aol, emparse_cts, emparse_compuserve, |
| 7735 | 273 emparse_providence, emparse_x400, emparse_passau] |
|
3780
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
274 |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
275 def sort_numeric(a, b): |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
276 a = string.atoi(a) |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
277 b = string.atoi(b) |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
278 if a < b: return -1 |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
279 elif a > b: return 1 |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
280 else: return 0 |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
281 |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
282 def parsedir(dir, modify): |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
283 os.chdir(dir) |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
284 pat = regex.compile('^[0-9]*$') |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
285 errordict = {} |
|
3780
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
286 errorfirst = {} |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
287 errorlast = {} |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
288 nok = nwarn = nbad = 0 |
|
3780
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
289 |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
290 # find all numeric file names and sort them |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
291 files = filter(lambda fn, pat=pat: pat.match(fn) > 0, os.listdir('.')) |
|
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
292 files.sort(sort_numeric) |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
293 |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
294 for fn in files: |
| 7735 | 295 # Lets try to parse the file. |
| 296 fp = open(fn) | |
| 297 m = ErrorMessage(fp) | |
| 298 sender = m.getaddr('From') | |
| 299 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
|
300 |
| 7735 | 301 if m.is_warning(): |
| 302 print 'warning only' | |
| 303 nwarn = nwarn + 1 | |
| 304 if modify: | |
| 305 os.unlink(fn) | |
| 306 continue | |
|
3780
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
307 |
| 7735 | 308 try: |
| 309 errors = m.get_errors() | |
| 310 except Unparseable: | |
| 311 print '** Not parseable' | |
| 312 nbad = nbad + 1 | |
| 313 continue | |
| 314 print len(errors), 'errors' | |
|
3780
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
315 |
| 7735 | 316 # Remember them |
| 317 for e in errors: | |
| 318 try: | |
| 319 mm, dd = m.getdate('date')[1:1+2] | |
| 320 date = '%s %02d' % (calendar.month_abbr[mm], dd) | |
| 321 except: | |
| 322 date = '??????' | |
| 323 if not errordict.has_key(e): | |
| 324 errordict[e] = 1 | |
| 325 errorfirst[e] = '%s (%s)' % (fn, date) | |
| 326 else: | |
| 327 errordict[e] = errordict[e] + 1 | |
| 328 errorlast[e] = '%s (%s)' % (fn, date) | |
|
3780
ac114a5fc8ff
More changes by Sjoerd & Jack
Guido van Rossum <guido@python.org>
parents:
3251
diff
changeset
|
329 |
| 7735 | 330 nok = nok + 1 |
| 331 if modify: | |
| 332 os.unlink(fn) | |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
333 |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
334 print '--------------' |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
335 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
|
336 print nbad,'files unparseable' |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
337 print '--------------' |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
338 for e in errordict.keys(): |
| 7735 | 339 print errordict[e], errorfirst[e], '-', errorlast[e], '\t', e |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
340 |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
341 def main(): |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
342 modify = 0 |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
343 if len(sys.argv) > 1 and sys.argv[1] == '-d': |
| 7735 | 344 modify = 1 |
| 345 del sys.argv[1] | |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
346 if len(sys.argv) > 1: |
| 7735 | 347 for folder in sys.argv[1:]: |
| 348 parsedir(folder, modify) | |
|
3222
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
349 else: |
| 7735 | 350 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
|
351 |
|
0f38cec56539
Script to read and parse error messages from mailer daemons.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
352 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
|
353 main() |
