server/serverconfig.py
branchstable
changeset 2107 6c4a4c514ac2
parent 2105 92ea410806fe
child 2476 1294a6bdf3bf
equal deleted inserted replaced
2106:2295f2aba61d 2107:6c4a4c514ac2
     8 __docformat__ = "restructuredtext en"
     8 __docformat__ = "restructuredtext en"
     9 
     9 
    10 import os
    10 import os
    11 from os.path import join, exists
    11 from os.path import join, exists
    12 
    12 
    13 from logilab.common.configuration import Method, Configuration, \
    13 from logilab.common.configuration import REQUIRED, Method, Configuration, \
    14      ini_format_section
    14      ini_format_section
    15 from logilab.common.decorators import wproperty, cached, clear_cache
    15 from logilab.common.decorators import wproperty, cached, clear_cache
    16 
    16 
    17 from cubicweb import CW_SOFTWARE_ROOT, RegistryNotFound
    17 from cubicweb import CW_SOFTWARE_ROOT, RegistryNotFound
    18 from cubicweb.toolsutils import env_path, read_config, restrict_perms_to_user
    18 from cubicweb.toolsutils import env_path, read_config, restrict_perms_to_user
    19 from cubicweb.cwconfig import CubicWebConfiguration, merge_options
    19 from cubicweb.cwconfig import CubicWebConfiguration, merge_options
    20 
    20 from cubicweb.server import SOURCE_TYPES
       
    21 
       
    22 
       
    23 USER_OPTIONS =  (
       
    24     ('login', {'type' : 'string',
       
    25                'default': REQUIRED,
       
    26                'help': "cubicweb manager account's login "
       
    27                '(this user will be created)',
       
    28                'inputlevel': 0,
       
    29                }),
       
    30     ('password', {'type' : 'password',
       
    31                   'help': "cubicweb manager account's password",
       
    32                   'inputlevel': 0,
       
    33                   }),
       
    34     )
    21 
    35 
    22 def generate_sources_file(sourcesfile, sourcescfg, keys=None):
    36 def generate_sources_file(sourcesfile, sourcescfg, keys=None):
    23     """serialize repository'sources configuration into a INI like file
    37     """serialize repository'sources configuration into a INI like file
    24 
    38 
    25     the `keys` parameter may be used to sort sections
    39     the `keys` parameter may be used to sort sections
    33     stream = open(sourcesfile, 'w')
    47     stream = open(sourcesfile, 'w')
    34     for uri in keys:
    48     for uri in keys:
    35         sconfig = sourcescfg[uri]
    49         sconfig = sourcescfg[uri]
    36         if isinstance(sconfig, dict):
    50         if isinstance(sconfig, dict):
    37             # get a Configuration object
    51             # get a Configuration object
    38             _sconfig = Configuration(options=SOURCE_TYPES[sconfig['adapter']].options)
    52             if uri == 'admin':
       
    53                 options = USER_OPTIONS
       
    54             else:
       
    55                 options = SOURCE_TYPES[sconfig['adapter']].options
       
    56             _sconfig = Configuration(options=options)
    39             for attr, val in sconfig.items():
    57             for attr, val in sconfig.items():
    40                 if attr == 'uri':
    58                 if attr == 'uri':
    41                     continue
    59                     continue
    42                 if attr == 'adapter':
    60                 if attr == 'adapter':
    43                     _sconfig.adapter = val
    61                     _sconfig.adapter = val
   237         return dict((uri, config) for uri, config in allsources.items()
   255         return dict((uri, config) for uri, config in allsources.items()
   238                     if uri in self._enabled_sources or uri == 'admin')
   256                     if uri in self._enabled_sources or uri == 'admin')
   239 
   257 
   240     def write_sources_file(self, sourcescfg):
   258     def write_sources_file(self, sourcescfg):
   241         sourcesfile = self.sources_file()
   259         sourcesfile = self.sources_file()
       
   260         if exists(sourcesfile):
       
   261             import shutil
       
   262             shutil.copy(sourcesfile, sourcesfile + '.bak')
   242         generate_sources_file(sourcesfile, sourcescfg, ['admin', 'system'])
   263         generate_sources_file(sourcesfile, sourcescfg, ['admin', 'system'])
   243         restrict_perms_to_user(sourcesfile)
   264         restrict_perms_to_user(sourcesfile)
   244 
   265 
   245     def pyro_enabled(self):
   266     def pyro_enabled(self):
   246         """pyro is always enabled in standalone repository configuration"""
   267         """pyro is always enabled in standalone repository configuration"""