Statistics
| Branch: | Tag: | Revision:

root / snf-app / synnefo / manage.py @ 3bd6223a

History | View | Annotate | Download (3.2 kB)

1
#!/usr/bin/env python
2

    
3
from django.core.management import ManagementUtility, setup_environ, \
4
BaseCommand, LaxOptionParser, handle_default_options
5

    
6
from optparse import Option, make_option
7
from synnefo import get_version
8

    
9
import sys
10
import os
11

    
12
class SynnefoManagementUtility(ManagementUtility):
13
    """
14
    Override django ManagementUtility to allow us provide a custom
15
    --settings-dir option for synnefo application.
16

17
    Most of the following code is a copy from django.core.management module
18
    """
19

    
20
    def execute(self):
21
        """
22
        Given the command-line arguments, this figures out which subcommand is
23
        being run, creates a parser appropriate to that command, and runs it.
24
        """
25

    
26
        # --settings-dir option
27
        # will remove it later to avoid django commands from raising errors
28
        option_list = BaseCommand.option_list + (
29
            make_option('--settings-dir',
30
                action='store',
31
                dest='settings_dir',
32
                default=None,
33
                help='Load *.conf files from directory as settings'),)
34

    
35
        # Preprocess options to extract --settings and --pythonpath.
36
        # These options could affect the commands that are available, so they
37
        # must be processed early.
38
        parser = LaxOptionParser(usage="%prog subcommand [options] [args]",
39
                                 version=get_version(),
40
                                 option_list=option_list)
41
        self.autocomplete()
42
        try:
43
            options, args = parser.parse_args(self.argv)
44
            handle_default_options(options)
45
        except:
46
            pass # Ignore any option errors at this point.
47

    
48
        # user provides custom settings dir
49
        # set it as environmental variable and remove it from self.argv
50
        if options.settings_dir:
51
            os.environ['SYNNEFO_SETTINGS_DIR'] = options.settings_dir
52
            for arg in self.argv:
53
                if arg.startswith('--settings-dir'):
54
                    self.argv.remove(arg)
55

    
56
        try:
57
            subcommand = self.argv[1]
58
        except IndexError:
59
            subcommand = 'help' # Display help if no arguments were given.
60

    
61
        if subcommand == 'help':
62
            if len(args) > 2:
63
                self.fetch_command(args[2]).print_help(self.prog_name, args[2])
64
            else:
65
                parser.print_lax_help()
66
                sys.stdout.write(self.main_help_text() + '\n')
67
                sys.exit(1)
68
        # Special-cases: We want 'django-admin.py --version' and
69
        # 'django-admin.py --help' to work, for backwards compatibility.
70
        elif self.argv[1:] == ['--version']:
71
            # LaxOptionParser already takes care of printing the version.
72
            pass
73
        elif self.argv[1:] in (['--help'], ['-h']):
74
            parser.print_lax_help()
75
            sys.stdout.write(self.main_help_text() + '\n')
76
        else:
77
            self.fetch_command(subcommand).run_from_argv(self.argv)
78

    
79
def main():
80
    # no need to run setup_environ
81
    # we already know our project
82
    os.environ['DJANGO_SETTINGS_MODULE'] = os.environ.get('DJANGO_SETTINGS_MODULE',
83
                                                          'synnefo.settings')
84
    mu = SynnefoManagementUtility(sys.argv)
85
    mu.execute()
86

    
87
if __name__ == "__main__":
88
    main()