ctl: add a runscript command, to run a given script within a publisher context

This commit is contained in:
Frédéric Péters 2013-01-29 17:42:07 +01:00 committed by Thomas NOEL
parent f0b7060d87
commit 56dfd14339
1 changed files with 46 additions and 0 deletions

46
wcs/ctl/runscript.py Normal file
View File

@ -0,0 +1,46 @@
# w.c.s. - web application for online forms
# Copyright (C) 2005-2013 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, see <http://www.gnu.org/licenses/>.
import os.path
import runpy
import sys
from qommon.ctl import Command, make_option
class CmdRunScript(Command):
'''Run a script within a given host publisher context'''
name = 'runscript'
def __init__(self):
Command.__init__(self, [
make_option('--vhost', metavar='VHOST', action='store',
dest='vhost'),
])
def execute(self, base_options, sub_options, args):
import publisher
self.config.remove_option('main', 'error_log')
publisher.WcsPublisher.configure(self.config, sub_options.extra)
publisher = publisher.WcsPublisher.create_publisher()
publisher.app_dir = os.path.join(publisher.app_dir, sub_options.vhost)
publisher.set_config()
fullpath = os.path.dirname(os.path.abspath(args[0]))
sys.path.append(fullpath)
module_name = os.path.splitext(os.path.basename(args[0]))[0]
runpy.run_module(module_name)
CmdRunScript.register()