|
| 1 | +# Copyright The IETF Trust 2011-2019, All Rights Reserved |
| 2 | +#!/usr/bin/python |
| 3 | + |
| 4 | +# simple script for exporting name related base data for the tests |
| 5 | + |
| 6 | +import inspect |
| 7 | +import io |
| 8 | +import os, sys |
| 9 | +import six |
| 10 | +if six.PY3: |
| 11 | + from typing import Any, List # pyflakes:ignore |
| 12 | + |
| 13 | + |
| 14 | +from django.conf import settings |
| 15 | +from django.core.management.base import BaseCommand |
| 16 | +from django.core.serializers import serialize |
| 17 | +from django.core.serializers.json import DjangoJSONEncoder |
| 18 | + |
| 19 | +import debug # pyflakes:ignore |
| 20 | + |
| 21 | +class SortedJsonEncoder(DjangoJSONEncoder): |
| 22 | + def __init__(self, *args, **kwargs): |
| 23 | + kwargs['sort_keys'] = True |
| 24 | + return super(SortedJsonEncoder, self).__init__(*args, **kwargs) |
| 25 | + |
| 26 | +class Command(BaseCommand): |
| 27 | + help = """ |
| 28 | + Generate a custom fixture for all objects needed by the datatracker test suite. |
| 29 | +
|
| 30 | + The recommended way to use this is unfortunately not the default, as the ordering |
| 31 | + of the resulting fixture isn't quite stable. Instead use: |
| 32 | +
|
| 33 | + "ietf/manage.py generate_name_fixture --stdout | jq --sort-keys 'sort_by(.model, .pk)' > ietf/name/fixtures/names.json" |
| 34 | + """ |
| 35 | + |
| 36 | + def add_arguments(self, parser): |
| 37 | + parser.add_argument('--stdout', action='store_true', default=False, help="Send fixture to stdout instead of ietf/name/fixtures/names.json") |
| 38 | + |
| 39 | + def say(self, msg): |
| 40 | + if self.verbosity > 0: |
| 41 | + sys.stdout.write(msg) |
| 42 | + sys.stdout.write('\n') |
| 43 | + |
| 44 | + def note(self, msg): |
| 45 | + if self.verbosity > 1: |
| 46 | + sys.stdout.write(msg) |
| 47 | + sys.stdout.write('\n') |
| 48 | + |
| 49 | + def mutter(self, msg): |
| 50 | + if self.verbosity > 2: |
| 51 | + sys.stdout.write(msg) |
| 52 | + sys.stdout.write('\n') |
| 53 | + |
| 54 | + def handle(self, *args, **options): |
| 55 | + self.output = sys.stdout if options.get('stdout') else io.open(os.path.join(settings.BASE_DIR, "name/fixtures/names.json"), 'w') |
| 56 | + |
| 57 | + def model_name(m): |
| 58 | + return '%s.%s' % (m._meta.app_label, m.__name__) |
| 59 | + |
| 60 | + def output(seq): |
| 61 | + try: |
| 62 | + f = self.output |
| 63 | + f.write(serialize("json", seq, cls=SortedJsonEncoder, indent=2)) |
| 64 | + f.close() |
| 65 | + except: |
| 66 | + from django.db import connection |
| 67 | + from pprint import pprint |
| 68 | + pprint(connection.queries) |
| 69 | + raise |
| 70 | + |
| 71 | + objects = [] # type: List[object] |
| 72 | + model_objects = {} |
| 73 | + |
| 74 | + import ietf.name.models |
| 75 | + from ietf.dbtemplate.models import DBTemplate |
| 76 | + from ietf.doc.models import BallotType, State, StateType |
| 77 | + from ietf.group.models import GroupFeatures |
| 78 | + from ietf.mailtrigger.models import MailTrigger, Recipient |
| 79 | + from ietf.stats.models import CountryAlias |
| 80 | + from ietf.utils.models import VersionInfo |
| 81 | + |
| 82 | + # Grab all ietf.name.models |
| 83 | + for n in dir(ietf.name.models): |
| 84 | + item = getattr(ietf.name.models, n) |
| 85 | + if inspect.isclass(item) and issubclass(item, ietf.name.models.NameModel): |
| 86 | + if not item._meta.abstract: |
| 87 | + model_objects[model_name(item)] = list(item.objects.all().order_by('pk')) |
| 88 | + |
| 89 | + |
| 90 | + for m in ( BallotType, State, StateType, GroupFeatures, MailTrigger, Recipient, CountryAlias, VersionInfo ): |
| 91 | + model_objects[model_name(m)] = list(m.objects.all().order_by('pk')) |
| 92 | + |
| 93 | + for m in ( DBTemplate, ): |
| 94 | + model_objects[model_name(m)] = [ m.objects.get(pk=354) ] |
| 95 | + |
| 96 | + for model_name in sorted(model_objects.keys()): |
| 97 | + objects += model_objects[model_name] |
| 98 | + |
| 99 | + output(objects) |
| 100 | + |
0 commit comments