X-Git-Url: https://git.tld-linux.org/?a=blobdiff_plain;f=TLD_Builder%2Fmailer.py;h=7725a60710f76576f3fff2b1022c2b0004c1a012;hb=b06ac0a74084c2ed513ecead310ea47208344d14;hp=f1459faf5c50f4e3cf377deda135e9e08d9d8f85;hpb=b999f53d4bf5d44586ecf028876e8bc20b5fd2ce;p=tld-builder.git diff --git a/TLD_Builder/mailer.py b/TLD_Builder/mailer.py index f1459fa..7725a60 100644 --- a/TLD_Builder/mailer.py +++ b/TLD_Builder/mailer.py @@ -3,7 +3,10 @@ import time import os import sys -import StringIO +if sys.version_info[0] == 2: + import StringIO +else: + from io import StringIO from config import config import util @@ -11,14 +14,14 @@ import log def recode(s): if s.__class__ == ''.__class__: - return s.decode('iso-8859-1', 'replace').encode('us-ascii', 'replace') + return s.encode('iso-8859-1', 'replace').decode('us-ascii', 'replace') else: - return s.encode('us-ascii', 'replace') + return s.decode('us-ascii', 'replace') class Message: def __init__(self): self.headers = {} - self.body = StringIO.StringIO() + self.body = StringIO() self.set_std_headers() def set_header(self, n, v): @@ -48,11 +51,11 @@ class Message: # just head and tail f = open(log) line_cnt = 0 - for l in f.xreadlines(): + for l in f: line_cnt += 1 f.seek(0) line = 0 - for l in f.xreadlines(): + for l in f: if line < 100 or line > line_cnt - 100: self.body.write(recode(l)) if line == line_cnt - 100: @@ -85,7 +88,7 @@ class Message: f = os.popen(send_sendmail, "w") try: self.write_to(f) - except IOError, e: + except IOError as e: log.alert("sending email message failed: %s" % e) f.close() return False