2010-10-18 15:26:51 +02:00
|
|
|
# w.c.s. - web application for online forms
|
|
|
|
# Copyright (C) 2005-2010 Entr'ouvert
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2012-01-26 17:32:10 +01:00
|
|
|
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2010-10-18 15:26:51 +02:00
|
|
|
|
2019-11-12 08:59:27 +01:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2010-10-18 15:26:51 +02:00
|
|
|
import optparse
|
|
|
|
from optparse import make_option
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'Command',
|
|
|
|
]
|
|
|
|
|
2019-11-11 21:57:32 +01:00
|
|
|
from django.utils.six.moves import configparser as ConfigParser
|
|
|
|
|
2019-09-29 20:53:23 +02:00
|
|
|
from wcs import qommon
|
|
|
|
from . import _
|
2010-10-18 15:26:51 +02:00
|
|
|
qommon._commands = {}
|
|
|
|
|
2014-12-20 19:50:40 +01:00
|
|
|
class Command(object):
|
2010-10-18 15:26:51 +02:00
|
|
|
doc = ''
|
|
|
|
name = None
|
|
|
|
usage_args = '[ options ... ]'
|
|
|
|
|
|
|
|
def __init__(self, options=[]):
|
2012-01-10 16:56:52 +01:00
|
|
|
self.config = ConfigParser.ConfigParser()
|
|
|
|
self.options = options + [
|
|
|
|
make_option('--extra', metavar='DIR', action='append',
|
|
|
|
dest='extra', default=[]),
|
|
|
|
make_option('--app-dir', metavar='DIR', action='store',
|
|
|
|
dest='app_dir', default=None),
|
|
|
|
make_option('--data-dir', metavar='DIR', action='store',
|
|
|
|
dest='data_dir', default=None),
|
|
|
|
]
|
2010-10-18 15:26:51 +02:00
|
|
|
|
2012-01-10 16:56:52 +01:00
|
|
|
def run(self, args, base_options):
|
|
|
|
if base_options.configfile:
|
2012-01-26 11:12:54 +01:00
|
|
|
if not os.path.exists(base_options.configfile):
|
2019-11-12 08:59:27 +01:00
|
|
|
print('Missing configuration file %s' % base_options.configfile, file=sys.stderr)
|
2012-01-26 11:12:54 +01:00
|
|
|
sys.exit(1)
|
2012-01-10 16:56:52 +01:00
|
|
|
try:
|
|
|
|
self.config.read(base_options.configfile)
|
2019-11-12 08:54:00 +01:00
|
|
|
except ConfigParser.ParsingError as e:
|
2019-11-12 08:59:27 +01:00
|
|
|
print('Invalid configuration file %s' % base_options.configfile, file=sys.stderr)
|
|
|
|
print(e, file=sys.stderr)
|
2012-01-26 11:12:54 +01:00
|
|
|
sys.exit(1)
|
2012-01-10 16:56:52 +01:00
|
|
|
if not self.config.has_section('main'):
|
|
|
|
self.config.add_section('main')
|
|
|
|
sub_options, args = self.parse_args(args)
|
|
|
|
if sub_options.app_dir:
|
|
|
|
self.config.set("main", "app_dir", sub_options.app_dir)
|
|
|
|
if sub_options.data_dir:
|
|
|
|
self.config.set("main", "data_dir", sub_options.data_dir)
|
2015-03-09 14:58:31 +01:00
|
|
|
if sub_options.extra:
|
|
|
|
if not self.config.has_section('extra'):
|
|
|
|
self.config.add_section('extra')
|
|
|
|
for i, extra in enumerate(sub_options.extra):
|
|
|
|
self.config.set("extra", "cmd_line_extra_%d" % i, extra)
|
2012-01-10 16:56:52 +01:00
|
|
|
return self.execute(base_options, sub_options, args)
|
2010-10-18 15:26:51 +02:00
|
|
|
|
|
|
|
def parse_args(self, args):
|
|
|
|
self.parser = optparse.OptionParser(
|
|
|
|
usage='%%prog %s %s' % (self.name, _(self.usage_args)),
|
|
|
|
description=self.doc)
|
|
|
|
self.parser.add_options(self.options)
|
|
|
|
return self.parser.parse_args(args)
|
|
|
|
|
2012-01-10 16:56:52 +01:00
|
|
|
def execute(self, base_options, sub_options, args):
|
2010-10-18 15:26:51 +02:00
|
|
|
"""The body of the command"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2016-03-11 16:40:16 +01:00
|
|
|
@classmethod
|
2010-10-18 15:26:51 +02:00
|
|
|
def register(cls):
|
|
|
|
qommon._commands[cls.name] = cls
|
|
|
|
|
2014-12-20 19:50:40 +01:00
|
|
|
class Ctl(object):
|
2010-10-18 15:26:51 +02:00
|
|
|
def __init__(self, cmd_prefixes=[]):
|
|
|
|
self.cmd_prefixes = cmd_prefixes
|
|
|
|
self.parser = optparse.OptionParser(
|
|
|
|
usage='%prog [ -f config ] command [ options ... ]',
|
|
|
|
add_help_option=False)
|
|
|
|
self.parser.disable_interspersed_args()
|
|
|
|
self.parser.add_option('-f', '--file', action='store', metavar='CONFIG',
|
|
|
|
type='string', dest='configfile',
|
|
|
|
help=_('use a non default configuration file'))
|
|
|
|
self.parser.add_option('--help', action='callback',
|
|
|
|
callback=self.print_help,
|
|
|
|
help=_("Display this help and exit"))
|
|
|
|
|
2014-12-28 19:38:41 +01:00
|
|
|
def load_all_commands(self, ignore_errors=True):
|
2010-10-18 15:26:51 +02:00
|
|
|
for cmd_prefix in self.cmd_prefixes:
|
|
|
|
if not cmd_prefix in sys.modules:
|
|
|
|
__import__(cmd_prefix)
|
|
|
|
mod = sys.modules.get(cmd_prefix)
|
|
|
|
if not mod:
|
|
|
|
continue
|
|
|
|
if os.path.isdir(mod.__file__):
|
|
|
|
cmddir = os.path.abspath(mod.__file__)
|
|
|
|
else:
|
|
|
|
cmddir = os.path.abspath(os.path.dirname(mod.__file__))
|
|
|
|
for fname in os.listdir(os.path.join(cmddir)):
|
|
|
|
name, ext = os.path.splitext(fname)
|
|
|
|
if not ext == '.py':
|
|
|
|
continue
|
|
|
|
if name.startswith('_'):
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
__import__('%s.%s' % (cmd_prefix, name))
|
2014-12-28 19:38:41 +01:00
|
|
|
except ImportError as e:
|
|
|
|
if not ignore_errors:
|
|
|
|
raise e
|
|
|
|
|
|
|
|
def get_commands(self):
|
|
|
|
return qommon._commands
|
2010-10-18 15:26:51 +02:00
|
|
|
|
2013-03-29 13:46:45 +01:00
|
|
|
def print_help(self, *args):
|
|
|
|
self.parser.print_help()
|
|
|
|
self.load_all_commands()
|
2019-11-12 08:59:27 +01:00
|
|
|
print()
|
2013-03-29 13:46:45 +01:00
|
|
|
|
2015-02-05 11:45:43 +01:00
|
|
|
commands = [(x.name, x.doc) for x in self.get_commands().values()]
|
2010-10-18 15:26:51 +02:00
|
|
|
commands.sort()
|
2019-11-12 08:59:27 +01:00
|
|
|
print('Available commands:')
|
2010-10-18 15:26:51 +02:00
|
|
|
for name, description in commands:
|
2019-11-12 08:59:27 +01:00
|
|
|
print(' %-15s %s' % (name, description))
|
2010-10-18 15:26:51 +02:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
def run(self, args):
|
|
|
|
options, args = self.parser.parse_args(args)
|
2012-01-10 16:56:52 +01:00
|
|
|
if not args:
|
|
|
|
self.parser.error('You must use a command')
|
2010-10-18 15:26:51 +02:00
|
|
|
command, args = args[0], args[1:]
|
2013-03-29 13:46:45 +01:00
|
|
|
|
2014-12-28 19:38:41 +01:00
|
|
|
if command not in self.get_commands():
|
2013-03-29 13:46:45 +01:00
|
|
|
# load a module named like the command, this is the common case
|
2010-10-18 15:26:51 +02:00
|
|
|
for cmd_prefix in self.cmd_prefixes:
|
|
|
|
try:
|
|
|
|
__import__('%s.%s' % (cmd_prefix, command))
|
|
|
|
except ImportError:
|
|
|
|
pass
|
2013-03-29 13:46:45 +01:00
|
|
|
|
2014-12-28 19:38:41 +01:00
|
|
|
if command not in self.get_commands():
|
2013-03-29 13:46:45 +01:00
|
|
|
# if the command could not be loaded from a same-name module,
|
|
|
|
# go over all modules
|
|
|
|
self.load_all_commands()
|
|
|
|
|
2014-12-28 19:38:41 +01:00
|
|
|
command_class = self.get_commands()[command]
|
2010-10-18 15:26:51 +02:00
|
|
|
cmd = command_class()
|
2012-01-10 16:56:52 +01:00
|
|
|
return cmd.run(args, options)
|
2010-10-18 15:26:51 +02:00
|
|
|
|