cwctl.py
changeset 8669 62213a34726e
parent 8564 0e56f0c3fd1e
child 8682 20bd1cdf86ae
equal deleted inserted replaced
8668:4fea61c636b2 8669:62213a34726e
    25 # possible (for cubicweb-ctl reactivity, necessary for instance for usable bash
    25 # possible (for cubicweb-ctl reactivity, necessary for instance for usable bash
    26 # completion). So import locally in command helpers.
    26 # completion). So import locally in command helpers.
    27 import sys
    27 import sys
    28 from warnings import warn
    28 from warnings import warn
    29 from os import remove, listdir, system, pathsep
    29 from os import remove, listdir, system, pathsep
       
    30 from os.path import exists, join, isfile, isdir, dirname, abspath
    30 try:
    31 try:
    31     from os import kill, getpgid
    32     from os import kill, getpgid
    32 except ImportError:
    33 except ImportError:
    33     def kill(*args):
    34     def kill(*args):
    34         """win32 kill implementation"""
    35         """win32 kill implementation"""
    35     def getpgid():
    36     def getpgid():
    36         """win32 getpgid implementation"""
    37         """win32 getpgid implementation"""
    37 
    38 
    38 
    39 
    39 from os.path import exists, join, isfile, isdir, dirname, abspath
       
    40 
       
    41 from urlparse import urlparse
       
    42 
    40 
    43 from logilab.common.clcommands import CommandLine
    41 from logilab.common.clcommands import CommandLine
    44 from logilab.common.shellutils import ASK
    42 from logilab.common.shellutils import ASK
    45 
    43 
    46 from cubicweb import ConfigurationError, ExecutionError, BadCommandUsage
    44 from cubicweb import ConfigurationError, ExecutionError, BadCommandUsage
   872         ('repo-uri',
   870         ('repo-uri',
   873          {'short': 'H', 'type' : 'string', 'metavar': '<protocol>://<[host][:port]>',
   871          {'short': 'H', 'type' : 'string', 'metavar': '<protocol>://<[host][:port]>',
   874           'help': 'URI of the CubicWeb repository to connect to. URI can be \
   872           'help': 'URI of the CubicWeb repository to connect to. URI can be \
   875 pyro://[host:port] the Pyro name server host; if the pyro nameserver is not set, \
   873 pyro://[host:port] the Pyro name server host; if the pyro nameserver is not set, \
   876 it will be detected by using a broadcast query, a ZMQ URL or \
   874 it will be detected by using a broadcast query, a ZMQ URL or \
   877 inmemory:// (default) use an in-memory repository.',
   875 inmemory:// (default) use an in-memory repository. THIS OPTION IS DEPRECATED, \
       
   876 directly give URI as instance id instead',
   878           'group': 'remote'
   877           'group': 'remote'
   879           }),
   878           }),
   880         )
   879         )
   881 
   880 
   882     def run(self, args):
   881     def run(self, args):
   883         appid = args.pop(0)
   882         from urlparse import urlparse
       
   883         appuri = args.pop(0)
   884         if self.config.repo_uri:
   884         if self.config.repo_uri:
   885             uri = urlparse(self.config.repo_uri)
   885             warn('[3.16] --repo-uri option is deprecated, directly give the URI as instance id',
   886             if uri.scheme == 'pyro':
   886                  DeprecationWarning)
   887                 cnxtype = uri.scheme
   887             if urlparse(self.config.repo_uri).scheme in ('pyro', 'inmemory'):
   888                 hostport = uri.netloc
   888                 appuri = '%s/%s' % (self.config.repo_uri.rstrip('/'), appuri)
   889             elif uri.scheme == 'inmemory':
   889         scheme = urlparse(self.config.repo_uri).scheme
   890                 cnxtype = ''
   890         if scheme not in ('', 'inmemory'):
   891                 hostport = ''
       
   892             else:
       
   893                 cnxtype = 'zmq'
       
   894                 hostport = self.config.repo_uri
       
   895         else:
       
   896             cnxtype = ''
       
   897 
       
   898         if cnxtype:
       
   899             from cubicweb import AuthenticationError
   891             from cubicweb import AuthenticationError
   900             from cubicweb.dbapi import connect, ConnectionProperties
   892             from cubicweb.dbapi import connect
   901             from cubicweb.server.utils import manager_userpasswd
   893             from cubicweb.server.utils import manager_userpasswd
   902             from cubicweb.server.migractions import ServerMigrationHelper
   894             from cubicweb.server.migractions import ServerMigrationHelper
   903             cnxprops = ConnectionProperties(cnxtype=cnxtype)
       
   904 
       
   905             while True:
   895             while True:
   906                 try:
   896                 try:
   907                     login, pwd = manager_userpasswd(msg=None)
   897                     login, pwd = manager_userpasswd(msg=None)
   908                     cnx = connect(appid, login=login, password=pwd,
   898                     cnx = connect(appuri, login=login, password=pwd, mulcnx=False)
   909                                   host=hostport, mulcnx=False,
       
   910                                   cnxprops=cnxprops)
       
   911                 except AuthenticationError, ex:
   899                 except AuthenticationError, ex:
   912                     print ex
   900                     print ex
   913                 except (KeyboardInterrupt, EOFError):
   901                 except (KeyboardInterrupt, EOFError):
   914                     print
   902                     print
   915                     sys.exit(0)
   903                     sys.exit(0)
   941                     mih.cmd_process_script(script, scriptargs=args)
   929                     mih.cmd_process_script(script, scriptargs=args)
   942                     mih.commit()
   930                     mih.commit()
   943             else:
   931             else:
   944                 mih.interactive_shell()
   932                 mih.interactive_shell()
   945         finally:
   933         finally:
   946             if not cnxtype: # shutdown in-memory repo
   934             if scheme in ('', 'inmemory'): # shutdown in-memory repo
   947                 mih.shutdown()
   935                 mih.shutdown()
   948             else:
   936             else:
   949                 cnx.close()
   937                 cnx.close()
   950 
   938 
   951 
   939