]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/bin/parsemail.py
[models] Make patches unique on (msgid, project), not just (msgid)
[patchwork] / apps / patchwork / bin / parsemail.py
index 07554bcb3cbffeaada7ddb3741667379ca5db295..7f6727fc79c9d7e7efaf36f303e8f3c2ee188a4d 100755 (executable)
@@ -38,16 +38,22 @@ from patchwork.models import Patch, Project, Person, Comment
 
 list_id_headers = ['List-ID', 'X-Mailing-List']
 
+whitespace_re = re.compile('\s+')
+def normalise_space(str):
+    return whitespace_re.sub(' ', str).strip()
+
 def clean_header(header):
     """ Decode (possibly non-ascii) headers """
 
-    def decode(str, fragment):
+    def decode(fragment):
         (frag_str, frag_encoding) = fragment
         if frag_encoding:
-            return str + frag_str.decode(frag_encoding)
-        return str + frag_str.decode()
+            return frag_str.decode(frag_encoding)
+        return frag_str.decode()
+
+    fragments = map(decode, decode_header(header))
 
-    return reduce(decode, decode_header(header), u'').strip()
+    return normalise_space(u' '.join(fragments))
 
 def find_project(mail):
     project = None
@@ -104,7 +110,7 @@ def find_author(mail):
     new_person = False
 
     try:
-        person = Person.objects.get(email = email)
+        person = Person.objects.get(email__iexact = email)
     except Person.DoesNotExist:
         person = Person(name = name, email = email)
         new_person = True
@@ -131,21 +137,21 @@ def find_content(project, mail):
         if part.get_content_maintype() != 'text':
             continue
 
-        #print "\t%s, %s" % \
-        #    (part.get_content_subtype(), part.get_content_charset())
-
+        payload = part.get_payload(decode=True)
         charset = part.get_content_charset()
-        if not charset:
-            charset = mail.get_charset()
-        if not charset:
+        subtype = part.get_content_subtype()
+
+        # if we don't have a charset, assume utf-8
+        if charset is None:
             charset = 'utf-8'
 
-        payload = unicode(part.get_payload(decode=True), charset, "replace")
+        if not isinstance(payload, unicode):
+            payload = unicode(payload, charset)
 
-        if part.get_content_subtype() == 'x-patch':
+        if subtype in ['x-patch', 'x-diff']:
             patchbuf = payload
 
-        if part.get_content_subtype() == 'plain':
+        elif subtype == 'plain':
             if not patchbuf:
                 (patchbuf, c) = parse_patch(payload)
             else:
@@ -167,7 +173,7 @@ def find_content(project, mail):
         if patch:
             cpatch = patch
         else:
-            cpatch = find_patch_for_comment(mail)
+            cpatch = find_patch_for_comment(project, mail)
             if not cpatch:
                 return (None, None)
         comment = Comment(patch = cpatch, date = mail_date(mail),
@@ -176,7 +182,7 @@ def find_content(project, mail):
 
     return (patch, comment)
 
-def find_patch_for_comment(mail):
+def find_patch_for_comment(project, mail):
     # construct a list of possible reply message ids
     refs = []
     if 'In-Reply-To' in mail:
@@ -194,14 +200,14 @@ def find_patch_for_comment(mail):
 
         # first, check for a direct reply
         try:
-            patch = Patch.objects.get(msgid = ref)
+            patch = Patch.objects.get(project = project, msgid = ref)
             return patch
         except Patch.DoesNotExist:
             pass
 
         # see if we have comments that refer to a patch
         try:
-            comment = Comment.objects.get(msgid = ref)
+            comment = Comment.objects.get(patch__project = project, msgid = ref)
             return comment.patch
         except Comment.DoesNotExist:
             pass
@@ -234,7 +240,6 @@ def split_prefixes(prefix):
 
 re_re = re.compile('^(re|fwd?)[:\s]\s*', re.I)
 prefix_re = re.compile('^\[([^\]]*)\]\s*(.*)$')
-whitespace_re = re.compile('\s+')
 
 def clean_subject(subject, drop_prefixes = None):
     """ Clean a Subject: header from an incoming patch.
@@ -286,8 +291,7 @@ def clean_subject(subject, drop_prefixes = None):
     # remove Re:, Fwd:, etc
     subject = re_re.sub(' ', subject)
 
-    # normalise whitespace
-    subject = whitespace_re.sub(' ', subject)
+    subject = normalise_space(subject)
 
     prefixes = []
 
@@ -301,7 +305,7 @@ def clean_subject(subject, drop_prefixes = None):
         subject = match.group(2)
         match = prefix_re.match(subject)
 
-    subject = whitespace_re.sub(' ', subject)
+    subject = normalise_space(subject)
 
     subject = subject.strip()
     if prefixes:
@@ -315,8 +319,7 @@ def clean_content(str):
     str = sig_re.sub('', str)
     return str.strip()
 
-def main(args):
-    mail = message_from_file(sys.stdin)
+def parse_mail(mail):
 
     # some basic sanity checks
     if 'From' not in mail:
@@ -372,5 +375,9 @@ def main(args):
 
     return 0
 
+def main(args):
+    mail = message_from_file(sys.stdin)
+    return parse_mail(mail)
+
 if __name__ == '__main__':
     sys.exit(main(sys.argv))