~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to pybb/management/commands/pybb_migrate.py

  • Committer: Holger Rapp
  • Date: 2009-02-25 16:55:36 UTC
  • Revision ID: sirver@kallisto.local-20090225165536-3abfhjx8qsgtzyru
- Added my hacked version of pybb. Remerging new versions is very difficult at this point :(

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from optparse import make_option
 
2
import os
 
3
 
 
4
from django.core.management.base import BaseCommand, CommandError
 
5
 
 
6
import pybb
 
7
 
 
8
class Command(BaseCommand):
 
9
    option_list = BaseCommand.option_list + ( 
 
10
        make_option('-l', '--list', dest='list', action='store_true', default=False,
 
11
            help='Show all available migrations'),
 
12
        make_option('-e', '--exec', dest='migration', help='Execute the migration')
 
13
    )   
 
14
    help = 'Execute migration scripts.'
 
15
 
 
16
    def handle(self, *args, **kwargs):
 
17
        if kwargs['list']:
 
18
            self.command_list()
 
19
        elif kwargs['migration']:
 
20
            self.command_migrate(kwargs['migration'])
 
21
        else:
 
22
            print 'Invalid options'
 
23
 
 
24
 
 
25
    def command_list(self):
 
26
        root = os.path.dirname(os.path.realpath(pybb.__file__))
 
27
        dir = os.path.join(root, 'migrations')
 
28
        migs = []
 
29
        for fname in os.listdir(dir):
 
30
            if fname.endswith('.py'):
 
31
                mod_name = fname[:-3]
 
32
                mod = __import__('pybb.migrations.%s' % mod_name,
 
33
                                 globals(), locals(), ['foobar'])
 
34
                if hasattr(mod, 'migrate'):
 
35
                    migs.append((mod_name, mod))
 
36
 
 
37
        migs = sorted(migs, lambda a, b: cmp(a[0], b[0]))
 
38
        for name, mig in migs:
 
39
            print '%s - %s' % (name, mig.DESCRIPTION)
 
40
 
 
41
 
 
42
    def command_migrate(self, mod_name):
 
43
        mod = __import__('pybb.migrations.%s' % mod_name,
 
44
                         globals(), locals(), ['foobar'])
 
45
        mod.migrate()