]> git.pld-linux.org Git - projects/pld-builder.new.git/blob - PLD_Builder/mailer.py
- handle 'weird' characters in buildlogs when sending out status mails
[projects/pld-builder.new.git] / 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
11 def recode(s):
12     if s.__class__ == ''.__class__:
13         return s.decode('iso-8859-1', 'replace').encode('us-ascii', 'replace')
14     else:
15         return s.encode('us-ascii', 'replace')
16
17 class Message:
18     def __init__(self):
19         self.headers = {}
20         self.body = StringIO.StringIO()
21         self.set_std_headers()
22
23     def set_header(self, n, v):
24         self.headers[n] = v
25
26     def set_headers(self, to = None, cc = None, subject = None):
27         if to != None:
28             self.set_header("To", to)
29         if cc != None:
30             self.set_header("Cc", cc)
31         if subject != None:
32             self.set_header("Subject", subject)
33
34     def write_line(self, l):
35         self.body.write(recode("%s\n" % l))
36
37     def write(self, s):
38         self.body.write(recode(s))
39
40     def append_log(self, log):
41         s = os.stat(log)
42         if s.st_size > 50000:
43             # just head and tail
44             f = open(log)
45             line_cnt = 0
46             for l in f.xreadlines():
47                 line_cnt += 1
48             f.seek(0)
49             line = 0
50             for l in f.xreadlines():
51                 if line < 100 or line > line_cnt - 100:
52                     self.body.write(recode(l))
53                 if line == line_cnt - 100:
54                     self.body.write("\n\n[...]\n\n")
55                 line += 1
56         else:
57             util.sendfile(open(log), self.body)
58
59     def set_std_headers(self):
60         self.headers["Date"] = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
61         self.headers["Message-ID"] = "<pld-builder.%f.%d@%s>" \
62                 % (time.time(), os.getpid(), os.uname()[1])
63         self.headers["From"] = "PLD %s builder <%s>" \
64                 % (config.builder, config.email)
65         self.headers["X-PLD-Builder"] = config.builder
66
67     def write_to(self, f):
68         for k, v in self.headers.items():
69             f.write("%s: %s\n" % (k, v))
70         f.write("\n")
71         self.body.seek(0)
72         util.sendfile(self.body, f)
73
74     def send(self):
75         send_sendmail = "/usr/sbin/sendmail -i -t -f %s" % config.admin_email
76         f = os.popen(send_sendmail, "w")
77         self.write_to(f)
78         f.close()
This page took 0.033126 seconds and 4 git commands to generate.