<commit_before>
import datetime
import os
import sys
from django.conf import settings
from django.db import models
from models import MigrationHistory
from south.db import db


def get_migrated_apps():
    for app in models.get_apps():
        if get_migration_package(app):
            yield app


def get_app_name(app):
    """
    Returns the _internal_ app name for the given app models module.
    i.e. for <module django.contrib.auth.models> will return 'auth'
    """
    if isinstance(app, (str, unicode)):
        # If it's a string, use the models module
        app = models.get_app(app)
    return app.__name__.split('.')[-2]


def short_from_long(app):
    return app.split(".")[-1]


def get_migration_package(app):
    """
    Returns the migrations module for the given app, or None
    if it does not use migrations.
    """
    if isinstance(app, (str, unicode)):
        # If it's a string, use the models module
        app = models.get_app(app)
    app_name = '.'.join( app.__name__.split('.')[0:-1] )
    mod = __import__(app_name, {}, {}, ['migrations'])
    if hasattr(mod, 'migrations'):
        return getattr(mod, 'migrations')


def get_migration_names(app):
    """
    Returns a list of migration file names for the given app.
    """
    return sorted([
        filename[:-3]
        for filename in os.listdir(os.path.dirname(get_migration_package(app).__file__))
        if filename.endswith(".py") and filename != "__init__.py"
    ])


def get_migration_classes(app):
    """
    Returns a list of migration classes (one for each migration) for the app.
    """
    for name in get_migration_names(app):
        yield get_migration(app, name)


def get_migration(app, name):
    """
    Returns the migration class implied by 'name'.
    """
    if not isinstance(app, (str, unicode)):
        app = get_app_name(app)
    module = __import__(app + ".migrations." + name, '', '', ['Migration'])
    return module.Migration


def all_migrations():
    return dict([
        (app, dict([(name, get_migration(app, name)) for name in get_migration_names(app)]))
        for app in get_migrated_apps()
    ])


def dependency_tree():
    tree = all_migrations()
    
    # Annotate tree with 'backwards edges'
    for app, classes in tree.items():
        for name, cls in classes.items():
            cls.needs = []
            if not hasattr(cls, "needed_by"):
                cls.needed_by = []
            if hasattr(cls, "depends_on"):
                for dapp, dname in cls.depends_on:
                    dapp = models.get_app(dapp)
                    if dapp not in tree:
                        print "Migration %s in app %s depends on unmigrated app %s." % (
                            name,
                            get_app_name(app),
                            dapp,
                        )
                        sys.exit(1)
                    if dname not in tree[dapp]:<commit_msg>Do imports correctly, so apps not on pythonpath directly work.<commit_after>
import datetime
import os
import sys
from django.conf import settings
from django.db import models
from models import MigrationHistory
from south.db import db


def get_migrated_apps():
    for app in models.get_apps():
        if get_migration_package(app):
            yield app


def get_app_name(app):
    """
    Returns the _internal_ app name for the given app models module.
    i.e. for <module django.contrib.auth.models> will return 'auth'
    """
    if isinstance(app, (str, unicode)):
        # If it's a string, use the models module
        app = models.get_app(app)
    return app.__name__.split('.')[-2]


def get_app_fullname(app):
    """
    Returns the full python name of an app - e.g. django.contrib.auth
    """
    return app.__name__[:-7]


def short_from_long(app):
    return app.split(".")[-1]


def get_migration_package(app):
    """
    Returns the migrations module for the given app, or None
    if it does not use migrations.
    """
    if isinstance(app, (str, unicode)):
        # If it's a string, use the models module
        app = models.get_app(app)
    mod = __import__(get_app_fullname(app), {}, {}, ['migrations'])
    if hasattr(mod, 'migrations'):
        return getattr(mod, 'migrations')


def get_migration_names(app):
    """
    Returns a list of migration file names for the given app.
    """
    return sorted([
        filename[:-3]
        for filename in os.listdir(os.path.dirname(get_migration_package(app).__file__))
        if filename.endswith(".py") and filename != "__init__.py"
    ])


def get_migration_classes(app):
    """
    Returns a list of migration classes (one for each migration) for the app.
    """
    for name in get_migration_names(app):
        yield get_migration(app, name)


def get_migration(app, name):
    """
    Returns the migration class implied by 'name'.
    """
    if not isinstance(app, (str, unicode)):
        app = get_app_fullname(app)
    module = __import__(app + ".migrations." + name, '', '', ['Migration'])
    return module.Migration


def all_migrations():
    return dict([
        (app, dict([(name, get_migration(app, name)) for name in get_migration_names(app)]))
        for app in get_migrated_apps()
    ])


def dependency_tree():
    tree = all_migrations()
    
    # Annotate tree with 'backwards edges'
    for app, classes in tree.items():
        for name, cls in classes.items():
            cls.needs = []
            if not hasattr(cls, "needed_by"):
                cls.needed_by = []
            if hasattr(cls, "depends_on"):
                for dapp, dname in cls.depends_on:
                    dapp = models.get_app(dapp)
                    if dapp not in tree:
                        print "Migration %s in app %s depends on unmigrated app %s." % (
                            name,
                            get_app_name(app),
                            dapp,
                        )
                        sys.exit(1)
                    if dname not in tree[dapp]:
{'fraction_non_alphanumeric': 0.11074215507222314, 'fraction_numerical': 0.0018263323924954341, 'mean_word_length': 2.1940615058324497, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6457032', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1782, 'n_words': 547}