Skip to content

Commit

Permalink
Add insane.py to check if files in the insane directory are still con…
Browse files Browse the repository at this point in the history
…sidered insane
  • Loading branch information
Mortal committed May 2, 2015
1 parent ee99a63 commit 9218118
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions insane.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os
import email

from io import BytesIO, StringIO
from email.generator import Generator

import emailtunnel


def main():
for filename in sorted(os.listdir('insane')):
if filename.endswith('.in'):
with open('insane/%s' % filename, 'rb') as fp:
a = fp.read()
if a.startswith(b'From nobody'):
nl = a.index(b'\n')
a = a[nl+1:]
strip_from = True
else:
strip_from = False
message = email.message_from_string(a)

fp = BytesIO()
g = Generator(fp,
mangle_from_=False,
maxheaderlen=0)
g.flatten(message, unixfrom=False)
b = fp.getvalue()

a = a.rstrip(b'\n')
b = b.rstrip(b'\n')

a_s = emailtunnel.Message.sanity_strip(a)
b_s = emailtunnel.Message.sanity_strip(b)

after_stripping = ' after stripping From' if strip_from else ''
if a == b:
print("%s: OK -- identical%s" % (filename, after_stripping))
elif a_s == b_s:
print("%s: OK%s" % (filename, after_stripping))
elif len(a_s) != len(b_s):
print("%s: Different # lines" % (filename,))
o = next((i, al, bl) for i, (al, bl) in enumerate(zip(a_s, b_s)) if al != bl)
print(o)
else:
print("%s: Not OK" % (filename,))


if __name__ == "__main__":
main()

0 comments on commit 9218118

Please sign in to comment.