From patchwork Fri Nov 18 00:54:50 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Finucane X-Patchwork-Id: 696365 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 AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3tKfgY2Wrhz9sBR for ; Fri, 18 Nov 2016 11:55:53 +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="hj3bOOiU"; 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 3tKfgY1HLtzDvtM for ; Fri, 18 Nov 2016 11:55:53 +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="hj3bOOiU"; dkim-atps=neutral X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Received: from butterfly.maple.relay.mailchannels.net (butterfly.maple.relay.mailchannels.net [23.83.214.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3tKfgT0Y9NzDvt0 for ; Fri, 18 Nov 2016 11:55:48 +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="hj3bOOiU"; dkim-atps=neutral X-Sender-Id: mxroute|x-authuser|stephen@that.guru Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 1BA0220B39; Fri, 18 Nov 2016 00:55:44 +0000 (UTC) Received: from one.mxroute.com (ip-10-229-10-199.us-west-2.compute.internal [10.229.10.199]) by relay.mailchannels.net (Postfix) with ESMTPA id 4BF802020F; Fri, 18 Nov 2016 00:55:43 +0000 (UTC) X-Sender-Id: mxroute|x-authuser|stephen@that.guru Received: from one.mxroute.com ([UNAVAILABLE]. [10.107.128.240]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384) by 0.0.0.0:2500 (trex/5.7.8); Fri, 18 Nov 2016 00:55:44 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: mxroute|x-authuser|stephen@that.guru X-MailChannels-Auth-Id: mxroute X-MC-Loop-Signature: 1479430543593:2529875008 X-MC-Ingress-Time: 1479430543592 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=OnJtDhFGJUILYbTKasYK4PJ2+mlWNLra797KKC/DJt8=; b=hj3bOOiUdgVDIs5aA9qtO9iqET fYrGMyH8y3yohvPfNeUdyCe3Kso0uU3oiN73d2VmCd1Bk7yAN5YrFcvFpzkcZY4bxmtDpmbUJWlzM XokFC5ZveUYrx8ufBJMTCdLJTs6xCBiJHiSEyFYBgfKv/0naDuYP2zbZ3ECdCJDepurkw1K8tQMWT f0RDPIcMLDMCtAVHwVG8OszqfSaChWJIkYDB6UsQ0QrIy8k1nNopCjn8qB/NHO5arA9vNPFLw0Evi 0O1wITgtmTguZEzc5MilwOSI+Li9UrP2GvJXBId5h1bI+Prn7SuO1mREGQbrIt0alhy3/Pifrk2Xa VT9nzUEQ==; From: Stephen Finucane To: patchwork@lists.ozlabs.org Subject: [PATCH 3/5] parsemail: Add '--hash' argument Date: Fri, 18 Nov 2016 00:54:50 +0000 Message-Id: <1479430492-22646-4-git-send-email-stephen@that.guru> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1479430492-22646-1-git-send-email-stephen@that.guru> References: <1479430492-22646-1-git-send-email-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: , Cc: Paul Jakma MIME-Version: 1.0 Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Patchwork" This allows us to replace the functionality previously provided by the 'parser' executable. Signed-off-by: Stephen Finucane Cc: Paul Jakma --- patchwork/management/commands/parsemail.py | 14 ++++++++++++++ patchwork/parser.py | 17 +++++++++++++++++ patchwork/tests/test_management.py | 9 +++++++++ 3 files changed, 40 insertions(+) diff --git a/patchwork/management/commands/parsemail.py b/patchwork/management/commands/parsemail.py index fd85763..02a84e7 100644 --- a/patchwork/management/commands/parsemail.py +++ b/patchwork/management/commands/parsemail.py @@ -26,6 +26,7 @@ import django from django.core.management import base from django.utils import six +from patchwork.parser import hash_mail from patchwork.parser import parse_mail from patchwork.management.commands import configure_logging @@ -42,6 +43,10 @@ class Command(base.BaseCommand): '--list-id', help='mailing list ID. If not supplied, this will be ' 'extracted from the mail headers.'), + make_option( + '--hash', + action='store_true', + help='show hash for parsed patch without storing it.'), ) else: def add_arguments(self, parser): @@ -55,6 +60,10 @@ class Command(base.BaseCommand): '--list-id', help='mailing list ID. If not supplied, this will be ' 'extracted from the mail headers.') + parser.add_argument( + '--hash', + action='store_true', + help='show hash for parsed patch without storing it.') def handle(self, *args, **options): infile = args[0] if args else options['infile'] @@ -76,6 +85,11 @@ class Command(base.BaseCommand): mail = email.message_from_binary_file(sys.stdin.buffer) else: mail = email.message_from_file(sys.stdin) + + if options['hash']: + self.stdout.write(hash_mail(mail)) + sys.exit(0) + try: result = parse_mail(mail, options['list_id']) if result: diff --git a/patchwork/parser.py b/patchwork/parser.py index fd7ec82..0432b52 100644 --- a/patchwork/parser.py +++ b/patchwork/parser.py @@ -907,6 +907,23 @@ def parse_mail(mail, list_id=None): return comment +def hash_mail(mail): + """Parse a mail and generate the hash for it. + + Args: + mail (`mbox.Mail`): Mail to parse and add. + + Returns: + None + """ + diff, content = find_content(mail) + + if not diff: + return # nothing to work with + + return Patch.hash_diff(diff).hexdigest() + + def find_filenames(diff): """Find files changes in a given diff.""" # normalise spaces diff --git a/patchwork/tests/test_management.py b/patchwork/tests/test_management.py index f916673..d61c5b1 100644 --- a/patchwork/tests/test_management.py +++ b/patchwork/tests/test_management.py @@ -112,6 +112,15 @@ class ParsemailTest(TestCase): count = models.Patch.objects.filter(project=project.id).count() self.assertEqual(count, 1) + def test_hash(self): + out = StringIO() + path = os.path.join(TEST_MAIL_DIR, '0013-with-utf8-body.mbox') + with self.assertRaises(SystemExit) as exc: + call_command('parsemail', infile=path, hash=True, stdout=out) + + self.assertEqual(exc.exception.code, 0) + self.assertIn('4df3c62e1', out.getvalue()) + class ParsearchiveTest(TestCase): def test_invalid_path(self):