X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=apps%2Fpatchwork%2Fbin%2Fparsemail.py;h=19e6e57d5214fa309f5f27068df726c6f3696dfa;hb=b08229de74367b95f851f7bb2c89781022df4101;hp=0a9daf541d6b8f5937639c6292dc70b28d199d00;hpb=fae459d0b284b9a04cdf95f375357addc8ed36ec;p=patchwork diff --git a/apps/patchwork/bin/parsemail.py b/apps/patchwork/bin/parsemail.py index 0a9daf5..19e6e57 100755 --- a/apps/patchwork/bin/parsemail.py +++ b/apps/patchwork/bin/parsemail.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # # Patchwork - automated patch tracking system # Copyright (C) 2008 Jeremy Kerr @@ -24,6 +24,7 @@ import re import datetime import time import operator +import codecs from email import message_from_file try: from email.header import Header, decode_header @@ -34,7 +35,9 @@ except ImportError: from email.Utils import parsedate_tz, mktime_tz from patchwork.parser import parse_patch -from patchwork.models import Patch, Project, Person, Comment +from patchwork.models import Patch, Project, Person, Comment, State, \ + get_default_initial_patch_state +from django.contrib.auth.models import User list_id_headers = ['List-ID', 'X-Mailing-List', 'X-list'] @@ -136,15 +139,22 @@ def mail_headers(mail): for (k, v) in mail.items()]) def find_pull_request(content): - git_re = re.compile('^The following changes since commit .*' + + git_re = re.compile('^The following changes since commit.*' + '^are available in the git repository at:\n' - '^\s*(git://[^\n]+)$', + '^\s*([\S]+://[^\n]+)$', re.DOTALL | re.MULTILINE) match = git_re.search(content) if match: return match.group(1) return None +def try_decode(payload, charset): + try: + payload = unicode(payload, charset) + except UnicodeDecodeError: + return None + return payload + def find_content(project, mail): patchbuf = None commentbuf = '' @@ -155,15 +165,35 @@ def find_content(project, mail): continue payload = part.get_payload(decode=True) - charset = part.get_content_charset() subtype = part.get_content_subtype() - # if we don't have a charset, assume utf-8 - if charset is None: - charset = 'utf-8' - if not isinstance(payload, unicode): - payload = unicode(payload, charset) + charset = part.get_content_charset() + + # Check that we have a charset that we understand. Otherwise, + # ignore it and fallback to our standard set. + if charset is not None: + try: + codec = codecs.lookup(charset) + except LookupError: + charset = None + + # If there is no charset or if it is unknown, then try some common + # charsets before we fail. + if charset is None: + try_charsets = ['utf-8', 'windows-1252', 'iso-8859-1'] + else: + try_charsets = [charset] + + for cset in try_charsets: + decoded_payload = try_decode(payload, cset) + if decoded_payload is not None: + break + payload = decoded_payload + + # Could not find a valid decoded payload. Fail. + if payload is None: + return (None, None) if subtype in ['x-patch', 'x-diff']: patchbuf = payload @@ -183,15 +213,9 @@ def find_content(project, mail): patch = None comment = None - if patchbuf: - mail_headers(mail) - name = clean_subject(mail.get('Subject'), [project.linkname]) - patch = Patch(name = name, content = patchbuf, - date = mail_date(mail), headers = mail_headers(mail)) - - if pullurl: + if pullurl or patchbuf: name = clean_subject(mail.get('Subject'), [project.linkname]) - patch = Patch(name = name, pull_url = pullurl, + patch = Patch(name = name, pull_url = pullurl, content = patchbuf, date = mail_date(mail), headers = mail_headers(mail)) if commentbuf: @@ -346,6 +370,24 @@ def clean_content(str): str = sig_re.sub('', str) return str.strip() +def get_state(state_name): + """ Return the state with the given name or the default State """ + if state_name: + try: + return State.objects.get(name__iexact=state_name) + except State.DoesNotExist: + pass + return get_default_initial_patch_state() + +def get_delegate(delegate_email): + """ Return the delegate with the given email or None """ + if delegate_email: + try: + return User.objects.get(email__iexact=delegate_email) + except User.DoesNotExist: + pass + return None + def parse_mail(mail): # some basic sanity checks @@ -381,6 +423,9 @@ def parse_mail(mail): patch.submitter = author patch.msgid = msgid patch.project = project + patch.state = get_state(mail.get('X-Patchwork-State', '').strip()) + patch.delegate = get_delegate( + mail.get('X-Patchwork-Delegate', '').strip()) try: patch.save() except Exception, ex: