etwist/twctl.py
changeset 0 b97547f5f1fa
child 1802 d628defebc17
equal deleted inserted replaced
-1:000000000000 0:b97547f5f1fa
       
     1 """cubicweb-clt handlers for twisted
       
     2 """
       
     3 
       
     4 import sys
       
     5 
       
     6 from cubicweb.toolsutils import CommandHandler
       
     7 from cubicweb.web.webctl import WebCreateHandler
       
     8 
       
     9 # trigger configuration registration
       
    10 import cubicweb.etwist.twconfig # pylint: disable-msg=W0611
       
    11 
       
    12 
       
    13 class TWCreateHandler(WebCreateHandler):
       
    14     cfgname = 'twisted'
       
    15 
       
    16     def bootstrap(self, cubes, inputlevel=0):
       
    17         """bootstrap this configuration"""
       
    18         print '** twisted configuration'
       
    19         mainpyfile = self.config.server_file()
       
    20         mainpy = open(mainpyfile, 'w')
       
    21         mainpy.write('''
       
    22 from cubicweb.etwist import server
       
    23 application = server.main(%r, %r)
       
    24 ''' % (self.config.appid, self.config.name))
       
    25         mainpy.close()
       
    26         print 'application\'s twisted file %s generated' % mainpyfile
       
    27         super(TWCreateHandler, self).bootstrap(cubes, inputlevel)
       
    28 
       
    29 
       
    30 class TWStartHandler(CommandHandler):
       
    31     cmdname = 'start'
       
    32     cfgname = 'twisted'
       
    33 
       
    34     def start_command(self, config, debug):
       
    35         command = ['%s `which twistd`' % sys.executable]
       
    36         for ctl_opt, server_opt in (('pid-file', 'pidfile'),
       
    37                                     ('uid', 'uid'),
       
    38                                     ('log-file', 'logfile',)):
       
    39             value = config[ctl_opt]
       
    40             if not value or (debug and ctl_opt == 'log-file'):
       
    41                 continue
       
    42             command.append('--%s %s' % (server_opt, value))
       
    43         if debug:
       
    44             command.append('-n')
       
    45         if config['profile']:
       
    46             command.append('-p %s --savestats' % config['profile'])
       
    47         command.append('-oy')
       
    48         command.append(self.config.server_file())
       
    49         return ' '.join(command)
       
    50 
       
    51 
       
    52 class TWStopHandler(CommandHandler):
       
    53     cmdname = 'stop'
       
    54     cfgname = 'twisted'
       
    55     
       
    56     
       
    57 try:
       
    58     from cubicweb.server import serverctl
       
    59 
       
    60     class AllInOneCreateHandler(serverctl.RepositoryCreateHandler, TWCreateHandler):
       
    61         """configuration to get a web application running in a twisted web
       
    62         server integrating a repository server in the same process
       
    63         """
       
    64         cfgname = 'all-in-one'
       
    65 
       
    66         def bootstrap(self, cubes, inputlevel=0):
       
    67             """bootstrap this configuration"""
       
    68             serverctl.RepositoryCreateHandler.bootstrap(self, cubes, inputlevel)
       
    69             TWCreateHandler.bootstrap(self, cubes, inputlevel)
       
    70             
       
    71     class AllInOneStartHandler(TWStartHandler):
       
    72         cmdname = 'start'
       
    73         cfgname = 'all-in-one'
       
    74         subcommand = 'cubicweb-twisted'
       
    75 
       
    76     class AllInOneStopHandler(serverctl.RepositoryStopHandler):
       
    77         cmdname = 'stop'
       
    78         cfgname = 'all-in-one'
       
    79         subcommand = 'cubicweb-twisted'
       
    80 
       
    81 except ImportError:
       
    82     pass
       
    83