cubicweb/pyramid/pyramidctl.py
branch3.24
changeset 11817 48f6ebd33cb9
parent 11814 ab8f652a9c99
child 11821 7534b32c45e3
equal deleted inserted replaced
11816:6392f34fcdad 11817:48f6ebd33cb9
     9 import atexit
     9 import atexit
    10 import errno
    10 import errno
    11 import os
    11 import os
    12 import signal
    12 import signal
    13 import sys
    13 import sys
    14 import tempfile
       
    15 import time
    14 import time
    16 import threading
    15 import threading
    17 import subprocess
    16 import subprocess
    18 
    17 
    19 from cubicweb import BadCommandUsage, ExecutionError
    18 from cubicweb import BadCommandUsage, ExecutionError
    93           'default': {},
    92           'default': {},
    94           'help': 'override <key> configuration file option with <value>.'}),
    93           'help': 'override <key> configuration file option with <value>.'}),
    95     )
    94     )
    96 
    95 
    97     _reloader_environ_key = 'CW_RELOADER_SHOULD_RUN'
    96     _reloader_environ_key = 'CW_RELOADER_SHOULD_RUN'
    98     _reloader_filelist_environ_key = 'CW_RELOADER_FILELIST'
       
    99 
    97 
   100     def debug(self, msg):
    98     def debug(self, msg):
   101         print('DEBUG - %s' % msg)
    99         print('DEBUG - %s' % msg)
   102 
   100 
   103     def info(self, msg):
   101     def info(self, msg):
   216         os.open(REDIRECT_TO, os.O_RDWR)  # standard input (0)
   214         os.open(REDIRECT_TO, os.O_RDWR)  # standard input (0)
   217         # Duplicate standard input to standard output and standard error.
   215         # Duplicate standard input to standard output and standard error.
   218         os.dup2(0, 1)  # standard output (1)
   216         os.dup2(0, 1)  # standard output (1)
   219         os.dup2(0, 2)  # standard error (2)
   217         os.dup2(0, 2)  # standard error (2)
   220 
   218 
   221     def restart_with_reloader(self):
   219     def restart_with_reloader(self, filelist_path):
   222         self.debug('Starting subprocess with file monitor')
   220         self.debug('Starting subprocess with file monitor')
   223 
   221 
   224         with tempfile.NamedTemporaryFile(delete=False) as f:
   222         # Create or clear monitored files list file.
   225             filelist_path = f.name
   223         with open(filelist_path, 'w') as f:
       
   224             pass
   226 
   225 
   227         while True:
   226         while True:
   228             args = [self.quote_first_command_arg(sys.executable)] + sys.argv
   227             args = [self.quote_first_command_arg(sys.executable)] + sys.argv
   229             new_environ = os.environ.copy()
   228             new_environ = os.environ.copy()
   230             new_environ[self._reloader_environ_key] = 'true'
   229             new_environ[self._reloader_environ_key] = 'true'
   231             new_environ[self._reloader_filelist_environ_key] = filelist_path
       
   232             proc = None
   230             proc = None
   233             try:
   231             try:
   234                 try:
   232                 try:
   235                     proc = subprocess.Popen(args, env=new_environ)
   233                     proc = subprocess.Popen(args, env=new_environ)
   236                     exit_code = proc.wait()
   234                     exit_code = proc.wait()
   293 
   291 
   294         debugmode = self['debug-mode'] or self['debug']
   292         debugmode = self['debug-mode'] or self['debug']
   295         autoreload = self['reload'] or self['debug']
   293         autoreload = self['reload'] or self['debug']
   296         daemonize = not (self['no-daemon'] or debugmode or autoreload)
   294         daemonize = not (self['no-daemon'] or debugmode or autoreload)
   297 
   295 
       
   296         cwconfig = cwcfg.config_for(appid, debugmode=debugmode)
       
   297         filelist_path = os.path.join(cwconfig.apphome,
       
   298                                      '.pyramid-reload-files.list')
       
   299 
   298         if autoreload and not os.environ.get(self._reloader_environ_key):
   300         if autoreload and not os.environ.get(self._reloader_environ_key):
   299             return self.restart_with_reloader()
   301             return self.restart_with_reloader(filelist_path)
   300 
   302 
   301         cwconfig = cwcfg.config_for(appid, debugmode=debugmode)
       
   302         if autoreload:
   303         if autoreload:
   303             _turn_sigterm_into_systemexit()
   304             _turn_sigterm_into_systemexit()
   304             self.debug('Running reloading file monitor')
   305             self.debug('Running reloading file monitor')
   305             extra_files = [sys.argv[0]]
   306             extra_files = [sys.argv[0]]
   306             extra_files.extend(self.configfiles(cwconfig))
   307             extra_files.extend(self.configfiles(cwconfig))
   307             extra_files.extend(self.i18nfiles(cwconfig))
   308             extra_files.extend(self.i18nfiles(cwconfig))
   308             self.install_reloader(
   309             self.install_reloader(
   309                 self['reload-interval'], extra_files,
   310                 self['reload-interval'], extra_files,
   310                 filelist_path=os.environ.get(
   311                 filelist_path=filelist_path)
   311                     self._reloader_filelist_environ_key))
       
   312 
   312 
   313         if daemonize:
   313         if daemonize:
   314             self.daemonize(cwconfig['pid-file'])
   314             self.daemonize(cwconfig['pid-file'])
   315             self.record_pid(cwconfig['pid-file'])
   315             self.record_pid(cwconfig['pid-file'])
   316 
   316