diff options
author | Rohit Sarkar <rohitsarkar5398@gmail.com> | 2020-08-01 11:54:15 +0530 |
---|---|---|
committer | Daniel Axtens <dja@axtens.net> | 2020-08-10 11:44:40 +1000 |
commit | fe0c0ca7279e35904c488dea57345e1d4f13f895 (patch) | |
tree | b8729932c03925beaed944463c2046de152663ac /patchwork/management/commands/replacerelations.py | |
parent | 8abd63eb75617ffff14205376ca6283c8f5e0ba2 (diff) | |
download | patchwork-fe0c0ca7279e35904c488dea57345e1d4f13f895.tar patchwork-fe0c0ca7279e35904c488dea57345e1d4f13f895.tar.gz |
management: introduce replacerelations command
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 <rohitsarkar5398@gmail.com>
[dja: pep8, drop relations directory as empty dirs don't get stored by git,
comment about how lines are generated.]
Signed-off-by: Daniel Axtens <dja@axtens.net>
Diffstat (limited to 'patchwork/management/commands/replacerelations.py')
-rw-r--r-- | patchwork/management/commands/replacerelations.py | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/patchwork/management/commands/replacerelations.py b/patchwork/management/commands/replacerelations.py new file mode 100644 index 0000000..1c72cd0 --- /dev/null +++ b/patchwork/management/commands/replacerelations.py @@ -0,0 +1,73 @@ +# Patchwork - automated patch tracking system +# Copyright (C) 2020 Rohit Sarkar <rohitsarkar5398@gmail.com> +# +# 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) |