156 lines
6.3 KiB
Python
156 lines
6.3 KiB
Python
import argparse
|
|
import os
|
|
import posix
|
|
import sys
|
|
import pkg_resources
|
|
from django.core.management import execute_from_command_line
|
|
|
|
from dav_base.config.modules import DJANGO_MAIN_MODULE, ModuleConfig
|
|
|
|
VERSION = '0.1'
|
|
|
|
|
|
class AdminCommand: # pylint: disable=too-few-public-methods
|
|
def _setup_argparser(self):
|
|
kwargs = {
|
|
'description': 'Tool to manage the DAV django project installation.',
|
|
'epilog': 'Off Belay.',
|
|
}
|
|
parser = argparse.ArgumentParser(**kwargs)
|
|
|
|
parser.add_argument('-V', '--version', action='version',
|
|
version='%(prog)s ' + VERSION)
|
|
|
|
subparsers = parser.add_subparsers(dest='subcmd', metavar='CMD',
|
|
title='subcommands',
|
|
description="Use '%(prog)s CMD -h' to show help for a subcommand")
|
|
subparser = subparsers.add_parser('setup', help='Setup the installation.')
|
|
subparser.add_argument('path', metavar='PATH',
|
|
help='A directory, where the project files will be installed.')
|
|
|
|
subparser = subparsers.add_parser('enable_module',
|
|
help='Enable a modular app within your django-dav installation.')
|
|
subparser.add_argument('path', metavar='PATH',
|
|
help='The directory, where your django project files are installed.')
|
|
subparser.add_argument('module', metavar='MODULE',
|
|
help='The name of the module.')
|
|
|
|
subparser = subparsers.add_parser('disable_module',
|
|
help='Disable a modular app within your django-dav installation.')
|
|
subparser.add_argument('path', metavar='PATH',
|
|
help='The directory, where your django project files are installed.')
|
|
subparser.add_argument('module', metavar='MODULE',
|
|
help='The name of the module.')
|
|
|
|
subparser = subparsers.add_parser('list_modules',
|
|
help='List enabled modular apps within your django-dav installation.')
|
|
subparser.add_argument('path', metavar='PATH',
|
|
help='The directory, where your django project files are installed.')
|
|
|
|
return parser
|
|
|
|
def _parse_args(self, argv=None):
|
|
if argv is None:
|
|
argv = sys.argv[1:]
|
|
if not argv:
|
|
argv = ['--help']
|
|
|
|
return self._argparser.parse_args(argv)
|
|
|
|
def _subcmd_setup(self, cmd_args):
|
|
django_main_module = DJANGO_MAIN_MODULE
|
|
django_base_dir = cmd_args.path
|
|
|
|
sys.stdout.write('Setup installation in \'{path}\'...\n'.format(path=django_base_dir))
|
|
|
|
if os.path.exists(django_base_dir):
|
|
if not os.path.isdir(django_base_dir):
|
|
sys.stderr.write('{path}: Not a directory.\n'.format(path=django_base_dir))
|
|
return posix.EX_USAGE
|
|
else:
|
|
os.makedirs(django_base_dir)
|
|
|
|
sys.stdout.write('Creating django project...\n')
|
|
django_cmd = 'django-admin startproject {name} "{path}"'.format(name=django_main_module,
|
|
path=django_base_dir)
|
|
exitval = os.system(django_cmd)
|
|
if exitval != posix.EX_OK:
|
|
return exitval
|
|
|
|
sys.stdout.write('Creating directories...\n')
|
|
dirs = [
|
|
os.path.join(django_base_dir, 'common', 'templates'),
|
|
os.path.join(django_base_dir, 'var', 'db'),
|
|
os.path.join(django_base_dir, 'var', 'log'),
|
|
os.path.join(django_base_dir, 'var', 'www', 'static'),
|
|
]
|
|
|
|
for d in dirs:
|
|
sys.stdout.write(' - %s\n' % d)
|
|
os.makedirs(d)
|
|
|
|
sys.stdout.write('Configure django project...\n')
|
|
|
|
config = ModuleConfig(django_base_dir=django_base_dir)
|
|
config.save()
|
|
|
|
input_file = os.path.join('django_project_config', 'additional_settings.py')
|
|
output_file = os.path.join(django_base_dir, django_main_module, 'settings.py')
|
|
with open(output_file, 'ab') as f:
|
|
f.write(pkg_resources.resource_string(__package__, input_file))
|
|
|
|
input_file = os.path.join('django_project_config', 'urls.py')
|
|
output_file = os.path.join(django_base_dir, django_main_module, 'urls.py')
|
|
with open(output_file, 'wb') as f:
|
|
f.write(pkg_resources.resource_string(__package__, input_file))
|
|
|
|
input_file = os.path.join('django_project_config', 'settings-dav_base.py')
|
|
output_file = os.path.join(django_base_dir, django_main_module, 'settings-dav_base.py')
|
|
with open(output_file, 'wb') as f:
|
|
f.write(pkg_resources.resource_string(__package__, input_file))
|
|
|
|
return posix.EX_OK
|
|
|
|
def _subcmd_enable_module(self, cmd_args):
|
|
django_main_module = DJANGO_MAIN_MODULE
|
|
django_base_dir = cmd_args.path
|
|
module_name = cmd_args.module
|
|
sys.path.append(django_base_dir)
|
|
os.environ['DJANGO_SETTINGS_MODULE'] = '{}.settings'.format(django_main_module)
|
|
execute_from_command_line(['manage.py', 'enable_module', module_name])
|
|
return posix.EX_OK
|
|
|
|
def _subcmd_disable_module(self, cmd_args):
|
|
django_main_module = DJANGO_MAIN_MODULE
|
|
django_base_dir = cmd_args.path
|
|
module_name = cmd_args.module
|
|
sys.path.append(django_base_dir)
|
|
os.environ['DJANGO_SETTINGS_MODULE'] = '{}.settings'.format(django_main_module)
|
|
execute_from_command_line(['manage.py', 'disable_module', module_name])
|
|
return posix.EX_OK
|
|
|
|
def _subcmd_list_modules(self, cmd_args):
|
|
django_main_module = DJANGO_MAIN_MODULE
|
|
django_base_dir = cmd_args.path
|
|
sys.path.append(django_base_dir)
|
|
os.environ['DJANGO_SETTINGS_MODULE'] = '{}.settings'.format(django_main_module)
|
|
execute_from_command_line(['manage.py', 'list_modules'])
|
|
return posix.EX_OK
|
|
|
|
def __init__(self):
|
|
self._argparser = self._setup_argparser()
|
|
|
|
def __call__(self, argv=None):
|
|
cmd_args = self._parse_args(argv)
|
|
subcmd = cmd_args.subcmd
|
|
method_name = '_subcmd_{}'.format(subcmd)
|
|
method = getattr(self, method_name)
|
|
exitval = method(cmd_args)
|
|
return exitval
|
|
|
|
|
|
def main():
|
|
cmd = AdminCommand()
|
|
exitval = cmd()
|
|
sys.exit(exitval)
|