# Path: baobab/utils/handle_south.py
# class HandleSouth(object):
# 
#     apps = None
# 
#     @classmethod
#     def get_apps(cls):
#         if not cls.apps:
#             app = filter(lambda x: x.startswith('baobab.'),
#                          settings.INSTALLED_APPS)
#             app = map(lambda x: x.rsplit('.', 1)[1], app)
#             ignore = ['ignore'] * len(app)
#             cls.apps = SortedDict(zip(app, ignore))
#         return cls.apps
# 
#     @classmethod
#     def disable(cls):
#         settings.SOUTH_MIGRATION_MODULES = cls.get_apps()
# 
#     @classmethod
#     def enable(cls):
#         if getattr(settings, 'SOUTH_MIGRATION_MODULES'):
#             del settings.SOUTH_MIGRATION_MODULES


import os
import re
import sys
from StringIO import StringIO
from django.conf import settings
from django.core.management import call_command
from django.core.management.base import BaseCommand
from south.models import MigrationHistory
from baobab.utils.handle_south import HandleSouth

        print 'backup the new fixtures: %s' % app
        backup = sys.stdout
        file_ = open('%s/db_%s.json' % (settings.FIXTURE_DIRS[0], app), 'w')
        sys.stdout = StringIO()
        call_command('dumpdata', app, indent=2)
        sys.stdout.seek(0)
        self.write(file_, sys.stdout.readlines())

        file_.close()
        sys.stdout.close()
        sys.stdout = backup

    def has_fixtures(self, app):
        if not self.fixtures:
            for rep in settings.FIXTURE_DIRS:
                self.fixtures.extend(map(lambda x: x.split('.', 1)[0],
                                         os.listdir(rep)))
        return 'db_%s' % app in self.fixtures

    def handle(self, *args, **options):
        if 'test' not in sys.argv or not settings.FIXTURE_DIRS:
            print 'ERROR: not in test env'
            exit(1)
        HandleSouth.disable()
        call_command('syncdb', interactive=False)
        HandleSouth.enable()
        # migration already done
        call_command('loaddata', 'db_south', verbosity='0')
        call_command('loaddata', 'db_user', verbosity='0')

# Path: baobab/utils/handle_south.py
# class HandleSouth(object):
# 
#     apps = None
# 
#     @classmethod
#     def get_apps(cls):
#         if not cls.apps:
#             app = filter(lambda x: x.startswith('baobab.'),
#                          settings.INSTALLED_APPS)
#             app = map(lambda x: x.rsplit('.', 1)[1], app)
#             ignore = ['ignore'] * len(app)
#             cls.apps = SortedDict(zip(app, ignore))
#         return cls.apps
# 
#     @classmethod
#     def disable(cls):
#         settings.SOUTH_MIGRATION_MODULES = cls.get_apps()
# 
#     @classmethod
#     def enable(cls):
#         if getattr(settings, 'SOUTH_MIGRATION_MODULES'):
#             del settings.SOUTH_MIGRATION_MODULES


# Path: baobab/backoffice/management/commands/migrate_fixtures.py
import os
import re
import sys
from StringIO import StringIO
from django.conf import settings
from django.core.management import call_command
from django.core.management.base import BaseCommand
from south.models import MigrationHistory
from baobab.utils.handle_south import HandleSouth


        print 'backup the new fixtures: %s' % app
        backup = sys.stdout
        file_ = open('%s/db_%s.json' % (settings.FIXTURE_DIRS[0], app), 'w')
        sys.stdout = StringIO()
        call_command('dumpdata', app, indent=2)
        sys.stdout.seek(0)
        self.write(file_, sys.stdout.readlines())

        file_.close()
        sys.stdout.close()
        sys.stdout = backup

    def has_fixtures(self, app):
        if not self.fixtures:
            for rep in settings.FIXTURE_DIRS:
                self.fixtures.extend(map(lambda x: x.split('.', 1)[0],
                                         os.listdir(rep)))
        return 'db_%s' % app in self.fixtures

    def handle(self, *args, **options):
        if 'test' not in sys.argv or not settings.FIXTURE_DIRS:
            print 'ERROR: not in test env'
            exit(1)
        HandleSouth.disable()
        call_command('syncdb', interactive=False)
        HandleSouth.enable()
        # migration already done
        call_command('loaddata', 'db_south', verbosity='0')
        call_command('loaddata', 'db_user', verbosity='0')

        for app in HandleSouth.get_apps():