X-Git-Url: https://git.tld-linux.org/?a=blobdiff_plain;f=TLD_Builder%2Fmailer.py;h=764bdf416d8845ad3e15fcb99a77339817f8a0e4;hb=a68953c88fba6e657febf9cb06273f44288f759c;hp=f1459faf5c50f4e3cf377deda135e9e08d9d8f85;hpb=b999f53d4bf5d44586ecf028876e8bc20b5fd2ce;p=tld-builder.git diff --git a/TLD_Builder/mailer.py b/TLD_Builder/mailer.py index f1459fa..764bdf4 100644 --- a/TLD_Builder/mailer.py +++ b/TLD_Builder/mailer.py @@ -3,22 +3,21 @@ import time import os import sys -import StringIO - +from io import StringIO from config import config -import util +import shutil 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,18 +47,18 @@ 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: self.body.write("\n\n[...]\n\n") line += 1 else: - util.sendfile(open(log), self.body) + shutil.copyfileobj(open(log), self.body) def set_std_headers(self): self.headers["Date"] = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) @@ -74,7 +73,7 @@ class Message: f.write("%s: %s\n" % (k, v)) f.write("\n") self.body.seek(0) - util.sendfile(self.body, f) + shutil.copyfileobj(self.body, f) def send(self): if not os.path.exists("/usr/lib/sendmail"): @@ -85,7 +84,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