mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
More changes by Sjoerd & Jack
This commit is contained in:
parent
e47d5f9c52
commit
8e7a54f525
1 changed files with 136 additions and 42 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
import string
|
import string
|
||||||
import rfc822
|
import rfc822
|
||||||
|
import calendar
|
||||||
import regex
|
import regex
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
@ -17,21 +18,22 @@ class ErrorMessage(rfc822.Message):
|
||||||
if not sub:
|
if not sub:
|
||||||
return 0
|
return 0
|
||||||
sub = string.lower(sub)
|
sub = string.lower(sub)
|
||||||
if sub == 'waiting mail': return 1
|
if sub[:12] == 'waiting mail': return 1
|
||||||
if string.find(sub, 'warning') >= 0: return 1
|
if string.find(sub, 'warning') >= 0: return 1
|
||||||
|
self.sub = sub
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def get_errors(self):
|
def get_errors(self):
|
||||||
for p in EMPARSERS:
|
for p in EMPARSERS:
|
||||||
self.rewindbody()
|
self.rewindbody()
|
||||||
try:
|
try:
|
||||||
return p(self.fp)
|
return p(self.fp, self.sub)
|
||||||
except Unparseable:
|
except Unparseable:
|
||||||
pass
|
pass
|
||||||
raise Unparseable
|
raise Unparseable
|
||||||
|
|
||||||
sendmail_pattern = regex.compile('[0-9][0-9][0-9] ')
|
sendmail_pattern = regex.compile('[0-9][0-9][0-9] ')
|
||||||
def emparse_sendmail(fp):
|
def emparse_sendmail(fp, sub):
|
||||||
while 1:
|
while 1:
|
||||||
line = fp.readline()
|
line = fp.readline()
|
||||||
if not line:
|
if not line:
|
||||||
|
@ -60,7 +62,6 @@ def emparse_sendmail(fp):
|
||||||
line = line[:-1]
|
line = line[:-1]
|
||||||
if not line:
|
if not line:
|
||||||
continue
|
continue
|
||||||
found_a_line = 1
|
|
||||||
if sendmail_pattern.match(line) == 4:
|
if sendmail_pattern.match(line) == 4:
|
||||||
# Yes, an error/warning line. Ignore 4, remember 5, stop on rest
|
# Yes, an error/warning line. Ignore 4, remember 5, stop on rest
|
||||||
if line[0] == '5':
|
if line[0] == '5':
|
||||||
|
@ -72,12 +73,23 @@ def emparse_sendmail(fp):
|
||||||
line = string.split(line)
|
line = string.split(line)
|
||||||
if line and line[0][:3] == '---':
|
if line and line[0][:3] == '---':
|
||||||
break
|
break
|
||||||
|
found_a_line = 1
|
||||||
|
# special case for CWI sendmail
|
||||||
|
if len(line) > 1 and line[1] == 'Undelivered':
|
||||||
|
while 1:
|
||||||
|
line = fp.readline()
|
||||||
|
if not line:
|
||||||
|
break
|
||||||
|
line = string.strip(line)
|
||||||
|
if not line:
|
||||||
|
break
|
||||||
|
errors.append(line + ': ' + sub)
|
||||||
# Empty transcripts are ok, others without an error are not.
|
# Empty transcripts are ok, others without an error are not.
|
||||||
if found_a_line and not (errors or warnings):
|
if found_a_line and not (errors or warnings):
|
||||||
raise Unparseable
|
raise Unparseable
|
||||||
return errors
|
return errors
|
||||||
|
|
||||||
def emparse_cts(fp):
|
def emparse_cts(fp, sub):
|
||||||
while 1:
|
while 1:
|
||||||
line = fp.readline()
|
line = fp.readline()
|
||||||
if not line:
|
if not line:
|
||||||
|
@ -106,7 +118,7 @@ def emparse_cts(fp):
|
||||||
errors.append(line)
|
errors.append(line)
|
||||||
return errors
|
return errors
|
||||||
|
|
||||||
def emparse_aol(fp):
|
def emparse_aol(fp, sub):
|
||||||
while 1:
|
while 1:
|
||||||
line = fp.readline()
|
line = fp.readline()
|
||||||
if not line:
|
if not line:
|
||||||
|
@ -132,7 +144,7 @@ def emparse_aol(fp):
|
||||||
raise Unparseable
|
raise Unparseable
|
||||||
return errors
|
return errors
|
||||||
|
|
||||||
def emparse_compuserve(fp):
|
def emparse_compuserve(fp, sub):
|
||||||
while 1:
|
while 1:
|
||||||
line = fp.readline()
|
line = fp.readline()
|
||||||
if not line:
|
if not line:
|
||||||
|
@ -157,8 +169,7 @@ def emparse_compuserve(fp):
|
||||||
return errors
|
return errors
|
||||||
|
|
||||||
prov_pattern = regex.compile('.* | \(.*\)')
|
prov_pattern = regex.compile('.* | \(.*\)')
|
||||||
|
def emparse_providence(fp, sub):
|
||||||
def emparse_providence(fp):
|
|
||||||
while 1:
|
while 1:
|
||||||
line = fp.readline()
|
line = fp.readline()
|
||||||
if not line:
|
if not line:
|
||||||
|
@ -189,58 +200,141 @@ def emparse_providence(fp):
|
||||||
raise Unparseable
|
raise Unparseable
|
||||||
return errors
|
return errors
|
||||||
|
|
||||||
|
def emparse_x400(fp, sub):
|
||||||
|
exp = 'This report relates to your message:'
|
||||||
|
while 1:
|
||||||
|
line = fp.readline()
|
||||||
|
if not line:
|
||||||
|
raise Unparseable
|
||||||
|
line = line[:-1]
|
||||||
|
|
||||||
|
# Check that we're not in the returned message yet
|
||||||
|
if string.lower(line)[:5] == 'from:':
|
||||||
|
raise Unparseable
|
||||||
|
if line[:len(exp)] == exp:
|
||||||
|
break
|
||||||
|
|
||||||
|
errors = []
|
||||||
|
exp = 'Your message was not delivered to'
|
||||||
|
while 1:
|
||||||
|
line = fp.readline()
|
||||||
|
if not line:
|
||||||
|
break
|
||||||
|
line = line[:-1]
|
||||||
|
if not line:
|
||||||
|
continue
|
||||||
|
if line[:len(exp)] == exp:
|
||||||
|
error = string.strip(line[len(exp):])
|
||||||
|
sep = ': '
|
||||||
|
while 1:
|
||||||
|
line = fp.readline()
|
||||||
|
if not line:
|
||||||
|
break
|
||||||
|
line = line[:-1]
|
||||||
|
if not line:
|
||||||
|
break
|
||||||
|
if line[0] == ' ' and line[-1] != ':':
|
||||||
|
error = error + sep + string.strip(line)
|
||||||
|
sep = '; '
|
||||||
|
errors.append(error)
|
||||||
|
return errors
|
||||||
|
raise Unparseable
|
||||||
|
|
||||||
|
def emparse_passau(fp, sub):
|
||||||
|
exp = 'Unable to deliver message because'
|
||||||
|
while 1:
|
||||||
|
line = fp.readline()
|
||||||
|
if not line:
|
||||||
|
raise Unparseable
|
||||||
|
if string.lower(line)[:5] == 'from:':
|
||||||
|
raise Unparseable
|
||||||
|
if line[:len(exp)] == exp:
|
||||||
|
break
|
||||||
|
|
||||||
|
errors = []
|
||||||
|
exp = 'Returned Text follows'
|
||||||
|
while 1:
|
||||||
|
line = fp.readline()
|
||||||
|
if not line:
|
||||||
|
raise Unparseable
|
||||||
|
line = line[:-1]
|
||||||
|
# Check that we're not in the returned message yet
|
||||||
|
if string.lower(line)[:5] == 'from:':
|
||||||
|
raise Unparseable
|
||||||
|
if not line:
|
||||||
|
continue
|
||||||
|
if line[:len(exp)] == exp:
|
||||||
|
return errors
|
||||||
|
errors.append(string.strip(line))
|
||||||
|
|
||||||
EMPARSERS = [emparse_sendmail, emparse_aol, emparse_cts, emparse_compuserve,
|
EMPARSERS = [emparse_sendmail, emparse_aol, emparse_cts, emparse_compuserve,
|
||||||
emparse_providence]
|
emparse_providence, emparse_x400, emparse_passau]
|
||||||
|
|
||||||
|
def sort_numeric(a, b):
|
||||||
|
a = string.atoi(a)
|
||||||
|
b = string.atoi(b)
|
||||||
|
if a < b: return -1
|
||||||
|
elif a > b: return 1
|
||||||
|
else: return 0
|
||||||
|
|
||||||
def parsedir(dir, modify):
|
def parsedir(dir, modify):
|
||||||
os.chdir(dir)
|
os.chdir(dir)
|
||||||
files = os.listdir('.')
|
|
||||||
pat = regex.compile('^[0-9]*$')
|
pat = regex.compile('^[0-9]*$')
|
||||||
errordict = {}
|
errordict = {}
|
||||||
|
errorfirst = {}
|
||||||
errorlast = {}
|
errorlast = {}
|
||||||
nok = nwarn = nbad = 0
|
nok = nwarn = nbad = 0
|
||||||
|
|
||||||
|
# find all numeric file names and sort them
|
||||||
|
files = filter(lambda fn, pat=pat: pat.match(fn) > 0, os.listdir('.'))
|
||||||
|
files.sort(sort_numeric)
|
||||||
|
|
||||||
for fn in files:
|
for fn in files:
|
||||||
if pat.match(fn) > 0:
|
# Lets try to parse the file.
|
||||||
# Ok, so it's a numeric filename. Lets try to parse it.
|
fp = open(fn)
|
||||||
fp = open(fn)
|
m = ErrorMessage(fp)
|
||||||
m = ErrorMessage(fp)
|
sender = m.getaddr('From')
|
||||||
sender = m.getaddr('From')
|
print '%s\t%-40s\t'%(fn, sender[1]),
|
||||||
print '%s\t%-40s\t'%(fn, sender[1]),
|
|
||||||
|
|
||||||
if m.is_warning():
|
if m.is_warning():
|
||||||
print 'warning only'
|
print 'warning only'
|
||||||
nwarn = nwarn + 1
|
nwarn = nwarn + 1
|
||||||
if modify:
|
|
||||||
os.unlink(fn)
|
|
||||||
continue
|
|
||||||
|
|
||||||
try:
|
|
||||||
errors = m.get_errors()
|
|
||||||
except Unparseable:
|
|
||||||
print '** Not parseable'
|
|
||||||
nbad = nbad + 1
|
|
||||||
continue
|
|
||||||
print len(errors), 'errors'
|
|
||||||
|
|
||||||
# Remember them
|
|
||||||
for e in errors:
|
|
||||||
if not errordict.has_key(e):
|
|
||||||
errordict[e] = 1
|
|
||||||
else:
|
|
||||||
errordict[e] = errordict[e] + 1
|
|
||||||
errorlast[e] = fn
|
|
||||||
|
|
||||||
nok = nok + 1
|
|
||||||
if modify:
|
if modify:
|
||||||
os.unlink(fn)
|
os.unlink(fn)
|
||||||
|
continue
|
||||||
|
|
||||||
|
try:
|
||||||
|
errors = m.get_errors()
|
||||||
|
except Unparseable:
|
||||||
|
print '** Not parseable'
|
||||||
|
nbad = nbad + 1
|
||||||
|
continue
|
||||||
|
print len(errors), 'errors'
|
||||||
|
|
||||||
|
# Remember them
|
||||||
|
for e in errors:
|
||||||
|
try:
|
||||||
|
mm, dd = m.getdate('date')[1:1+2]
|
||||||
|
date = '%s %02d' % (calendar.month_abbr[mm], dd)
|
||||||
|
except:
|
||||||
|
date = '??????'
|
||||||
|
if not errordict.has_key(e):
|
||||||
|
errordict[e] = 1
|
||||||
|
errorfirst[e] = '%s (%s)' % (fn, date)
|
||||||
|
else:
|
||||||
|
errordict[e] = errordict[e] + 1
|
||||||
|
errorlast[e] = '%s (%s)' % (fn, date)
|
||||||
|
|
||||||
|
nok = nok + 1
|
||||||
|
if modify:
|
||||||
|
os.unlink(fn)
|
||||||
|
|
||||||
print '--------------'
|
print '--------------'
|
||||||
print nok, 'files parsed,',nwarn,'files warning-only,',
|
print nok, 'files parsed,',nwarn,'files warning-only,',
|
||||||
print nbad,'files unparseable'
|
print nbad,'files unparseable'
|
||||||
print '--------------'
|
print '--------------'
|
||||||
for e in errordict.keys():
|
for e in errordict.keys():
|
||||||
print errordict[e], '\t', errorlast[e], '\t', e
|
print errordict[e], errorfirst[e], '-', errorlast[e], '\t', e
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
modify = 0
|
modify = 0
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue