Projet

Général

Profil

0001-ctl-make-runscript-a-management-command-34405.patch

Frédéric Péters, 30 juin 2019 14:08

Télécharger (6,15 ko)

Voir les différences:

Subject: [PATCH 1/2] ctl: make runscript a management command (#34405)

 wcs/ctl/management/commands/__init__.py      | 31 ++++++++++++++
 wcs/ctl/management/commands/runscript.py     | 44 ++++++++++++++++++++
 wcs/ctl/management/commands/trigger_jumps.py | 16 ++-----
 wcs/ctl/runscript.py                         |  2 +
 4 files changed, 81 insertions(+), 12 deletions(-)
 create mode 100644 wcs/ctl/management/commands/runscript.py
wcs/ctl/management/commands/__init__.py
1
# w.c.s. - web application for online forms
2
# Copyright (C) 2005-2019  Entr'ouvert
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, see <http://www.gnu.org/licenses/>.
16

  
17
import os
18

  
19
from django.core.management.base import BaseCommand, CommandError
20

  
21
from qommon.publisher import get_publisher_class
22

  
23

  
24
class TenantCommand(BaseCommand):
25
    def init_tenant_publisher(self, domain, **kwargs):
26
        publisher = get_publisher_class().create_publisher(**kwargs)
27
        if not domain in publisher.get_tenants():
28
            raise CommandError('unknown tenant')
29
        publisher.app_dir = os.path.join(publisher.APP_DIR, domain)
30
        publisher.set_config()
31
        return publisher
wcs/ctl/management/commands/runscript.py
1
# w.c.s. - web application for online forms
2
# Copyright (C) 2005-2019  Entr'ouvert
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, see <http://www.gnu.org/licenses/>.
16

  
17
import argparse
18
import os.path
19
import runpy
20
import sys
21

  
22
from qommon.publisher import get_publisher_class
23

  
24
from . import TenantCommand
25

  
26

  
27
class Command(TenantCommand):
28
    '''Run a script within a given host publisher context'''
29

  
30
    def add_arguments(self, parser):
31
        parser.add_argument('-d', '--domain', '--vhost', metavar='DOMAIN', required=True)
32
        parser.add_argument('--app-dir', metavar='DIR', action='store', dest='app_dir', default=None)
33
        parser.add_argument('args', nargs=argparse.REMAINDER)
34

  
35
    def handle(self, *args, **options):
36
        if options.get('app_dir'):
37
            get_publisher_class().APP_DIR = options.get('app_dir')
38
        domain = options.get('domain')
39
        self.init_tenant_publisher(domain, register_tld_names=False)
40
        fullpath = os.path.dirname(os.path.abspath(args[0]))
41
        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)
wcs/ctl/management/commands/trigger_jumps.py
17 17
import json
18 18
import os
19 19

  
20
from django.core.management.base import BaseCommand, CommandError
20
from django.core.management.base import CommandError
21 21

  
22 22
from quixote import get_publisher
23
from qommon.publisher import get_publisher_class
24 23

  
25 24
from wcs.formdef import FormDef
26 25
from wcs.workflows import Workflow
27 26
from wcs.wf.jump import JumpWorkflowStatusItem, jump_and_perform as wcs_jump_and_perform
28 27

  
28
from . import TenantCommand
29 29

  
30
class Command(BaseCommand):
30

  
31
class Command(TenantCommand):
31 32
    '''Triggers all "jump trigger" for a formdef, given host publisher context
32 33

  
33 34
    source.json file format:
......
48 49
        parser.add_argument('--all-formdata', action='store_true')
49 50
        parser.add_argument('filenames', metavar='FILENAME', nargs='+')
50 51

  
51
    def init_tenant_publisher(self, domain):
52
        publisher_class = get_publisher_class()
53
        publisher = publisher_class.create_publisher()
54
        if not domain in publisher.get_tenants():
55
            raise CommandError('unknown tenant')
56
        publisher.app_dir = os.path.join(publisher.app_dir, domain)
57
        publisher.set_config()
58
        return publisher
59

  
60 52
    def handle(self, filenames, domain, trigger, workflow_id, formdef_id,
61 53
            all_formdata, **options):
62 54

  
wcs/ctl/runscript.py
16 16

  
17 17
import os.path
18 18
import runpy
19
import warnings
19 20
import sys
20 21

  
21 22
from qommon.ctl import Command, make_option
......
32 33
                ])
33 34

  
34 35
    def execute(self, base_options, sub_options, args):
36
        warnings.warn('Deprecated command, use management command', DeprecationWarning)
35 37
        import publisher
36 38
        self.config.remove_option('main', 'error_log')
37 39
        publisher.WcsPublisher.configure(self.config)
38
-