From patchwork Fri Mar 10 17:38:01 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Finucane X-Patchwork-Id: 737490 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3vfvdJ3lr7z9s7n for ; Sat, 11 Mar 2017 04:39:00 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b="GGv8ppaL"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3vfvdJ2lQQzDqYZ for ; Sat, 11 Mar 2017 04:39:00 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b="GGv8ppaL"; dkim-atps=neutral X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Received: from relay1007.mymailcheap.com (relay1007.mymailcheap.com [144.217.215.66]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3vfvcq540WzDqXf for ; Sat, 11 Mar 2017 04:38:35 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b="GGv8ppaL"; dkim-atps=neutral Received: from filter1001.mymailcheap.com (filter1001.mymailcheap.com [158.69.67.251]) by relay1007.mymailcheap.com (Postfix) with ESMTPS id ACD581E018 for ; Fri, 10 Mar 2017 12:38:31 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by filter1001.mymailcheap.com (Postfix) with ESMTP id A5BC320918 for ; Fri, 10 Mar 2017 17:38:32 +0000 (UTC) Authentication-Results: filter1001.mymailcheap.com; dkim=permerror reason="key not found" header.d=that.guru header.i=@that.guru header.b=GGv8ppaL; dkim-adsp=none (unprotected policy); dkim-atps=neutral X-Virus-Scanned: Debian amavisd-new at filter1001.mymailcheap.com Received: from filter1001.mymailcheap.com ([127.0.0.1]) by localhost (filter1001.mymailcheap.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id fkwiH01FKQrC for ; Fri, 10 Mar 2017 17:38:29 +0000 (UTC) Received: from one.mxroute.com (one-outgoing.mxroute.com [23.92.70.115]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by filter1001.mymailcheap.com (Postfix) with ESMTPS for ; Fri, 10 Mar 2017 17:38:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=that.guru; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=2Wk1ifohY9GDD7w8dibQgX9DE/x4td9WwKO7DUDGgQQ=; b=GGv8ppaLuTpUFXyH0GWzYdIWKH nGbR3FO8Sg3R0ULGio6pNwJj0BSIRoRJoqs6I9v/jWeTD9ouu3fJj2VXHP3cjuamnHw2iNEWhnuZ+ 8CRt5G2S6kVwIDgOmMDoEUA7+h7d58/QEpwzEyuoXUUOHaYoFJGAki6YCwJp6jeqfKfrncgb72zLj R6Y2424UHeTPwP6hPmcTnE9Zq/rfMK5MoBC+A015qh0jAwbC9ag+vS307TBaThloDtOR0q9NWT9y2 u6IcKbsYIwiAEw7yOAthkh4oXGfmypzi/updHk+/YG8Swpor35FuZdV135/PpblqkDjKHAKI4rIx5 Z3U1AVEQ==; From: Stephen Finucane To: patchwork@lists.ozlabs.org Subject: [PATCH v4 3/9] utils: Move patch_to_mbox to utils module Date: Fri, 10 Mar 2017 17:38:01 +0000 Message-Id: <20170310173807.13407-4-stephen@that.guru> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170310173807.13407-1-stephen@that.guru> References: <20170310173807.13407-1-stephen@that.guru> X-OutGoing-Spam-Status: No, score=-10.0 X-AuthUser: stephen@that.guru X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Patchwork development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Patchwork" There's no reason to have this in '__init__.py'. Signed-off-by: Stephen Finucane Reviewed-by: Daniel Axtens --- patchwork/views/__init__.py | 79 --------------------------------- patchwork/views/bundle.py | 3 +- patchwork/views/patch.py | 3 +- patchwork/views/utils.py | 106 ++++++++++++++++++++++++++++++++++++++++++++ patchwork/views/xmlrpc.py | 2 +- 5 files changed, 111 insertions(+), 82 deletions(-) create mode 100644 patchwork/views/utils.py diff --git a/patchwork/views/__init__.py b/patchwork/views/__init__.py index db53cdf..c884515 100644 --- a/patchwork/views/__init__.py +++ b/patchwork/views/__init__.py @@ -17,25 +17,13 @@ # along with Patchwork; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -from __future__ import absolute_import - -import datetime -from email.encoders import encode_7or8bit -from email.header import Header -from email.mime.nonmultipart import MIMENonMultipart -from email.parser import HeaderParser -import email.utils -import re - from django.contrib import messages from django.shortcuts import get_object_or_404 -from django.utils import six from patchwork.filters import Filters from patchwork.forms import MultiplePatchForm from patchwork.models import Bundle from patchwork.models import BundlePatch -from patchwork.models import Comment from patchwork.models import Patch from patchwork.models import Project from patchwork.paginator import Paginator @@ -341,70 +329,3 @@ def process_multiplepatch_form(request, form, action, patches, context): messages.warning(request, 'No patches updated') return errors - - -class PatchMbox(MIMENonMultipart): - patch_charset = 'utf-8' - - def __init__(self, _text): - MIMENonMultipart.__init__(self, 'text', 'plain', - **{'charset': self.patch_charset}) - self.set_payload(_text.encode(self.patch_charset)) - encode_7or8bit(self) - - -def patch_to_mbox(patch): - postscript_re = re.compile('\n-{2,3} ?\n') - body = '' - - if patch.content: - body = patch.content.strip() + "\n" - - parts = postscript_re.split(body, 1) - if len(parts) == 2: - (body, postscript) = parts - body = body.strip() + "\n" - postscript = postscript.rstrip() - else: - postscript = '' - - # TODO(stephenfin): Make this use the tags infrastructure - for comment in Comment.objects.filter(submission=patch): - body += comment.patch_responses - - if postscript: - body += '---\n' + postscript + '\n' - - if patch.diff: - body += '\n' + patch.diff - - delta = patch.date - datetime.datetime.utcfromtimestamp(0) - utc_timestamp = delta.seconds + delta.days * 24 * 3600 - - mail = PatchMbox(body) - mail['Subject'] = patch.name - mail['X-Patchwork-Submitter'] = email.utils.formataddr(( - str(Header(patch.submitter.name, mail.patch_charset)), - patch.submitter.email)) - mail['X-Patchwork-Id'] = str(patch.id) - if patch.delegate: - mail['X-Patchwork-Delegate'] = str(patch.delegate.email) - mail['Message-Id'] = patch.msgid - mail.set_unixfrom('From patchwork ' + patch.date.ctime()) - - copied_headers = ['To', 'Cc', 'Date', 'From', 'List-Id'] - orig_headers = HeaderParser().parsestr(str(patch.headers)) - for header in copied_headers: - if header in orig_headers: - mail[header] = orig_headers[header] - - if 'Date' not in mail: - mail['Date'] = email.utils.formatdate(utc_timestamp) - - # NOTE(stephenfin) http://stackoverflow.com/a/28584090/613428 - if six.PY3: - mail = mail.as_bytes(True).decode() - else: - mail = mail.as_string(True) - - return mail diff --git a/patchwork/views/bundle.py b/patchwork/views/bundle.py index bab431c..b6959e9 100644 --- a/patchwork/views/bundle.py +++ b/patchwork/views/bundle.py @@ -28,7 +28,8 @@ from django.shortcuts import render, get_object_or_404 from patchwork.filters import DelegateFilter from patchwork.forms import BundleForm, DeleteBundleForm from patchwork.models import Bundle, BundlePatch, Project -from patchwork.views import generic_list, patch_to_mbox +from patchwork.views import generic_list +from patchwork.views.utils import patch_to_mbox @login_required diff --git a/patchwork/views/patch.py b/patchwork/views/patch.py index 705359f..d1e81d1 100644 --- a/patchwork/views/patch.py +++ b/patchwork/views/patch.py @@ -29,7 +29,8 @@ from django.shortcuts import render, get_object_or_404 from patchwork.forms import PatchForm, CreateBundleForm from patchwork.models import Patch, Project, Bundle, Submission -from patchwork.views import generic_list, patch_to_mbox +from patchwork.views import generic_list +from patchwork.views.utils import patch_to_mbox def patch_list(request, project_id): diff --git a/patchwork/views/utils.py b/patchwork/views/utils.py new file mode 100644 index 0000000..c998647 --- /dev/null +++ b/patchwork/views/utils.py @@ -0,0 +1,106 @@ +# Patchwork - automated patch tracking system +# Copyright (C) 2008 Jeremy Kerr +# Copyright (C) 2017 Stephen Finucane +# +# This file is part of the Patchwork package. +# +# Patchwork is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# Patchwork is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Patchwork; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import datetime +from email.encoders import encode_7or8bit +from email.header import Header +from email.mime.nonmultipart import MIMENonMultipart +from email.parser import HeaderParser +import email.utils +import re + +from django.utils import six + +from patchwork.models import Comment + + +class PatchMbox(MIMENonMultipart): + patch_charset = 'utf-8' + + def __init__(self, _text): + MIMENonMultipart.__init__(self, 'text', 'plain', + **{'charset': self.patch_charset}) + self.set_payload(_text.encode(self.patch_charset)) + encode_7or8bit(self) + + +def patch_to_mbox(patch): + """Get an mbox representation of a single patch. + + Arguments: + patch: The Patch object to convert. + + Returns: + A string for the mbox file. + """ + postscript_re = re.compile('\n-{2,3} ?\n') + body = '' + + if patch.content: + body = patch.content.strip() + "\n" + + parts = postscript_re.split(body, 1) + if len(parts) == 2: + (body, postscript) = parts + body = body.strip() + "\n" + postscript = postscript.rstrip() + else: + postscript = '' + + # TODO(stephenfin): Make this use the tags infrastructure + for comment in Comment.objects.filter(submission=patch): + body += comment.patch_responses + + if postscript: + body += '---\n' + postscript + '\n' + + if patch.diff: + body += '\n' + patch.diff + + delta = patch.date - datetime.datetime.utcfromtimestamp(0) + utc_timestamp = delta.seconds + delta.days * 24 * 3600 + + mail = PatchMbox(body) + mail['Subject'] = patch.name + mail['X-Patchwork-Submitter'] = email.utils.formataddr(( + str(Header(patch.submitter.name, mail.patch_charset)), + patch.submitter.email)) + mail['X-Patchwork-Id'] = str(patch.id) + if patch.delegate: + mail['X-Patchwork-Delegate'] = str(patch.delegate.email) + mail['Message-Id'] = patch.msgid + mail.set_unixfrom('From patchwork ' + patch.date.ctime()) + + copied_headers = ['To', 'Cc', 'Date', 'From', 'List-Id'] + orig_headers = HeaderParser().parsestr(str(patch.headers)) + for header in copied_headers: + if header in orig_headers: + mail[header] = orig_headers[header] + + if 'Date' not in mail: + mail['Date'] = email.utils.formatdate(utc_timestamp) + + # NOTE(stephenfin) http://stackoverflow.com/a/28584090/613428 + if six.PY3: + mail = mail.as_bytes(True).decode() + else: + mail = mail.as_string(True) + + return mail diff --git a/patchwork/views/xmlrpc.py b/patchwork/views/xmlrpc.py index 8b52d24..f715dd7 100644 --- a/patchwork/views/xmlrpc.py +++ b/patchwork/views/xmlrpc.py @@ -45,7 +45,7 @@ from patchwork.models import Patch from patchwork.models import Person from patchwork.models import Project from patchwork.models import State -from patchwork.views import patch_to_mbox +from patchwork.views.utils import patch_to_mbox class PatchworkXMLRPCDispatcher(SimpleXMLRPCDispatcher,