diff --git a/modoboa/core/commands/__init__.py b/modoboa/core/commands/__init__.py new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/modoboa/core/commands/__init__.py @@ -0,0 +1 @@ + diff --git a/modoboa/core/commands/deploy.py b/modoboa/core/commands/deploy.py new file mode 100644 index 000000000..3043265f4 --- /dev/null +++ b/modoboa/core/commands/deploy.py @@ -0,0 +1,32 @@ +import sys +import os +import shutil +from django.core import management +from modoboa.core.management import Command + +class DeployCommand(Command): + + def __init__(self): + super(DeployCommand, self).__init__() + self._parser.add_argument('name', type=str, + help='The name of your Modoboa instance') + + def handle(self, parsed_args): + management.call_command('startproject', parsed_args.name, verbosity=False) + if os.path.exists("%(name)s/%(name)s" % {'name' : parsed_args.name}): + # Django 1.4+ + path = "%(name)s/%(name)s" % {'name' : parsed_args.name} + sys.path.append(parsed_args.name) + else: + path = parsed_args.name + sys.path.append(".") + mod = __import__(parsed_args.name, globals(), locals(), ['settings']) + tpl = self._render_template("%s/settings.py" % self._templates_dir, { + 'secret_key' : mod.settings.SECRET_KEY, + 'name' : parsed_args.name + }) + fp = open("%s/settings.py" % path, "w") + fp.write(tpl) + fp.close() + shutil.copyfile("%s/urls.py" % self._templates_dir, "%s/urls.py" % path) + diff --git a/modoboa/core/commands/postfix_maps.py b/modoboa/core/commands/postfix_maps.py new file mode 100644 index 000000000..a55edb4a5 --- /dev/null +++ b/modoboa/core/commands/postfix_maps.py @@ -0,0 +1,8 @@ + +from modoboa.core.management import Command + +class PostfixMapsCommand(Command): + def __init__(self): + super(PostfixMapsCommand, self).__init__() + self._parser.add_argument('--dbtype', type=str, choices=['mysql', 'postgres'], + help='The database type you use') diff --git a/modoboa/core/management.py b/modoboa/core/management.py index 6302da2aa..ee38e8b3b 100644 --- a/modoboa/core/management.py +++ b/modoboa/core/management.py @@ -1,12 +1,10 @@ # coding: utf-8 -from django.core import management from django.conf import settings from django.template import Context, Template import argparse -import sys import os -import shutil +import sys class Command(object): @@ -14,6 +12,7 @@ class Command(object): self._parser = argparse.ArgumentParser() if not settings.configured: settings.configure() + self._templates_dir = "%s/templates" % os.path.dirname(__file__) def _render_template(self, tplfile, env): fp = open(tplfile) @@ -28,43 +27,20 @@ class Command(object): def handle(self, parsed_args): raise NotImplemented -class DeployCommand(Command): - - def __init__(self): - super(DeployCommand, self).__init__() - self._parser.add_argument('name', type=str, - help='The name of your Modoboa instance') - - def handle(self, parsed_args): - management.call_command('startproject', parsed_args.name, verbosity=False) - templates_dir = "%s/templates" % os.path.dirname(__file__) - if os.path.exists("%(name)s/%(name)s" % {'name' : parsed_args.name}): - # Django 1.4+ - path = "%(name)s/%(name)s" % {'name' : parsed_args.name} - sys.path.append(parsed_args.name) - else: - path = parsed_args.name - sys.path.append(".") - mod = __import__(parsed_args.name, globals(), locals(), ['settings']) - tpl = self._render_template("%s/settings.py" % templates_dir, { - 'secret_key' : mod.settings.SECRET_KEY, - 'name' : parsed_args.name - }) - fp = open("%s/settings.py" % path, "w") - fp.write(tpl) - fp.close() - shutil.copyfile("%s/urls.py" % templates_dir, "%s/urls.py" % path) - - def handle_command_line(): parser = argparse.ArgumentParser() parser.add_argument('command', type=str, help='A valid command name') (args, remaining) = parser.parse_known_args() + try: - cmd = globals()["%sCommand" % args.command.capitalize()]() - except KeyError: + cmdmod = __import__("modoboa.core.commands", globals(), locals(), + [args.command]) + cmdmod = getattr(cmdmod, args.command) + cmdclass = getattr(cmdmod, "%sCommand" % args.command.capitalize()) + cmd = cmdclass() + except ImportError: print >>sys.stderr, "Unknown command '%s'" % args.command sys.exit(1)