Rewrote command line handling, to use OptionParser
This commit is contained in:
parent
89065b34f8
commit
84e6cd0513
|
@ -16,25 +16,36 @@
|
||||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||||
# 02110-1301 USA
|
# 02110-1301 USA
|
||||||
|
|
||||||
import wcs.workflows as workflows
|
from qommon.ctl import Command, make_option
|
||||||
|
|
||||||
import publisher
|
class CmdApplyTimeouts(Command):
|
||||||
|
name = 'apply_timeouts'
|
||||||
|
|
||||||
def apply_timeouts(args):
|
def __init__(self):
|
||||||
i = 0
|
Command.__init__(self, [
|
||||||
while i < len(args):
|
make_option('--extra', metavar='DIR', action='append',
|
||||||
if args[i] == '--extra':
|
dest='extra', default=[]),
|
||||||
publisher.WcsPublisher.register_extra_dir(args[i+1])
|
make_option('--app-dir', metavar='DIR', action='store',
|
||||||
i += 1
|
dest='app_dir', default=None),
|
||||||
elif args[i] == '--app-dir':
|
make_option('--data-dir', metavar='DIR', action='store',
|
||||||
publisher.WcsPublisher.APP_DIR = args[i+1]
|
dest='data_dir', default=None),
|
||||||
i += 1
|
make_option('--silent', action='store_true',
|
||||||
elif args[i] == '--data-dir':
|
dest='silent', default=False),
|
||||||
publisher.WcsPublisher.DATA_DIR = args[i+1]
|
])
|
||||||
i += 1
|
|
||||||
elif args[i] == '--silent':
|
def execute(self, options, args):
|
||||||
|
import wcs.workflows as workflows
|
||||||
|
import publisher
|
||||||
|
|
||||||
|
if options.app_dir:
|
||||||
|
publisher.WcsPublisher.APP_DIR = options.app_dir
|
||||||
|
if options.data_dir:
|
||||||
|
publisher.WcsPublisher.DATA_DIR = options.data_dir
|
||||||
|
if options.silent:
|
||||||
sys.stdout = file('/dev/null', 'w')
|
sys.stdout = file('/dev/null', 'w')
|
||||||
sys.stderr = file('/dev/null', 'w')
|
sys.stderr = file('/dev/null', 'w')
|
||||||
i += 1
|
|
||||||
pub = publisher.WcsPublisher.create_publisher()
|
pub = publisher.WcsPublisher.create_publisher()
|
||||||
workflows.apply_timeouts()
|
workflows.apply_timeouts()
|
||||||
|
|
||||||
|
CmdApplyTimeouts.register()
|
||||||
|
|
|
@ -20,8 +20,7 @@ import time
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import publisher
|
from qommon.ctl import Command, make_option
|
||||||
import sessions
|
|
||||||
|
|
||||||
def clean_vhost_sessions():
|
def clean_vhost_sessions():
|
||||||
manager = sessions.StorageSessionManager()
|
manager = sessions.StorageSessionManager()
|
||||||
|
@ -36,15 +35,20 @@ def clean_vhost_sessions():
|
||||||
if session._access_time < one_week_ago or session._creation_time < one_month_ago:
|
if session._access_time < one_week_ago or session._creation_time < one_month_ago:
|
||||||
del manager[session.id]
|
del manager[session.id]
|
||||||
|
|
||||||
def clean_sessions(args):
|
|
||||||
pub = publisher.WcsPublisher.create_publisher()
|
|
||||||
|
|
||||||
if '--single_host' in args:
|
class CmdCleanSessions(Command):
|
||||||
clean_vhost_sessions()
|
name = 'clean_sessions'
|
||||||
else:
|
|
||||||
|
def execute(self, options, args):
|
||||||
|
import publisher
|
||||||
|
import sessions
|
||||||
|
|
||||||
|
pub = publisher.WcsPublisher.create_publisher()
|
||||||
|
|
||||||
app_dir = pub.app_dir
|
app_dir = pub.app_dir
|
||||||
hostnames = os.listdir(app_dir)
|
hostnames = os.listdir(app_dir)
|
||||||
for hostname in hostnames:
|
for hostname in hostnames:
|
||||||
pub.app_dir = os.path.join(app_dir, hostname)
|
pub.app_dir = os.path.join(app_dir, hostname)
|
||||||
clean_vhost_sessions()
|
clean_vhost_sessions()
|
||||||
|
|
||||||
|
CmdCleanSessions.register()
|
||||||
|
|
|
@ -19,19 +19,23 @@
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import publisher
|
from qommon.ctl import Command, make_option
|
||||||
|
|
||||||
def export_settings(args):
|
class CmdExportSettings(Command):
|
||||||
stdout = sys.stdout
|
name = 'export_settings'
|
||||||
pub = publisher.WcsPublisher.create_publisher()
|
|
||||||
|
|
||||||
i = 0
|
def __init__(self):
|
||||||
while i < len(args):
|
Command.__init__(self, [
|
||||||
if args[i] == '--vhost':
|
make_option('--vhost', metavar='VHOST', action='store',
|
||||||
pub.app_dir = os.path.join(pub.app_dir, args[i+1])
|
dest='vhost'),
|
||||||
i += 1
|
])
|
||||||
i += 1
|
|
||||||
|
|
||||||
pub.reload_cfg()
|
def execute(self, options, args):
|
||||||
print >> stdout, pub.export_cfg()
|
import publisher
|
||||||
|
stdout = sys.stdout
|
||||||
|
pub = publisher.WcsPublisher.create_publisher()
|
||||||
|
pub.app_dir = os.path.join(pub.app_dir, options.vhost)
|
||||||
|
pub.reload_cfg()
|
||||||
|
print >> stdout, pub.export_cfg()
|
||||||
|
|
||||||
|
CmdExportSettings.register()
|
||||||
|
|
|
@ -24,59 +24,65 @@ import imp
|
||||||
|
|
||||||
from Bouncers import BouncerAPI
|
from Bouncers import BouncerAPI
|
||||||
|
|
||||||
import publisher
|
from qommon.ctl import Command, make_option
|
||||||
|
|
||||||
from qommon.tokens import Token
|
|
||||||
from qommon.bounces import Bounce
|
|
||||||
|
|
||||||
COMMA_SPACE = ', '
|
COMMA_SPACE = ', '
|
||||||
|
|
||||||
def process_bounce(args):
|
class CmdProcessBounce(Command):
|
||||||
try:
|
name = 'process_bounce'
|
||||||
parser = email.Parser.Parser()
|
|
||||||
bouncers_dir = os.path.join(os.path.dirname(__file__), 'Bouncers')
|
def execute(self, options, args):
|
||||||
sys.path.append(bouncers_dir)
|
from qommon.tokens import Token
|
||||||
msg = parser.parse(sys.stdin)
|
from qommon.bounces import Bounce
|
||||||
for modname in BouncerAPI.BOUNCE_PIPELINE:
|
|
||||||
__import__(modname)
|
import publisher
|
||||||
addrs = sys.modules[modname].process(msg)
|
|
||||||
if addrs is BouncerAPI.Stop:
|
|
||||||
return # Stop means to ignore message
|
|
||||||
if addrs:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
return # didn't find any match
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
to = msg['To']
|
parser = email.Parser.Parser()
|
||||||
local_part, server_part = to.split('@')
|
bouncers_dir = os.path.join(os.path.dirname(__file__), 'Bouncers')
|
||||||
token_id = local_part.split('+')[1]
|
sys.path.append(bouncers_dir)
|
||||||
except (IndexError, KeyError):
|
msg = parser.parse(sys.stdin)
|
||||||
return
|
for modname in BouncerAPI.BOUNCE_PIPELINE:
|
||||||
|
__import__(modname)
|
||||||
|
addrs = sys.modules[modname].process(msg)
|
||||||
|
if addrs is BouncerAPI.Stop:
|
||||||
|
return # Stop means to ignore message
|
||||||
|
if addrs:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
return # didn't find any match
|
||||||
|
|
||||||
pub = publisher.WcsPublisher.create_publisher()
|
try:
|
||||||
pub.app_dir = os.path.join(pub.app_dir, server_part)
|
to = msg['To']
|
||||||
|
local_part, server_part = to.split('@')
|
||||||
|
token_id = local_part.split('+')[1]
|
||||||
|
except (IndexError, KeyError):
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
pub = publisher.WcsPublisher.create_publisher()
|
||||||
token = Token.get(token_id)
|
pub.app_dir = os.path.join(pub.app_dir, server_part)
|
||||||
except KeyError:
|
|
||||||
return
|
|
||||||
|
|
||||||
if token.type != 'email-bounce':
|
try:
|
||||||
return
|
token = Token.get(token_id)
|
||||||
|
except KeyError:
|
||||||
|
return
|
||||||
|
|
||||||
token.remove_self()
|
if token.type != 'email-bounce':
|
||||||
|
return
|
||||||
|
|
||||||
bounce = Bounce()
|
token.remove_self()
|
||||||
bounce.arrival_time = time.time()
|
|
||||||
bounce.bounce_message = msg.as_string()
|
|
||||||
bounce.addrs = addrs
|
|
||||||
bounce.original_message = token.email_message
|
|
||||||
bounce.original_rcpts = token.email_rcpts
|
|
||||||
bounce.email_type = token.email_type
|
|
||||||
bounce.store()
|
|
||||||
except Exception, e:
|
|
||||||
import traceback
|
|
||||||
file('/tmp/bounces-error', 'a+').write(traceback.format_exc())
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
|
bounce = Bounce()
|
||||||
|
bounce.arrival_time = time.time()
|
||||||
|
bounce.bounce_message = msg.as_string()
|
||||||
|
bounce.addrs = addrs
|
||||||
|
bounce.original_message = token.email_message
|
||||||
|
bounce.original_rcpts = token.email_rcpts
|
||||||
|
bounce.email_type = token.email_type
|
||||||
|
bounce.store()
|
||||||
|
except Exception, e:
|
||||||
|
import traceback
|
||||||
|
file('/tmp/bounces-error', 'a+').write(traceback.format_exc())
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
CmdProcessBounce.register()
|
||||||
|
|
|
@ -20,22 +20,25 @@ import time
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import publisher
|
from qommon.ctl import Command, make_option
|
||||||
|
|
||||||
from wcs.formdef import FormDef
|
|
||||||
|
|
||||||
def rebuild_vhost_indexes():
|
def rebuild_vhost_indexes():
|
||||||
FormDef.rebuild_indexes()
|
FormDef.rebuild_indexes()
|
||||||
|
|
||||||
def rebuild_indexes(args):
|
|
||||||
pub = publisher.WcsPublisher.create_publisher()
|
|
||||||
|
|
||||||
if '--single_host' in args:
|
class CmdRebuildIndexes(Command):
|
||||||
rebuild_vhost_indexes()
|
name = 'rebuild_indexes'
|
||||||
else:
|
|
||||||
|
def execute(self, options, args):
|
||||||
|
import publisher
|
||||||
|
from wcs.formdef import FormDef
|
||||||
|
|
||||||
|
pub = publisher.WcsPublisher.create_publisher()
|
||||||
|
|
||||||
app_dir = pub.app_dir
|
app_dir = pub.app_dir
|
||||||
hostnames = os.listdir(app_dir)
|
hostnames = os.listdir(app_dir)
|
||||||
for hostname in hostnames:
|
for hostname in hostnames:
|
||||||
pub.app_dir = os.path.join(app_dir, hostname)
|
pub.app_dir = os.path.join(app_dir, hostname)
|
||||||
rebuild_vhost_indexes()
|
rebuild_vhost_indexes()
|
||||||
|
|
||||||
|
CmdRebuildIndexes.register()
|
||||||
|
|
|
@ -21,48 +21,64 @@ import sys
|
||||||
import qommon.scgi_server
|
import qommon.scgi_server
|
||||||
import quixote.server.simple_server
|
import quixote.server.simple_server
|
||||||
|
|
||||||
import publisher
|
from qommon.ctl import Command, make_option
|
||||||
|
|
||||||
def start(args):
|
class CmdStart(Command):
|
||||||
run_kwargs = {}
|
name = 'start'
|
||||||
run_kwargs['port'] = 3001
|
|
||||||
run_kwargs['spawn_cron'] = True
|
|
||||||
run_function = qommon.scgi_server.run
|
|
||||||
|
|
||||||
i = 0
|
def __init__(self):
|
||||||
while i < len(args):
|
Command.__init__(self, [
|
||||||
if args[i] == '--port':
|
make_option('--port', metavar='PORT', action='store',
|
||||||
run_kwargs['port'] = int(args[i+1])
|
dest='port', default=3001),
|
||||||
i += 1
|
make_option('--extra', metavar='DIR', action='append',
|
||||||
elif args[i] == '--extra':
|
dest='extra', default=[]),
|
||||||
publisher.WcsPublisher.register_extra_dir(args[i+1])
|
make_option('--handler-connection-limit', metavar='LIMIT',
|
||||||
i += 1
|
action='store',
|
||||||
elif args[i] == '--handler-connection-limit':
|
dest='handler_connection_limit', default=None),
|
||||||
run_kwargs['handler_connection_limit'] = int(args[i+1])
|
make_option('--script-name', metavar='NAME', action='store',
|
||||||
i += 1
|
dest='script_name', default=None),
|
||||||
elif args[i] == '--script-name':
|
make_option('--app-dir', metavar='DIR', action='store',
|
||||||
run_kwargs['script_name'] = args[i+1]
|
dest='app_dir', default=None),
|
||||||
i += 1
|
make_option('--data-dir', metavar='DIR', action='store',
|
||||||
elif args[i] == '--app-dir':
|
dest='data_dir', default=None),
|
||||||
publisher.WcsPublisher.APP_DIR = args[i+1]
|
make_option('--http', action='store_true',
|
||||||
i += 1
|
dest='http', default=False),
|
||||||
elif args[i] == '--data-dir':
|
make_option('--silent', action='store_true',
|
||||||
publisher.WcsPublisher.DATA_DIR = args[i+1]
|
dest='silent', default=False),
|
||||||
i += 1
|
])
|
||||||
elif args[i] == '--http':
|
|
||||||
|
def execute(self, options, args):
|
||||||
|
import publisher
|
||||||
|
|
||||||
|
run_kwargs = {}
|
||||||
|
run_kwargs['port'] = int(options.port)
|
||||||
|
run_kwargs['spawn_cron'] = True
|
||||||
|
run_function = qommon.scgi_server.run
|
||||||
|
for directory in options.extra:
|
||||||
|
publisher.WcsPublisher.register_extra_dir(directory)
|
||||||
|
if options.handler_connection_limit:
|
||||||
|
run_kwargs['handler_connection_limit'] = int(options.handler_connection_limit)
|
||||||
|
if options.script_name:
|
||||||
|
run_kwargs['script_name'] = options.script_name
|
||||||
|
if options.app_dir:
|
||||||
|
publisher.WcsPublisher.APP_DIR = options.app_dir
|
||||||
|
if options.data_dir:
|
||||||
|
publisher.WcsPublisher.DATA_DIR = options.data_dir
|
||||||
|
if options.http:
|
||||||
run_function = qommon.scgi_server.http_run
|
run_function = qommon.scgi_server.http_run
|
||||||
elif args[i] == '--silent':
|
if options.silent:
|
||||||
sys.stdout = file('/dev/null', 'w')
|
sys.stdout = file('/dev/null', 'w')
|
||||||
sys.stderr = file('/dev/null', 'w')
|
sys.stderr = file('/dev/null', 'w')
|
||||||
i += 1
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
run_function(publisher.WcsPublisher.create_publisher, **run_kwargs)
|
run_function(publisher.WcsPublisher.create_publisher, **run_kwargs)
|
||||||
except socket.error, e:
|
except socket.error, e:
|
||||||
if e[0] == 98:
|
if e[0] == 98:
|
||||||
print >> sys.stderr, 'address already in use'
|
print >> sys.stderr, 'address already in use'
|
||||||
|
sys.exit(1)
|
||||||
|
raise
|
||||||
|
except KeyboardInterrupt:
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
raise
|
|
||||||
except KeyboardInterrupt:
|
CmdStart.register()
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,116 @@
|
||||||
|
# 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
|
||||||
|
# along with this program; if not, write to the Free Software
|
||||||
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||||
|
# 02110-1301 USA
|
||||||
|
|
||||||
|
import optparse
|
||||||
|
from optparse import make_option
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
'Command',
|
||||||
|
'register_command',
|
||||||
|
]
|
||||||
|
|
||||||
|
import qommon
|
||||||
|
qommon._commands = {}
|
||||||
|
|
||||||
|
class Command:
|
||||||
|
doc = ''
|
||||||
|
name = None
|
||||||
|
usage_args = '[ options ... ]'
|
||||||
|
|
||||||
|
def __init__(self, options=[]):
|
||||||
|
self.options = options
|
||||||
|
|
||||||
|
def run(self, args):
|
||||||
|
options, args = self.parse_args(args)
|
||||||
|
return self.execute(options, args)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
def execute(self, options, args):
|
||||||
|
"""The body of the command"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def register(cls):
|
||||||
|
qommon._commands[cls.name] = cls
|
||||||
|
register = classmethod(register)
|
||||||
|
|
||||||
|
class Ctl:
|
||||||
|
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"))
|
||||||
|
|
||||||
|
def print_help(self, *args):
|
||||||
|
self.parser.print_help()
|
||||||
|
print
|
||||||
|
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))
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
commands = [(x.name, x.doc) for x in qommon._commands.values()]
|
||||||
|
commands.sort()
|
||||||
|
print 'Available commands:'
|
||||||
|
for name, description in commands:
|
||||||
|
print ' %-15s %s' % (name, description)
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
def run(self, args):
|
||||||
|
options, args = self.parser.parse_args(args)
|
||||||
|
command, args = args[0], args[1:]
|
||||||
|
if command not in qommon._commands:
|
||||||
|
for cmd_prefix in self.cmd_prefixes:
|
||||||
|
try:
|
||||||
|
__import__('%s.%s' % (cmd_prefix, command))
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
command_class = qommon._commands[command]
|
||||||
|
cmd = command_class()
|
||||||
|
return cmd.run(args)
|
||||||
|
|
39
wcsctl.py
39
wcsctl.py
|
@ -2,41 +2,8 @@
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
def print_usage():
|
import wcs.qommon.ctl
|
||||||
print 'Usage: wcsctl.py command [...]'
|
|
||||||
print ''
|
|
||||||
print 'Commands:'
|
|
||||||
print ' start start server'
|
|
||||||
print ' clean_sessions clean old sessions'
|
|
||||||
print ' rebuild_indexes rebuild database indexes'
|
|
||||||
print ' export_settings export settings'
|
|
||||||
print ' apply_timeouts expire forms with timeout state'
|
|
||||||
|
|
||||||
|
ctl = wcs.qommon.ctl.Ctl(cmd_prefixes=['wcs.ctl'])
|
||||||
if len(sys.argv) < 2:
|
ctl.run(sys.argv[1:])
|
||||||
print_usage()
|
|
||||||
sys.exit(1)
|
|
||||||
else:
|
|
||||||
command = sys.argv[1]
|
|
||||||
|
|
||||||
if command == 'start':
|
|
||||||
from wcs.ctl.start import start
|
|
||||||
start(sys.argv[2:])
|
|
||||||
elif command == 'clean_sessions':
|
|
||||||
from wcs.ctl.clean_sessions import clean_sessions
|
|
||||||
clean_sessions(sys.argv[2:])
|
|
||||||
elif command == 'rebuild_indexes':
|
|
||||||
from wcs.ctl.rebuild_indexes import rebuild_indexes
|
|
||||||
rebuild_indexes(sys.argv[2:])
|
|
||||||
elif command == 'process_bounce':
|
|
||||||
from wcs.ctl.process_bounce import process_bounce
|
|
||||||
process_bounce(sys.argv[2:])
|
|
||||||
elif command == 'export_settings':
|
|
||||||
from wcs.ctl.export_settings import export_settings
|
|
||||||
export_settings(sys.argv[2:])
|
|
||||||
elif command == 'apply_timeouts':
|
|
||||||
from wcs.ctl.apply_timeouts import apply_timeouts
|
|
||||||
apply_timeouts(sys.argv[2:])
|
|
||||||
else:
|
|
||||||
print_usage()
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue