]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/bin/parsemail.py
parser: Decode the subject header
[patchwork] / apps / patchwork / bin / parsemail.py
index e2beeae1b752d8ce03d64ee77d839caf294cd09d..0a9daf541d6b8f5937639c6292dc70b28d199d00 100755 (executable)
@@ -36,7 +36,7 @@ except ImportError:
 from patchwork.parser import parse_patch
 from patchwork.models import Patch, Project, Person, Comment
 
-list_id_headers = ['List-ID', 'X-Mailing-List']
+list_id_headers = ['List-ID', 'X-Mailing-List', 'X-list']
 
 whitespace_re = re.compile('\s+')
 def normalise_space(str):
@@ -57,11 +57,17 @@ def clean_header(header):
 
 def find_project(mail):
     project = None
-    listid_re = re.compile('.*<([^>]+)>.*', re.S)
+    listid_res = [re.compile('.*<([^>]+)>.*', re.S),
+                  re.compile('^([\S]+)$', re.S)]
 
     for header in list_id_headers:
         if header in mail:
-            match = listid_re.match(mail.get(header))
+
+            for listid_re in listid_res:
+                match = listid_re.match(mail.get(header))
+                if match:
+                    break
+
             if not match:
                 continue
 
@@ -129,33 +135,47 @@ def mail_headers(mail):
                     continuation_ws = '\t').encode()) \
                 for (k, v) in mail.items()])
 
+def find_pull_request(content):
+    git_re = re.compile('^The following changes since commit .*' +
+                        '^are available in the git repository at:\n'
+                        '^\s*(git://[^\n]+)$',
+                           re.DOTALL | re.MULTILINE)
+    match = git_re.search(content)
+    if match:
+        return match.group(1)
+    return None
+
 def find_content(project, mail):
     patchbuf = None
     commentbuf = ''
+    pullurl = None
 
     for part in mail.walk():
         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':
+            c = payload
+
             if not patchbuf:
                 (patchbuf, c) = parse_patch(payload)
-            else:
-                c = payload
+
+            if not pullurl:
+                pullurl = find_pull_request(payload)
 
             if c is not None:
                 commentbuf += c.strip() + '\n'
@@ -165,15 +185,20 @@ def find_content(project, mail):
 
     if patchbuf:
         mail_headers(mail)
-       name = clean_subject(mail.get('Subject'), [project.linkname])
+        name = clean_subject(mail.get('Subject'), [project.linkname])
         patch = Patch(name = name, content = patchbuf,
                     date = mail_date(mail), headers = mail_headers(mail))
 
+    if pullurl:
+        name = clean_subject(mail.get('Subject'), [project.linkname])
+        patch = Patch(name = name, pull_url = pullurl,
+                    date = mail_date(mail), headers = mail_headers(mail))
+
     if commentbuf:
         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),
@@ -182,7 +207,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:
@@ -200,14 +225,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
@@ -281,6 +306,8 @@ def clean_subject(subject, drop_prefixes = None):
     '[bar] meep'
     """
 
+    subject = clean_header(subject)
+
     if drop_prefixes is None:
         drop_prefixes = []
     else:
@@ -319,8 +346,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:
@@ -376,5 +402,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))