]> TLD Linux GIT Repositories - TLD.git/blob - pld-builder.new/PLD_Builder/mailer.py
e0b3ae601567a063e2025ac4f519d84ab72ee409
[TLD.git] / pld-builder.new / PLD_Builder / mailer.py
1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
2
3 import time
4 import os
5 import sys
6 import StringIO
7
8 from config import config
9 import util
10 import log
11
12 def recode(s):
13     if s.__class__ == ''.__class__:
14         return s.decode('iso-8859-1', 'replace').encode('us-ascii', 'replace')
15     else:
16         return s.encode('us-ascii', 'replace')
17
18 class Message:
19     def __init__(self):
20         self.headers = {}
21         self.body = StringIO.StringIO()
22         self.set_std_headers()
23
24     def set_header(self, n, v):
25         self.headers[n] = v
26
27     def set_headers(self, to = None, cc = None, subject = None):
28         if to != None:
29             self.set_header("To", to)
30         if cc != None:
31             self.set_header("Cc", cc)
32         if subject != None:
33             self.set_header("Subject", subject)
34
35     def write_line(self, l):
36         self.body.write(recode("%s\n" % l))
37
38     def write(self, s):
39         self.body.write(recode(s))
40
41     def append_log(self, log):
42         s = os.stat(log)
43         if s.st_size > 50000:
44             # just head and tail
45             f = open(log)
46             line_cnt = 0
47             for l in f.xreadlines():
48                 line_cnt += 1
49             f.seek(0)
50             line = 0
51             for l in f.xreadlines():
52                 if line < 100 or line > line_cnt - 100:
53                     self.body.write(recode(l))
54                 if line == line_cnt - 100:
55                     self.body.write("\n\n[...]\n\n")
56                 line += 1
57         else:
58             util.sendfile(open(log), self.body)
59
60     def set_std_headers(self):
61         self.headers["Date"] = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
62         self.headers["Message-ID"] = "<pld-builder.%f.%d@%s>" \
63                 % (time.time(), os.getpid(), os.uname()[1])
64         self.headers["From"] = "PLD %s builder <%s>" \
65                 % (config.builder, config.email)
66         self.headers["X-PLD-Builder"] = config.builder
67
68     def write_to(self, f):
69         for k, v in self.headers.items():
70             f.write("%s: %s\n" % (k, v))
71         f.write("\n")
72         self.body.seek(0)
73         util.sendfile(self.body, f)
74
75     def send(self):
76         if not os.path.exists("/usr/lib/sendmail"):
77             # TODO: dump to file?
78             log.alert("/usr/lib/sendmail doesn't exist: Can't send email")
79             return False
80         send_sendmail = "/usr/lib/sendmail -i -t -f %s" % config.admin_email
81         f = os.popen(send_sendmail, "w")
82         try:
83             self.write_to(f)
84         except IOError, e:
85             log.alert("sending email message failed: %s" % e)
86             f.close()
87             return False
88         return f.close()