From patchwork Sat Aug 1 06:24:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rohit Sarkar X-Patchwork-Id: 1339669 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.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BJYzL1gMZz9sRK for ; Sat, 1 Aug 2020 16:25:02 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=Mqx7nQCc; dkim-atps=neutral Received: from bilbo.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 4BJYzL1FPtzDqTx for ; Sat, 1 Aug 2020 16:25:02 +1000 (AEST) X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gmail.com (client-ip=2607:f8b0:4864:20::1044; helo=mail-pj1-x1044.google.com; envelope-from=rohitsarkar5398@gmail.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=Mqx7nQCc; dkim-atps=neutral Received: from mail-pj1-x1044.google.com (mail-pj1-x1044.google.com [IPv6:2607:f8b0:4864:20::1044]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4BJYz008X7zDqTx for ; Sat, 1 Aug 2020 16:24:43 +1000 (AEST) Received: by mail-pj1-x1044.google.com with SMTP id mt12so8369865pjb.4 for ; Fri, 31 Jul 2020 23:24:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rBTwrt/Uqz4oKhpciEhZt08NnuY/k/neV9bO1tzHTNA=; b=Mqx7nQCc8VqpUGl9xzFRAW6J3fBhieKeGILSSTfjjZ2tMEfJxElbGqGApcVzCONaUN Zc3MhBpMAPETPhY/aGUdFH8zKo7kauA+GAA4hZ0/fQp7u2+T/r1VPued1JlHGgTOK4+u NH54rhGHyXEyrKsykME1dtfEs1mZhk8gJ1Nl4IfmNARP82XoaN7i6OvowQ4SYpv6DjQj yKM0/+qAmp0WFYNs7jMgny5vf92RtQJSkCd4J2znA7OU7aQy5YiTelzqqy0QN3e85js2 EOXTc50+ZPu0lGTmkk7xlbYe8vmAT2c4rhNkyFHjGhoSESpzhKsq+sKgXPjnhg5LZ74O U+Zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rBTwrt/Uqz4oKhpciEhZt08NnuY/k/neV9bO1tzHTNA=; b=gsh5fGJeALwOoBWEAuBCCK3JhmeyBCP22dzlgLxtg+hELNEmu3mKY+GVaD0xM7FVkB mNAgKU3FQR3j5OyqK62dMTkYLMHtW8285Fjlxhb7hynCvsR+hO8n8+qckhjkN5b5Ntxt XTpfRt9miv5tN3qW54URk7lQrkhA0g8Dio3xHirpg4PL3aeaQhzk2mC246ll7f8etN1l ZFjR9qKHH+r51Objt7pL00FOv+qgUiP0g5SIsqu25YeYlb4iwtqFMNi8vbos7VW9FMmO pDiN/qT8DPQrommRKv/yynky2RxREdnbrMYExfswf21cwG5MOaL6VLyUcvdVXdgN5tdl nf4A== X-Gm-Message-State: AOAM530C//6hxFFNHJ6/UJlltAiUkbfhLkRDrKaEOegJyi2vksrXkgAJ 1m3s3jSJqV8m9GFJmIWL6vmifK6/my8= X-Google-Smtp-Source: ABdhPJyrCr1JeJGqjFHm9Qgo0VtDVEYQDKqjAz3U5QWLRcCRRkuKoi9SjxRterzlb/6OTAaWWfI2JA== X-Received: by 2002:a17:90a:6509:: with SMTP id i9mr7458963pjj.104.1596263081637; Fri, 31 Jul 2020 23:24:41 -0700 (PDT) Received: from localhost.localdomain ([156.237.1.10]) by smtp.gmail.com with ESMTPSA id d129sm2511596pfc.108.2020.07.31.23.24.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Jul 2020 23:24:41 -0700 (PDT) From: Rohit Sarkar To: patchwork@lists.ozlabs.org Subject: [PATCH v5 1/1] management: introduce replacerelations command Date: Sat, 1 Aug 2020 11:54:15 +0530 Message-Id: <20200801062415.13795-2-rohitsarkar5398@gmail.com> X-Mailer: git-send-email 2.23.0.385.gbc12974a89 In-Reply-To: <20200801062415.13795-1-rohitsarkar5398@gmail.com> References: <20200801062415.13795-1-rohitsarkar5398@gmail.com> MIME-Version: 1.0 X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Patchwork development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: ralf.ramsauer@oth-regensburg.de Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Patchwork" The replacerelations script is used to ingest relations into Patchwork's patch database. A patch groups file is taken as input, which on each line contains a space separated list of patchwork ids denoting a relation. All the existing relations in Patchwork's database are removed and the relations read from the patch groups file are ingested. Signed-off-by: Rohit Sarkar --- docs/deployment/management.rst | 37 ++++++++++ .../management/commands/replacerelations.py | 72 +++++++++++++++++++ patchwork/tests/__init__.py | 1 + patchwork/tests/test_management.py | 54 +++++++++++++- 4 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 patchwork/management/commands/replacerelations.py diff --git a/docs/deployment/management.rst b/docs/deployment/management.rst index 9c57f19..dcee9ff 100644 --- a/docs/deployment/management.rst +++ b/docs/deployment/management.rst @@ -116,6 +116,43 @@ the :ref:`deployment installation guide `. input mbox filename. If not supplied, a patch will be read from ``stdin``. +replacerelations +~~~~~~~~~~~~~~~~ + +.. program:: manage.py replacerelations + +Parse a patch groups file and store any relation found + +.. code-block:: shell + + ./manage.py replacerelations + +This is a script used to ingest relations into Patchwork. + +A patch groups file contains on each line a list of space separated patch IDs +of patches that form a relation. + +For example, consider the contents of a sample patch groups file:: + + 1 3 5 + 2 + 7 10 11 12 + +In this case the script will identify 2 relations, (1, 3, 5) and +(7, 10, 11, 12). The single patch ID "2" on the second line is ignored as a +relation always consists of more than 1 patch. + +Further, if a patch ID in the patch groups file does not exist in the database +of the Patchwork instance, that patch ID will be silently ignored while forming +the relations. + +Running this script will remove all existing relations and replace them with +the relations found in the file. + +.. option:: infile + + input patch groups file. + rehash ~~~~~~ diff --git a/patchwork/management/commands/replacerelations.py b/patchwork/management/commands/replacerelations.py new file mode 100644 index 0000000..0cc4d0a --- /dev/null +++ b/patchwork/management/commands/replacerelations.py @@ -0,0 +1,72 @@ +# Patchwork - automated patch tracking system +# Copyright (C) 2020 Rohit Sarkar +# +# SPDX-License-Identifier: GPL-2.0-or-later + +import logging +import os +import sys + +from django.db import transaction +from django.core.management.base import BaseCommand + +from patchwork.models import Patch +from patchwork.models import PatchRelation + +logger = logging.getLogger(__name__) + +class Command(BaseCommand): + help = 'Parse a relations file generated by PaStA and replace existing relations with the ones parsed' + + def add_arguments(self, parser): + parser.add_argument( + 'infile', + help='input relations filename') + + def handle(self, *args, **options): + verbosity = int(options['verbosity']) + if not verbosity: + level = logging.CRITICAL + elif verbosity == 1: + level = logging.ERROR + elif verbosity == 2: + level = logging.INFO + else: + level = logging.DEBUG + + logger.setLevel(level) + + path = args and args[0] or options['infile'] + if not os.path.exists(path): + logger.error('Invalid path: %s', path) + sys.exit(1) + + + with open(path, 'r') as f: + lines = f.readlines() + + # filter out trailing empty lines + while len(lines) and not lines[-1]: + lines.pop() + + relations = [line.split(' ') for line in lines] + + with transaction.atomic(): + PatchRelation.objects.all().delete() + count = len(relations) + ingested = 0 + logger.info('Parsing %d relations' % count) + for i, patch_ids in enumerate(relations): + related_patches = Patch.objects.filter(id__in=patch_ids) + + if len(related_patches) > 1: + relation = PatchRelation() + relation.save() + related_patches.update(related=relation) + ingested += 1 + + if i % 10 == 0: + self.stdout.write('%06d/%06d\r' % (i, count), ending='') + self.stdout.flush() + + self.stdout.write('Ingested %d relations' % ingested) diff --git a/patchwork/tests/__init__.py b/patchwork/tests/__init__.py index 8f78ea7..83358a6 100644 --- a/patchwork/tests/__init__.py +++ b/patchwork/tests/__init__.py @@ -8,3 +8,4 @@ import os TEST_MAIL_DIR = os.path.join(os.path.dirname(__file__), 'mail') TEST_PATCH_DIR = os.path.join(os.path.dirname(__file__), 'patches') TEST_FUZZ_DIR = os.path.join(os.path.dirname(__file__), 'fuzztests') +TEST_RELATIONS_DIR = os.path.join(os.path.dirname(__file__), 'relations') diff --git a/patchwork/tests/test_management.py b/patchwork/tests/test_management.py index 66c6bad..d33a9f3 100644 --- a/patchwork/tests/test_management.py +++ b/patchwork/tests/test_management.py @@ -5,13 +5,14 @@ import os import sys +import tempfile from io import StringIO from django.core.management import call_command from django.test import TestCase from patchwork import models -from patchwork.tests import TEST_MAIL_DIR +from patchwork.tests import TEST_MAIL_DIR, TEST_RELATIONS_DIR from patchwork.tests import utils @@ -124,3 +125,54 @@ class ParsearchiveTest(TestCase): self.assertIn('Processed 1 messages -->', out.getvalue()) self.assertIn(' 1 dropped', out.getvalue()) + +class ReplacerelationsTest(TestCase): + def test_invalid_path(self): + out = StringIO() + with self.assertRaises(SystemExit) as exc: + call_command('replacerelations', 'xyz123random', '-v 0', stdout=out) + self.assertEqual(exc.exception.code, 1) + + def test_valid_relations(self): + test_submitter = utils.create_person() + utils.create_patches(8, submitter=test_submitter) + patch_ids = ( + models.Patch.objects + .filter(submitter=test_submitter) + .values_list('id', flat=True) + ) + + with tempfile.NamedTemporaryFile(delete=False, + dir=TEST_RELATIONS_DIR, + mode='w+') as f1: + for i in range(0, len(patch_ids), 3): + f1.write('%s\n' % ' '.join(map(str, patch_ids[i:i+3]))) + + + out = StringIO() + call_command('replacerelations', + os.path.join(TEST_RELATIONS_DIR, + f1.name), + stdout=out) + self.assertEqual(models.PatchRelation.objects.count(), 3) + os.unlink(f1.name) + + patch_ids_with_missing = ( + list(patch_ids) + + [i for i in range(max(patch_ids), max(patch_ids) + 3)] + ) + with tempfile.NamedTemporaryFile(delete=False, + dir=TEST_RELATIONS_DIR, + mode='w+') as f2: + for i in range(0, len(patch_ids_with_missing), 3): + f2.write( + '%s\n' % ' '.join(map(str, + patch_ids_with_missing[i:i+3])) + ) + + call_command('replacerelations', + os.path.join(TEST_RELATIONS_DIR, + f2.name), + stdout=out) + self.assertEqual(models.PatchRelation.objects.count(), 3) + os.unlink(f2.name)