[cubicweb-ctl] backport --dbglevel option of pyramid to all instance commands
authorLaurent Peuch <cortex@worlddomination.be>
Tue, 21 May 2019 18:16:51 +0200
changeset 12737 56f7386bc6b9
parent 12736 5add82b08a6d
child 12738 a54037a68b14
[cubicweb-ctl] backport --dbglevel option of pyramid to all instance commands Closes #17219653
cubicweb/cwctl.py
cubicweb/pyramid/pyramidctl.py
cubicweb/test/unittest_cwctl.py
doc/changes/3.27.rst
--- a/cubicweb/cwctl.py	Tue May 21 18:08:17 2019 +0200
+++ b/cubicweb/cwctl.py	Tue May 21 18:16:51 2019 +0200
@@ -42,10 +42,12 @@
 
 from cubicweb import ConfigurationError, ExecutionError, BadCommandUsage
 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg, CONFIGURATIONS
+from cubicweb.server import set_debug
 from cubicweb.toolsutils import Command, rm, create_dir, underline_title
 from cubicweb.__pkginfo__ import version as cw_version
 
 LOG_LEVELS = ('debug', 'info', 'warning', 'error')
+DBG_FLAGS = ('RQL', 'SQL', 'REPO', 'HOOKS', 'OPS', 'SEC', 'MORE')
 
 # don't check duplicated commands, it occurs when reloading site_cubicweb
 CWCTL = CommandLine('cubicweb-ctl', 'The CubicWeb swiss-knife.',
@@ -136,6 +138,13 @@
                   % (', '.join(LOG_LEVELS)),
           }
          ),
+        ('dbglevel',
+         {'type': 'multiple_choice', 'metavar': '<debug level>',
+          'default': None,
+          'choices': DBG_FLAGS,
+          'help': ('Set the server debugging flags; you may choose several '
+                   'values in %s; imply "debug" loglevel if loglevel is not set' % (DBG_FLAGS,)),
+          }),
     )
     actionverb = None
 
@@ -157,10 +166,17 @@
         # certain situations if it's not explicitly set by the user and we want
         # to detect that (the "None" case)
         if self['loglevel'] is None:
-            init_cmdline_log_threshold(self.cwconfig, 'error')
+            # if no loglevel is set but dbglevel is here we want to set level to debug
+            if self['dbglevel']:
+                init_cmdline_log_threshold(self.cwconfig, 'debug')
+            else:
+                init_cmdline_log_threshold(self.cwconfig, 'error')
         else:
             init_cmdline_log_threshold(self.cwconfig, self['loglevel'])
 
+        if self['dbglevel']:
+            set_debug('|'.join('DBG_' + x.upper() for x in self['dbglevel']))
+
         try:
             status = cmdmeth(appid) or 0
         except (ExecutionError, ConfigurationError) as ex:
--- a/cubicweb/pyramid/pyramidctl.py	Tue May 21 18:08:17 2019 +0200
+++ b/cubicweb/pyramid/pyramidctl.py	Tue May 21 18:16:51 2019 +0200
@@ -37,7 +37,7 @@
 from cubicweb.cwctl import CWCTL, InstanceCommand, init_cmdline_log_threshold
 from cubicweb.pyramid import wsgi_application_from_cwconfig
 from cubicweb.pyramid.config import get_random_secret_key
-from cubicweb.server import serverctl, set_debug
+from cubicweb.server import serverctl
 from cubicweb.web.webctl import WebCreateHandler
 from cubicweb.toolsutils import fill_templated_file
 
@@ -45,8 +45,6 @@
 
 MAXFD = 1024
 
-DBG_FLAGS = ('RQL', 'SQL', 'REPO', 'HOOKS', 'OPS', 'SEC', 'MORE')
-
 
 def _generate_pyramid_ini_file(pyramid_ini_path):
     """Write a 'pyramid.ini' file into apphome."""
@@ -107,13 +105,6 @@
         ('reload-interval',
          {'type': 'int', 'default': 1,
           'help': 'Interval, in seconds, between file modifications checks'}),
-        ('dbglevel',
-         {'type': 'multiple_choice', 'metavar': '<dbg level>',
-          'default': None,
-          'choices': DBG_FLAGS,
-          'help': ('Set the server debugging flags; you may choose several '
-                   'values in %s; imply "debug" loglevel' % (DBG_FLAGS,)),
-          }),
         ('profile',
          {'action': 'store_true',
           'default': False,
@@ -260,12 +251,9 @@
                 self['reload-interval'], extra_files,
                 filelist_path=filelist_path)
 
-        if self['dbglevel']:
-            set_debug('|'.join('DBG_' + x.upper() for x in self['dbglevel']))
-
-        # if no loglevel is specified and --debug or --dbglevel are here, set log level at debug
-        if self['loglevel'] is None and (self['debug'] or self['dbglevel']):
-            init_cmdline_log_threshold(cwconfig, 'debug')
+        # if no loglevel is specified and --debug is here, set log level at debug
+        if self['loglevel'] is None and self['debug']:
+            init_cmdline_log_threshold(self.cwconfig, 'debug')
 
         app = wsgi_application_from_cwconfig(
             cwconfig, profile=self['profile'],
--- a/cubicweb/test/unittest_cwctl.py	Tue May 21 18:08:17 2019 +0200
+++ b/cubicweb/test/unittest_cwctl.py	Tue May 21 18:16:51 2019 +0200
@@ -24,7 +24,7 @@
 
 from logilab.common.clcommands import CommandLine
 
-from cubicweb import cwctl
+from cubicweb import cwctl, server
 from cubicweb.cwctl import ListCommand, InstanceCommand
 from cubicweb.devtools.testlib import CubicWebTC
 from cubicweb.server.migractions import ServerMigrationHelper
@@ -112,9 +112,9 @@
         self.fake_config.global_set_option = MagicMock()
 
         # pretend that this instance exists
-        patcher = patch.object(cwcfg, 'config_for', return_value=self.fake_config)
-        patcher.start()
-        self.addCleanup(patcher.stop)
+        config_patcher = patch.object(cwcfg, 'config_for', return_value=self.fake_config)
+        config_patcher.start()
+        self.addCleanup(config_patcher.stop)
 
     @patch.object(_TestCommand, 'test_instance', return_value=0)
     def test_getting_called(self, test_instance):
@@ -181,6 +181,20 @@
             self.fake_config.global_set_option.assert_called_with('log-threshold',
                                                                   log_level.upper())
 
+    @patch.object(server, "DEBUG", 0)
+    def test_set_dblevel(self):
+        DBG_FLAGS = ('RQL', 'SQL', 'REPO', 'HOOKS', 'OPS', 'SEC', 'MORE')
+
+        total_value = 0
+
+        for dbg_flag in DBG_FLAGS:
+            with self.assertRaises(SystemExit) as cm:
+                self.CWCTL.run(["test", "some_instance", "--dbglevel", dbg_flag])
+            self.assertEqual(cm.exception.code, 0)
+
+            total_value += getattr(server, "DBG_%s" % dbg_flag)
+            self.assertEqual(total_value, server.DEBUG)
+
 
 if __name__ == '__main__':
     unittest.main()
--- a/doc/changes/3.27.rst	Tue May 21 18:08:17 2019 +0200
+++ b/doc/changes/3.27.rst	Tue May 21 18:16:51 2019 +0200
@@ -20,8 +20,8 @@
 * add a --pdb flag to all cubicweb-ctl command to launch (i)pdb if an exception
   occurs during a command execution.
 
-* the --loglevel flag is available for all cubicweb-ctl instance commands (and
-  not only the ``pyramid`` one)
+* the --loglevel and --dbglevel flags are available for all cubicweb-ctl
+  instance commands (and not only the ``pyramid`` one)
 
 Backwards incompatible changes
 ------------------------------