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