<commit_before>import json
from django.core.management.base import BaseCommand, CommandError
from pervert.models import AbstractPervert, SchemaState, PervertError

class Command(BaseCommand):
    help = "Registers new schema for Pervert-controlled models"

    def handle(self, *args, **options):
        ret = []
        print "Reading the schema of Pervert-controlled models..."
        for cl in AbstractPervert.__subclasses__():
            state = {
                "app_label": cl._meta.app_label,
                "model": cl._meta.object_name,
                "fields": [],
                "fks": []
            }
            print "%s.models.%s" % (state["app_label"], state["model"],)
            for field in cl._meta.fields:
                print " * %s" % field.name
                if field.name == "uid":
                    continue
                if field.__class__.__name__ == "ForeignKey":
                    state["fks"].append(field.name)
                else:
                    state["fields"].append(field.name)
            ret.append(state)
        ss = SchemaState(state = json.dumps(ret))
        ss.save()
        print "SchemaState saved on %s" % ss.when

"""
from south.signals import post_migrate
from south.models import MigrationHistory
post_migrate.connect(Command.handle)

try:
    from south.signals import post_migrate
    from south.models import MigrationHistory
    post_migrate.connect(Command.handle)
except ImportError:
    pass
"""

<commit_msg>Make "pervert_migrate" check whether there have been any changes since the last migration and quit if not.<commit_after>import json
from django.core.management.base import BaseCommand, CommandError
from pervert.models import AbstractPervert, SchemaState, PervertError

class Command(BaseCommand):
    help = "Registers new schema for Pervert-controlled models"

    def handle(self, *args, **options):
        states = []
        print "Reading the schema of Pervert-controlled models..."
        state_text = ""
        for cl in AbstractPervert.__subclasses__():
            state = {
                "app_label": cl._meta.app_label,
                "model": cl._meta.object_name,
                "fields": [],
                "fks": []
            }
            state_text += "%s.models.%s\n" % (state["app_label"], state["model"],)
            for field in cl._meta.fields:
                state_text += " * %s\n" % field.name
                if field.name == "uid":
                    continue
                if field.__class__.__name__ == "ForeignKey":
                    state["fks"].append(field.name)
                else:
                    state["fields"].append(field.name)
            # Sort to make sure there is a unique json representation of each state
            state["fields"].sort()
            state["fks"].sort()
            states.append(state)

        jsonstate = json.dumps(states)
        # If the json is identical to the last saved state
        if SchemaState.objects.count() and \
            SchemaState.objects.order_by("-when")[0].state == jsonstate:
            print "The state hasn't changed, nothing to do."
        else:
            # Save new state
            ss = SchemaState(state = json.dumps(states))
            ss.save()
            print state_text + "SchemaState saved on %s" % ss.when


{'fraction_non_alphanumeric': 0.1074107410741074, 'fraction_numerical': 0.00030003000300030005, 'mean_word_length': 1.9635555555555555, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2227209', 'n_tokens_mistral': 897, 'n_tokens_neox': 865, 'n_words': 238}