mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-18 05:51:26 +02:00
wip: command-line management tool
Splitting commands into separate files. see #314
This commit is contained in:
@@ -0,0 +1 @@
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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')
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user