1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
6 from io import StringIO
7 from config import config
12 if s.__class__ == ''.__class__:
13 return s.encode('iso-8859-1', 'replace').decode('us-ascii', 'replace')
15 return s.decode('us-ascii', 'replace')
20 self.body = StringIO()
21 self.set_std_headers()
23 def set_header(self, n, v):
26 def set_headers(self, to = None, cc = None, subject = None):
28 self.set_header("To", to)
30 self.set_header("Cc", cc)
32 self.set_header("Subject", subject)
34 def remove_header(self, n):
38 def write_line(self, l):
39 self.body.write(recode("%s\n" % l))
42 self.body.write(recode(s))
44 def append_log(self, log):
55 if line < 100 or line > line_cnt - 100:
56 self.body.write(recode(l))
57 if line == line_cnt - 100:
58 self.body.write("\n\n[...]\n\n")
61 shutil.copyfileobj(open(log), self.body)
63 def set_std_headers(self):
64 self.headers["Date"] = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
65 self.headers["Message-ID"] = "<tld-builder.%f.%d@%s>" \
66 % (time.time(), os.getpid(), os.uname()[1])
67 self.headers["From"] = "TLD %s builder <%s>" \
68 % (config.builder, config.email)
69 self.headers["X-TLD-Builder"] = config.builder
71 def write_to(self, f):
72 for k, v in self.headers.items():
73 f.write("%s: %s\n" % (k, v))
76 shutil.copyfileobj(self.body, f)
79 if not os.path.exists("/usr/lib/sendmail"):
81 log.alert("/usr/lib/sendmail doesn't exist: Can't send email")
83 send_sendmail = "/usr/lib/sendmail -i -t -f %s" % config.admin_email
84 f = os.popen(send_sendmail, "w")
88 log.alert("sending email message failed: %s" % e)