From d7d12d20738e230286cf8f35249d3273c00d2563 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Sun, 30 Jun 2019 13:18:58 +0200
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

diff --git a/wcs/ctl/management/commands/__init__.py b/wcs/ctl/management/commands/__init__.py
index e69de29bb..e6d3d54a4 100644
--- a/wcs/ctl/management/commands/__init__.py
+++ b/wcs/ctl/management/commands/__init__.py
@@ -0,0 +1,31 @@
+# w.c.s. - web application for online forms
+# Copyright (C) 2005-2019  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
+
+from django.core.management.base import BaseCommand, CommandError
+
+from qommon.publisher import get_publisher_class
+
+
+class TenantCommand(BaseCommand):
+    def init_tenant_publisher(self, domain, **kwargs):
+        publisher = get_publisher_class().create_publisher(**kwargs)
+        if not domain in publisher.get_tenants():
+            raise CommandError('unknown tenant')
+        publisher.app_dir = os.path.join(publisher.APP_DIR, domain)
+        publisher.set_config()
+        return publisher
diff --git a/wcs/ctl/management/commands/runscript.py b/wcs/ctl/management/commands/runscript.py
new file mode 100644
index 000000000..51fc069d7
--- /dev/null
+++ b/wcs/ctl/management/commands/runscript.py
@@ -0,0 +1,44 @@
+# w.c.s. - web application for online forms
+# Copyright (C) 2005-2019  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 argparse
+import os.path
+import runpy
+import sys
+
+from qommon.publisher import get_publisher_class
+
+from . import TenantCommand
+
+
+class Command(TenantCommand):
+    '''Run a script within a given host publisher context'''
+
+    def add_arguments(self, parser):
+        parser.add_argument('-d', '--domain', '--vhost', metavar='DOMAIN', required=True)
+        parser.add_argument('--app-dir', metavar='DIR', action='store', dest='app_dir', default=None)
+        parser.add_argument('args', nargs=argparse.REMAINDER)
+
+    def handle(self, *args, **options):
+        if options.get('app_dir'):
+            get_publisher_class().APP_DIR = options.get('app_dir')
+        domain = options.get('domain')
+        self.init_tenant_publisher(domain, register_tld_names=False)
+        fullpath = os.path.dirname(os.path.abspath(args[0]))
+        sys.path.insert(0, fullpath)
+        module_name = os.path.splitext(os.path.basename(args[0]))[0]
+        sys.argv = args
+        runpy.run_module(module_name)
diff --git a/wcs/ctl/management/commands/trigger_jumps.py b/wcs/ctl/management/commands/trigger_jumps.py
index 4b5cc5e61..f77bb6775 100644
--- a/wcs/ctl/management/commands/trigger_jumps.py
+++ b/wcs/ctl/management/commands/trigger_jumps.py
@@ -17,17 +17,18 @@
 import json
 import os
 
-from django.core.management.base import BaseCommand, CommandError
+from django.core.management.base import CommandError
 
 from quixote import get_publisher
-from qommon.publisher import get_publisher_class
 
 from wcs.formdef import FormDef
 from wcs.workflows import Workflow
 from wcs.wf.jump import JumpWorkflowStatusItem, jump_and_perform as wcs_jump_and_perform
 
+from . import TenantCommand
 
-class Command(BaseCommand):
+
+class Command(TenantCommand):
     '''Triggers all "jump trigger" for a formdef, given host publisher context
 
     source.json file format:
@@ -48,15 +49,6 @@ class Command(BaseCommand):
         parser.add_argument('--all-formdata', action='store_true')
         parser.add_argument('filenames', metavar='FILENAME', nargs='+')
 
-    def init_tenant_publisher(self, domain):
-        publisher_class = get_publisher_class()
-        publisher = publisher_class.create_publisher()
-        if not domain in publisher.get_tenants():
-            raise CommandError('unknown tenant')
-        publisher.app_dir = os.path.join(publisher.app_dir, domain)
-        publisher.set_config()
-        return publisher
-
     def handle(self, filenames, domain, trigger, workflow_id, formdef_id,
             all_formdata, **options):
 
diff --git a/wcs/ctl/runscript.py b/wcs/ctl/runscript.py
index 44e427d44..b01a271a0 100644
--- a/wcs/ctl/runscript.py
+++ b/wcs/ctl/runscript.py
@@ -16,6 +16,7 @@
 
 import os.path
 import runpy
+import warnings
 import sys
 
 from qommon.ctl import Command, make_option
@@ -32,6 +33,7 @@ class CmdRunScript(Command):
                 ])
 
     def execute(self, base_options, sub_options, args):
+        warnings.warn('Deprecated command, use management command', DeprecationWarning)
         import publisher
         self.config.remove_option('main', 'error_log')
         publisher.WcsPublisher.configure(self.config)
-- 
2.20.1

