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")
91 person = Person.objects.get(email = email)
92 except Person.DoesNotExist:
93 person = Person(name = name, email = email)
96 return (person, new_person)
99 t = parsedate_tz(mail.get('Date', ''))
102 return datetime.datetime.utcnow()
103 return datetime.datetime.utcfromtimestamp(mktime_tz(t))
105 def mail_headers(mail):
106 return reduce(operator.__concat__,
107 ['%s: %s\n' % (k, Header(v, header_name = k, \
108 continuation_ws = '\t').encode()) \
109 for (k, v) in mail.items()])
111 def find_content(project, mail):
115 for part in mail.walk():
116 if part.get_content_maintype() != 'text':
119 #print "\t%s, %s" % \
120 # (part.get_content_subtype(), part.get_content_charset())
122 charset = part.get_content_charset()
124 charset = mail.get_charset()
128 payload = unicode(part.get_payload(decode=True), charset, "replace")
130 if part.get_content_subtype() == 'x-patch':
133 if part.get_content_subtype() == 'plain':
135 (patchbuf, c) = parse_patch(payload)
140 commentbuf += c.strip() + '\n'
147 patch = Patch(name = clean_subject(mail.get('Subject')),
148 content = patchbuf, date = mail_date(mail),
149 headers = mail_headers(mail))
155 cpatch = find_patch_for_comment(mail)
158 comment = Comment(patch = cpatch, date = mail_date(mail),
159 content = clean_content(commentbuf),
160 headers = mail_headers(mail))
162 return (patch, comment)
164 def find_patch_for_comment(mail):
165 # construct a list of possible reply message ids
167 if 'In-Reply-To' in mail:
168 refs.append(mail.get('In-Reply-To'))
170 if 'References' in mail:
171 rs = mail.get('References').split()
180 # first, check for a direct reply
182 patch = Patch.objects.get(msgid = ref)
184 except Patch.DoesNotExist:
187 # see if we have comments that refer to a patch
189 comment = Comment.objects.get(msgid = ref)
191 except Comment.DoesNotExist:
197 re_re = re.compile('^(re|fwd?)[:\s]\s*', re.I)
198 prefix_re = re.compile('^\[.*\]\s*')
199 whitespace_re = re.compile('\s+')
201 def clean_subject(subject):
202 subject = re_re.sub(' ', subject)
203 subject = prefix_re.sub('', subject)
204 subject = whitespace_re.sub(' ', subject)
205 return subject.strip()
207 sig_re = re.compile('^(-{2,3} ?|_+)\n.*', re.S | re.M)
208 def clean_content(str):
209 str = sig_re.sub('', str)
213 mail = message_from_file(sys.stdin)
215 # some basic sanity checks
216 if 'From' not in mail:
219 if 'Subject' not in mail:
222 if 'Message-Id' not in mail:
225 hint = mail.get('X-Patchwork-Hint', '').lower()
229 project = find_project(mail)
231 print "no project found"
234 msgid = mail.get('Message-Id').strip()
236 (author, save_required) = find_author(mail)
238 (patch, comment) = find_content(project, mail)
241 # we delay the saving until we know we have a patch.
244 save_required = False
245 patch.submitter = author
247 patch.project = project
250 except Exception, ex:
256 # looks like the original constructor for Comment takes the pk
257 # when the Comment is created. reset it here.
259 comment.patch = patch
260 comment.submitter = author
261 comment.msgid = msgid
264 except Exception, ex:
269 if __name__ == '__main__':
270 sys.exit(main(sys.argv))