src/cm/monkey_patches.py
author Production Moz <dev@sopinspace.com>
Tue, 31 May 2011 17:58:47 +0200
changeset 347 0a359382701b
child 504 b2e0186daa5b
permissions -rw-r--r--
prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
347
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
     1
from south.management.commands.migrate import  Command
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
     2
from south.db import DEFAULT_DB_ALIAS
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
     3
from south import migration
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
     4
import sys
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
     5
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
     6
### RBA+GIB: prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
     7
def new_handle(self, app=None, target=None, skip=False, merge=False, backwards=False, fake=False, db_dry_run=False, show_list=False, database=DEFAULT_DB_ALIAS, delete_ghosts=False, ignore_ghosts=False, **options):
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
     8
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
     9
    # NOTE: THIS IS DUPLICATED FROM django.core.management.commands.syncdb
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    10
    # This code imports any module named 'management' in INSTALLED_APPS.
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    11
    # The 'management' module is the preferred way of listening to post_syncdb
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    12
    # signals, and since we're sending those out with create_table migrations,
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    13
    # we need apps to behave correctly.
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    14
    from django.conf import settings
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    15
    for app_name in settings.INSTALLED_APPS:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    16
        try:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    17
            __import__(app_name + '.management', {}, {}, [''])
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    18
        except ImportError, exc:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    19
            msg = exc.args[0]
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    20
            if (not msg.startswith('No module named') and not msg.endswith(' is unknown') ) or 'management' not in msg:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    21
                raise
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    22
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    23
    # END DJANGO DUPE CODE
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    24
        
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    25
    # if all_apps flag is set, shift app over to target
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    26
    if options.get('all_apps', False):
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    27
        target = app
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    28
        app = None
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    29
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    30
    # Migrate each app
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    31
    if app:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    32
        try:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    33
            apps = [Migrations(app)]
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    34
        except NoMigrations:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    35
            print "The app '%s' does not appear to use migrations." % app
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    36
            print "./manage.py migrate " + self.args
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    37
            return
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    38
    else:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    39
        apps = list(migration.all_migrations())
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    40
        
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    41
    # Do we need to show the list of migrations?
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    42
    if show_list and apps:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    43
        list_migrations(apps, database)
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    44
        
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    45
    if not show_list:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    46
            
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    47
        for app in apps:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    48
            result = migration.migrate_app(
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    49
                app,
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    50
                target_name = target,
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    51
                fake = fake,
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    52
                db_dry_run = db_dry_run,
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    53
                verbosity = int(options.get('verbosity', 0)),
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    54
                interactive = options.get('interactive', True),
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    55
                load_initial_data = not options.get('no_initial_data', False),
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    56
                merge = merge,
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    57
                skip = skip,
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    58
                database = database,
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    59
                delete_ghosts = delete_ghosts,
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    60
                ignore_ghosts = ignore_ghosts,
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    61
            )
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    62
            if result is False:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    63
                sys.exit(1) # Migration failed, so the command fails.
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    64
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    65
Command.handle = new_handle