cubicweb/pyramid/pyramidctl.py
changeset 12736 5add82b08a6d
parent 12730 6c48a49cd3c2
child 12737 56f7386bc6b9
equal deleted inserted replaced
12735:17d1b1f4eddd 12736:5add82b08a6d
    30 import sys
    30 import sys
    31 import time
    31 import time
    32 import threading
    32 import threading
    33 import subprocess
    33 import subprocess
    34 
    34 
    35 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg
    35 from logilab.common.configuration import merge_options
       
    36 
    36 from cubicweb.cwctl import CWCTL, InstanceCommand, init_cmdline_log_threshold
    37 from cubicweb.cwctl import CWCTL, InstanceCommand, init_cmdline_log_threshold
    37 from cubicweb.pyramid import wsgi_application_from_cwconfig
    38 from cubicweb.pyramid import wsgi_application_from_cwconfig
    38 from cubicweb.pyramid.config import get_random_secret_key
    39 from cubicweb.pyramid.config import get_random_secret_key
    39 from cubicweb.server import serverctl, set_debug
    40 from cubicweb.server import serverctl, set_debug
    40 from cubicweb.web.webctl import WebCreateHandler
    41 from cubicweb.web.webctl import WebCreateHandler
    43 import waitress
    44 import waitress
    44 
    45 
    45 MAXFD = 1024
    46 MAXFD = 1024
    46 
    47 
    47 DBG_FLAGS = ('RQL', 'SQL', 'REPO', 'HOOKS', 'OPS', 'SEC', 'MORE')
    48 DBG_FLAGS = ('RQL', 'SQL', 'REPO', 'HOOKS', 'OPS', 'SEC', 'MORE')
    48 LOG_LEVELS = ('debug', 'info', 'warning', 'error')
       
    49 
    49 
    50 
    50 
    51 def _generate_pyramid_ini_file(pyramid_ini_path):
    51 def _generate_pyramid_ini_file(pyramid_ini_path):
    52     """Write a 'pyramid.ini' file into apphome."""
    52     """Write a 'pyramid.ini' file into apphome."""
    53     template_fpath = os.path.join(os.path.dirname(__file__), 'pyramid.ini.tmpl')
    53     template_fpath = os.path.join(os.path.dirname(__file__), 'pyramid.ini.tmpl')
    91       identifier of the instance to configure.
    91       identifier of the instance to configure.
    92     """
    92     """
    93     name = 'pyramid'
    93     name = 'pyramid'
    94     actionverb = 'started'
    94     actionverb = 'started'
    95 
    95 
    96     options = (
    96     options = merge_options((
    97         ('debug-mode',
    97         ('debug-mode',
    98          {'action': 'store_true',
    98          {'action': 'store_true',
    99           'help': 'Activate the repository debug mode ('
    99           'help': 'Activate the repository debug mode ('
   100                   'logs in the console and the debug toolbar).'}),
   100                   'logs in the console and the debug toolbar).'}),
   101         ('debug',
   101         ('debug',
   105          {'action': 'store_true',
   105          {'action': 'store_true',
   106           'help': 'Restart the server if any source file is changed'}),
   106           'help': 'Restart the server if any source file is changed'}),
   107         ('reload-interval',
   107         ('reload-interval',
   108          {'type': 'int', 'default': 1,
   108          {'type': 'int', 'default': 1,
   109           'help': 'Interval, in seconds, between file modifications checks'}),
   109           'help': 'Interval, in seconds, between file modifications checks'}),
   110         ('loglevel',
       
   111          {'short': 'l', 'type': 'choice', 'metavar': '<log level>',
       
   112           'default': None, 'choices': LOG_LEVELS,
       
   113           'help': 'debug if -D is set, error otherwise; '
       
   114                   'one of %s' % (LOG_LEVELS,),
       
   115           }),
       
   116         ('dbglevel',
   110         ('dbglevel',
   117          {'type': 'multiple_choice', 'metavar': '<dbg level>',
   111          {'type': 'multiple_choice', 'metavar': '<dbg level>',
   118           'default': None,
   112           'default': None,
   119           'choices': DBG_FLAGS,
   113           'choices': DBG_FLAGS,
   120           'help': ('Set the server debugging flags; you may choose several '
   114           'help': ('Set the server debugging flags; you may choose several '
   138          {'short': 'p',
   132          {'short': 'p',
   139           'type': 'named',
   133           'type': 'named',
   140           'metavar': 'key1:value1,key2:value2',
   134           'metavar': 'key1:value1,key2:value2',
   141           'default': {},
   135           'default': {},
   142           'help': 'override <key> configuration file option with <value>.'}),
   136           'help': 'override <key> configuration file option with <value>.'}),
   143     )
   137     ) + InstanceCommand.options)
   144 
   138 
   145     _reloader_environ_key = 'CW_RELOADER_SHOULD_RUN'
   139     _reloader_environ_key = 'CW_RELOADER_SHOULD_RUN'
   146 
   140 
   147     def debug(self, msg):
   141     def debug(self, msg):
   148         print('DEBUG - %s' % msg)
   142         print('DEBUG - %s' % msg)
   243     def pyramid_instance(self, appid):
   237     def pyramid_instance(self, appid):
   244         self._needreload = False
   238         self._needreload = False
   245 
   239 
   246         autoreload = self['reload'] or self['debug']
   240         autoreload = self['reload'] or self['debug']
   247 
   241 
   248         # debugmode=True is to force to have a StreamHandler used instead of
   242         cwconfig = self.cwconfig
   249         # writting the logs into a file in /tmp
       
   250         cwconfig = cwcfg.config_for(appid, debugmode=True)
       
   251         filelist_path = os.path.join(cwconfig.apphome,
   243         filelist_path = os.path.join(cwconfig.apphome,
   252                                      '.pyramid-reload-files.list')
   244                                      '.pyramid-reload-files.list')
   253 
   245 
   254         pyramid_ini_path = os.path.join(cwconfig.apphome, "pyramid.ini")
   246         pyramid_ini_path = os.path.join(cwconfig.apphome, "pyramid.ini")
   255         if not os.path.exists(pyramid_ini_path):
   247         if not os.path.exists(pyramid_ini_path):
   267             self.install_reloader(
   259             self.install_reloader(
   268                 self['reload-interval'], extra_files,
   260                 self['reload-interval'], extra_files,
   269                 filelist_path=filelist_path)
   261                 filelist_path=filelist_path)
   270 
   262 
   271         if self['dbglevel']:
   263         if self['dbglevel']:
   272             self['loglevel'] = 'debug'
       
   273             set_debug('|'.join('DBG_' + x.upper() for x in self['dbglevel']))
   264             set_debug('|'.join('DBG_' + x.upper() for x in self['dbglevel']))
   274         init_cmdline_log_threshold(cwconfig, self['loglevel'])
   265 
       
   266         # if no loglevel is specified and --debug or --dbglevel are here, set log level at debug
       
   267         if self['loglevel'] is None and (self['debug'] or self['dbglevel']):
       
   268             init_cmdline_log_threshold(cwconfig, 'debug')
   275 
   269 
   276         app = wsgi_application_from_cwconfig(
   270         app = wsgi_application_from_cwconfig(
   277             cwconfig, profile=self['profile'],
   271             cwconfig, profile=self['profile'],
   278             profile_output=self['profile-output'],
   272             profile_output=self['profile-output'],
   279             profile_dump_every=self['profile-dump-every']
   273             profile_dump_every=self['profile-dump-every']