0002-ctl-add-all-tenants-parameter-to-runscript-34405.patch
tests/test_ctl.py | ||
---|---|---|
5 | 5 |
from email.mime.multipart import MIMEMultipart |
6 | 6 |
import psycopg2 |
7 | 7 | |
8 |
from django.core.management import call_command, CommandError |
|
9 | ||
8 | 10 |
from wcs.formdef import FormDef |
9 | 11 |
from wcs.workflows import Workflow |
10 | 12 |
from wcs.wf.jump import JumpWorkflowStatusItem |
... | ... | |
17 | 19 |
from wcs.ctl.process_bounce import CmdProcessBounce |
18 | 20 |
from wcs.ctl.rebuild_indexes import rebuild_vhost_indexes |
19 | 21 |
from wcs.ctl.wipe_data import CmdWipeData |
22 |
from wcs.ctl.management.commands.runscript import Command as CmdRunScript |
|
20 | 23 |
from wcs.ctl.management.commands.trigger_jumps import select_and_jump_formdata |
21 | 24 |
from wcs.ctl.delete_tenant import CmdDeleteTenant |
22 | 25 |
from wcs.sql import get_connection_and_cursor, cleanup_connection |
... | ... | |
361 | 364 | |
362 | 365 |
rebuild_vhost_indexes(two_pubs, destroy=True) |
363 | 366 |
assert os.listdir(os.path.join(two_pubs.app_dir, 'formdefs-url_name')) == ['example'] |
367 | ||
368 |
def test_runscript(pub): |
|
369 |
with pytest.raises(CommandError): |
|
370 |
call_command('runscript') |
|
371 |
with pytest.raises(CommandError): |
|
372 |
call_command('runscript', '--domain=a', '--all-tenants') |
|
373 |
with open(os.path.join(pub.app_dir, 'test2.py'), 'w') as fd: |
|
374 |
fd.write(''' |
|
375 |
import os |
|
376 |
from quixote import get_publisher |
|
377 |
open(os.path.join(get_publisher().app_dir, 'runscript.test'), 'w').close() |
|
378 |
''') |
|
379 |
call_command('runscript', '--domain=example.net', os.path.join(pub.app_dir, 'test2.py')) |
|
380 |
assert os.path.exists(os.path.join(pub.app_dir, 'runscript.test')) |
|
381 |
os.unlink(os.path.join(pub.app_dir, 'runscript.test')) |
|
382 |
call_command('runscript', '--all-tenants', os.path.join(pub.app_dir, 'test2.py')) |
|
383 |
assert os.path.exists(os.path.join(pub.app_dir, 'runscript.test')) |
wcs/ctl/management/commands/runscript.py | ||
---|---|---|
19 | 19 |
import runpy |
20 | 20 |
import sys |
21 | 21 | |
22 |
from django.core.management.base import CommandError |
|
23 | ||
22 | 24 |
from qommon.publisher import get_publisher_class |
23 | 25 | |
24 | 26 |
from . import TenantCommand |
... | ... | |
28 | 30 |
'''Run a script within a given host publisher context''' |
29 | 31 | |
30 | 32 |
def add_arguments(self, parser): |
31 |
parser.add_argument('-d', '--domain', '--vhost', metavar='DOMAIN', required=True)
|
|
33 |
parser.add_argument('-d', '--domain', '--vhost', metavar='DOMAIN') |
|
32 | 34 |
parser.add_argument('--app-dir', metavar='DIR', action='store', dest='app_dir', default=None) |
35 |
parser.add_argument('--all-tenants', action='store_true') |
|
33 | 36 |
parser.add_argument('args', nargs=argparse.REMAINDER) |
34 | 37 | |
35 | 38 |
def handle(self, *args, **options): |
36 | 39 |
if options.get('app_dir'): |
37 | 40 |
get_publisher_class().APP_DIR = options.get('app_dir') |
38 | 41 |
domain = options.get('domain') |
39 |
self.init_tenant_publisher(domain, register_tld_names=False) |
|
42 |
all_tenants = options.get('all_tenants') |
|
43 |
if domain and all_tenants: |
|
44 |
raise CommandError('--domain and --all-tenants are exclusive') |
|
45 |
if not (domain or all_tenants): |
|
46 |
raise CommandError('--domain or --all-tenants are required') |
|
47 |
if domain: |
|
48 |
domains = [domain] |
|
49 |
else: |
|
50 |
domains = get_publisher_class().get_tenants() |
|
40 | 51 |
fullpath = os.path.dirname(os.path.abspath(args[0])) |
41 | 52 |
sys.path.insert(0, fullpath) |
42 |
module_name = os.path.splitext(os.path.basename(args[0]))[0] |
|
43 |
sys.argv = args |
|
44 |
runpy.run_module(module_name) |
|
53 |
module_name = os.path.splitext(os.path.basename(args[0]))[0].encode('utf-8') |
|
54 |
for domain in domains: |
|
55 |
sys.argv = args[:] |
|
56 |
self.init_tenant_publisher(domain, register_tld_names=False) |
|
57 |
runpy.run_module(module_name) |
|
45 |
- |