misc: add tenant support to shell management command, deprecate old ctl (#42099)

This commit is contained in:
Frédéric Péters 2020-04-25 15:31:54 +02:00
parent ef6699be4a
commit 583381bc9e
3 changed files with 40 additions and 84 deletions

View File

@ -399,3 +399,8 @@ def test_import_site():
site_zip_path = os.path.join(os.path.dirname(__file__), 'missing_file.zip')
with pytest.raises(CommandError, match='missing file:'):
call_command('import_site', '--domain=example.net', site_zip_path)
def test_shell():
with pytest.raises(CommandError):
call_command('shell') # missing tenant name

View File

@ -0,0 +1,31 @@
# w.c.s. - web application for online forms
# Copyright (C) 2005-2020 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/>.
from django.core.management.commands import shell
from . import TenantCommand
class Command(shell.Command, TenantCommand):
help = '''Run a shell in tenant context'''
def add_arguments(self, parser):
parser.add_argument('-d', '--domain', '--vhost', metavar='DOMAIN', required=True)
super(Command, self).add_arguments(parser)
def handle(self, *args, **options):
domain = options.pop('domain')
self.init_tenant_publisher(domain, register_tld_names=False)
return shell.Command().handle(*args, **options)

View File

@ -19,96 +19,16 @@
#
# [1]: file django/core/management/commands/shell.py
from __future__ import print_function
import sys
import os.path
from ..qommon.ctl import Command, make_option
from ..qommon.ctl import Command
class CmdShell(Command):
'''Launch a shell and initialize a publisher on a given host'''
'''(obsolete) Launch a shell and initialize a publisher on a given host'''
name = 'shell'
def __init__(self):
Command.__init__(self, [
make_option('--plain', action='store_true',
dest='plain', default=False) ])
def execute(self, base_options, sub_options, args):
from .. import publisher
self.config.remove_option('main', 'error_log')
publisher.WcsPublisher.configure(self.config)
publisher = publisher.WcsPublisher.create_publisher(
register_tld_names=False)
publisher.app_dir = os.path.join(publisher.APP_DIR, args[0])
if not os.path.exists(publisher.app_dir):
print('Application directory %r does not exist.' % publisher.app_dir)
raise SystemExit(1)
publisher.set_config()
try:
if sub_options.plain:
# Don't bother loading IPython, because the user wants plain Python.
raise ImportError
self.run_shell()
except ImportError:
import code
# Set up a dictionary to serve as the environment for the shell, so
# that tab completion works on objects that are imported at runtime.
# See ticket 5082.
imported_objects = {}
try: # Try activating rlcompleter, because it's handy.
import readline
except ImportError:
pass
else:
# We don't have to wrap the following import in a 'try', because
# we already know 'readline' was imported successfully.
import rlcompleter
readline.set_completer(rlcompleter.Completer(imported_objects).complete)
readline.parse_and_bind("tab:complete")
# We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system
# conventions and get $PYTHONSTARTUP first then .pythonrc.py.
if not sub_options.plain:
for pythonrc in (os.environ.get("PYTHONSTARTUP"),
os.path.expanduser('~/.pythonrc.py')):
if pythonrc and os.path.isfile(pythonrc):
try:
with open(pythonrc) as handle:
exec(compile(handle.read(), pythonrc, 'exec'))
except NameError:
pass
code.interact(local=imported_objects)
shells = [ 'ipython', 'bpython' ]
def ipython(self):
try:
from IPython import embed
embed()
except ImportError:
# IPython < 0.11
# Explicitly pass an empty list as arguments, because otherwise
# IPython would use sys.argv from this script.
try:
from IPython.Shell import IPShell
shell = IPShell(argv=[])
shell.mainloop()
except ImportError:
# IPython not found at all, raise ImportError
raise
def bpython(self):
import bpython
bpython.embed()
def run_shell(self):
for shell in self.shells:
try:
return getattr(self, shell)()
except ImportError:
pass
raise ImportError
print('Error: use wcs-manage shell command', file=sys.stderr)
CmdShell.register()