]> git.ozlabs.org Git - patchwork/blobdiff - patchwork/bin/parsemail.py
patchwork/parser: Adapt for new unsaved-foreign-key behaviour in django 1.8
[patchwork] / patchwork / bin / parsemail.py
index 19e6e57d5214fa309f5f27068df726c6f3696dfa..f2b10bd38ee9f0536745d0e624db5c2a8f7c726f 100755 (executable)
@@ -37,6 +37,7 @@ except ImportError:
 from patchwork.parser import parse_patch
 from patchwork.models import Patch, Project, Person, Comment, State, \
         get_default_initial_patch_state
+import django
 from django.contrib.auth.models import User
 
 list_id_headers = ['List-ID', 'X-Mailing-List', 'X-list']
@@ -79,7 +80,7 @@ def find_project(mail):
             try:
                 project = Project.objects.get(listid = listid)
                 break
-            except:
+            except Project.DoesNotExist:
                 pass
 
     return project
@@ -219,15 +220,20 @@ def find_content(project, mail):
                     date = mail_date(mail), headers = mail_headers(mail))
 
     if commentbuf:
+        # If this is a new patch, we defer setting comment.patch until
+        # patch has been saved by the caller
         if patch:
-            cpatch = patch
+            comment = Comment(date = mail_date(mail),
+                    content = clean_content(commentbuf),
+                    headers = mail_headers(mail))
+
         else:
             cpatch = find_patch_for_comment(project, mail)
             if not cpatch:
                 return (None, None)
-        comment = Comment(patch = cpatch, date = mail_date(mail),
-                content = clean_content(commentbuf),
-                headers = mail_headers(mail))
+            comment = Comment(patch = cpatch, date = mail_date(mail),
+                    content = clean_content(commentbuf),
+                    headers = mail_headers(mail))
 
     return (patch, comment)
 
@@ -267,23 +273,8 @@ def find_patch_for_comment(project, mail):
 split_re = re.compile('[,\s]+')
 
 def split_prefixes(prefix):
-    """ Turn a prefix string into a list of prefix tokens
-
-    >>> split_prefixes('PATCH')
-    ['PATCH']
-    >>> split_prefixes('PATCH,RFC')
-    ['PATCH', 'RFC']
-    >>> split_prefixes('')
-    []
-    >>> split_prefixes('PATCH,')
-    ['PATCH']
-    >>> split_prefixes('PATCH ')
-    ['PATCH']
-    >>> split_prefixes('PATCH,RFC')
-    ['PATCH', 'RFC']
-    >>> split_prefixes('PATCH 1/2')
-    ['PATCH', '1/2']
-    """
+    """ Turn a prefix string into a list of prefix tokens """
+
     matches = split_re.split(prefix)
     return [ s for s in matches if s != '' ]
 
@@ -296,39 +287,8 @@ def clean_subject(subject, drop_prefixes = None):
     Removes Re: and Fwd: strings, as well as [PATCH]-style prefixes. By
     default, only [PATCH] is removed, and we keep any other bracketed data
     in the subject. If drop_prefixes is provided, remove those too,
-    comparing case-insensitively.
-
-    >>> clean_subject('meep')
-    'meep'
-    >>> clean_subject('Re: meep')
-    'meep'
-    >>> clean_subject('[PATCH] meep')
-    'meep'
-    >>> clean_subject('[PATCH] meep \\n meep')
-    'meep meep'
-    >>> clean_subject('[PATCH RFC] meep')
-    '[RFC] meep'
-    >>> clean_subject('[PATCH,RFC] meep')
-    '[RFC] meep'
-    >>> clean_subject('[PATCH,1/2] meep')
-    '[1/2] meep'
-    >>> clean_subject('[PATCH RFC 1/2] meep')
-    '[RFC,1/2] meep'
-    >>> clean_subject('[PATCH] [RFC] meep')
-    '[RFC] meep'
-    >>> clean_subject('[PATCH] [RFC,1/2] meep')
-    '[RFC,1/2] meep'
-    >>> clean_subject('[PATCH] [RFC] [1/2] meep')
-    '[RFC,1/2] meep'
-    >>> clean_subject('[PATCH] rewrite [a-z] regexes')
-    'rewrite [a-z] regexes'
-    >>> clean_subject('[PATCH] [RFC] rewrite [a-z] regexes')
-    '[RFC] rewrite [a-z] regexes'
-    >>> clean_subject('[foo] [bar] meep', ['foo'])
-    '[bar] meep'
-    >>> clean_subject('[FOO] [bar] meep', ['foo'])
-    '[bar] meep'
-    """
+    comparing case-insensitively."""
+
 
     subject = clean_header(subject)
 
@@ -434,8 +394,7 @@ def parse_mail(mail):
     if comment:
         if save_required:
             author.save()
-        # looks like the original constructor for Comment takes the pk
-        # when the Comment is created. reset it here.
+        # we defer this assignment until we know that we have a saved patch
         if patch:
             comment.patch = patch
         comment.submitter = author
@@ -448,6 +407,7 @@ def parse_mail(mail):
     return 0
 
 def main(args):
+    django.setup()
     mail = message_from_file(sys.stdin)
     return parse_mail(mail)