3 # Patchwork - automated patch tracking system
4 # Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
6 # This file is part of the Patchwork package.
8 # Patchwork is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # Patchwork is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Patchwork; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 from email import message_from_file
28 from email.header import Header
29 from email.utils import parsedate_tz, mktime_tz
31 from patchparser import parse_patch
32 from patchwork.models import Patch, Project, Person, Comment
34 list_id_headers = ['List-ID', 'X-Mailing-List']
36 def find_project(mail):
38 listid_re = re.compile('.*<([^>]+)>.*', re.S)
40 for header in list_id_headers:
42 match = listid_re.match(mail.get(header))
46 listid = match.group(1)
49 project = Project.objects.get(listid = listid)
56 def find_author(mail):
58 from_header = mail.get('From').strip()
59 (name, email) = (None, None)
61 # tuple of (regex, fn)
62 # - where fn returns a (name, email) tuple from the match groups resulting
63 # from re.match().groups()
65 # for "Firstname Lastname" <example@example.com> style addresses
66 (re.compile('"?(.*?)"?\s*<([^>]+)>'), (lambda g: (g[0], g[1]))),
68 # for example@example.com (Firstname Lastname) style addresses
69 (re.compile('"?(.*?)"?\s*\(([^\)]+)\)'), (lambda g: (g[1], g[0]))),
72 (re.compile('(.*)'), (lambda g: (None, g[0]))),
75 for regex, fn in from_res:
76 match = regex.match(from_header)
78 (name, email) = fn(match.groups())
82 raise Exception("Could not parse From: header")
89 person = Person.objects.get(email = email)
90 except Person.DoesNotExist:
91 person = Person(name = name, email = email)
96 t = parsedate_tz(mail.get('Date', ''))
99 return datetime.datetime.utcnow()
100 return datetime.datetime.utcfromtimestamp(mktime_tz(t))
102 def mail_headers(mail):
103 return reduce(operator.__concat__,
104 ['%s: %s\n' % (k, Header(v, header_name = k, \
105 continuation_ws = '\t').encode()) \
106 for (k, v) in mail.items()])
108 def find_content(project, mail):
112 for part in mail.walk():
113 if part.get_content_maintype() != 'text':
116 #print "\t%s, %s" % \
117 # (part.get_content_subtype(), part.get_content_charset())
119 charset = part.get_content_charset()
121 charset = mail.get_charset()
125 payload = unicode(part.get_payload(decode=True), charset, "replace")
127 if part.get_content_subtype() == 'x-patch':
130 if part.get_content_subtype() == 'plain':
132 (patchbuf, c) = parse_patch(payload)
137 commentbuf += c.strip() + '\n'
144 patch = Patch(name = clean_subject(mail.get('Subject')),
145 content = patchbuf, date = mail_date(mail),
146 headers = mail_headers(mail))
152 cpatch = find_patch_for_comment(mail)
155 comment = Comment(patch = cpatch, date = mail_date(mail),
156 content = clean_content(commentbuf),
157 headers = mail_headers(mail))
159 return (patch, comment)
161 def find_patch_for_comment(mail):
162 # construct a list of possible reply message ids
164 if 'In-Reply-To' in mail:
165 refs.append(mail.get('In-Reply-To'))
167 if 'References' in mail:
168 rs = mail.get('References').split()
177 # first, check for a direct reply
179 patch = Patch.objects.get(msgid = ref)
181 except Patch.DoesNotExist:
184 # see if we have comments that refer to a patch
186 comment = Comment.objects.get(msgid = ref)
188 except Comment.DoesNotExist:
194 re_re = re.compile('^(re|fwd?)[:\s]\s*', re.I)
195 prefix_re = re.compile('^\[.*\]\s*')
196 whitespace_re = re.compile('\s+')
198 def clean_subject(subject):
199 subject = re_re.sub(' ', subject)
200 subject = prefix_re.sub('', subject)
201 subject = whitespace_re.sub(' ', subject)
202 return subject.strip()
204 sig_re = re.compile('^(-{2,3} ?|_+)\n.*', re.S | re.M)
205 def clean_content(str):
206 str = sig_re.sub('', str)
210 mail = message_from_file(sys.stdin)
212 # some basic sanity checks
213 if 'From' not in mail:
216 if 'Subject' not in mail:
219 if 'Message-Id' not in mail:
222 hint = mail.get('X-Patchwork-Hint', '').lower()
226 project = find_project(mail)
228 print "no project found"
231 msgid = mail.get('Message-Id').strip()
233 author = find_author(mail)
235 (patch, comment) = find_content(project, mail)
239 patch.submitter = author
241 patch.project = project
244 except Exception, ex:
249 # looks like the original constructor for Comment takes the pk
250 # when the Comment is created. reset it here.
252 comment.patch = patch
253 comment.submitter = author
254 comment.msgid = msgid
257 except Exception, ex:
262 if __name__ == '__main__':
263 sys.exit(main(sys.argv))