1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
6 from io import StringIO
7 from config import config
15 self.body = StringIO()
16 self.set_std_headers()
18 def set_header(self, n, v):
21 def set_headers(self, to = None, cc = None, subject = None):
23 self.set_header("To", to)
25 self.set_header("Cc", cc)
27 self.set_header("Subject", subject)
29 def remove_header(self, n):
33 def write_line(self, l):
34 self.body.write(util.to_str("%s\n" % l))
37 self.body.write(util.to_str(s))
39 def append_log(self, log):
50 if line < 100 or line > line_cnt - 100:
51 self.body.write(util.to_str(l))
52 if line == line_cnt - 100:
53 self.body.write("\n\n[...]\n\n")
56 shutil.copyfileobj(open(log), self.body)
58 def set_std_headers(self):
59 self.headers["Date"] = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
60 self.headers["Message-ID"] = "<tld-builder.%f.%d@%s>" \
61 % (time.time(), os.getpid(), os.uname()[1])
62 self.headers["From"] = "TLD %s builder <%s>" \
63 % (config.builder, config.email)
64 self.headers["X-TLD-Builder"] = config.builder
66 def write_to(self, f):
67 for k, v in self.headers.items():
68 f.write("%s: %s\n" % (k, v))
71 shutil.copyfileobj(self.body, f)
74 if not os.path.exists("/usr/lib/sendmail"):
76 log.alert("/usr/lib/sendmail doesn't exist: Can't send email")
78 send_sendmail = "/usr/lib/sendmail -i -t -f %s" % config.admin_email
79 f = os.popen(send_sendmail, "w")
83 log.alert("sending email message failed: %s" % e)