web/webctl.py
changeset 7274 4653a2a5110b
parent 7140 ba51dac1115d
child 7275 bb3bb8104134
equal deleted inserted replaced
7273:a949fc438029 7274:4653a2a5110b
    19 web configuration
    19 web configuration
    20 """
    20 """
    21 
    21 
    22 __docformat__ = "restructuredtext en"
    22 __docformat__ = "restructuredtext en"
    23 
    23 
       
    24 import os, os.path as osp
       
    25 from shutil import copy
       
    26 
    24 from logilab.common.shellutils import ASK
    27 from logilab.common.shellutils import ASK
    25 
    28 
    26 from cubicweb.toolsutils import CommandHandler, underline_title
    29 from cubicweb import ExecutionError
       
    30 from cubicweb.cwctl import CWCTL
       
    31 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg
       
    32 from cubicweb.toolsutils import Command, CommandHandler, underline_title
       
    33 
    27 
    34 
    28 class WebCreateHandler(CommandHandler):
    35 class WebCreateHandler(CommandHandler):
    29     cmdname = 'create'
    36     cmdname = 'create'
    30 
    37 
    31     def bootstrap(self, cubes, automatic=False, inputlevel=0):
    38     def bootstrap(self, cubes, automatic=False, inputlevel=0):
    41                 config.global_set_option('anonymous-user', 'anon')
    48                 config.global_set_option('anonymous-user', 'anon')
    42                 config.global_set_option('anonymous-password', 'anon')
    49                 config.global_set_option('anonymous-password', 'anon')
    43 
    50 
    44     def postcreate(self, *args, **kwargs):
    51     def postcreate(self, *args, **kwargs):
    45         """hooks called once instance's initialization has been completed"""
    52         """hooks called once instance's initialization has been completed"""
       
    53 
       
    54 
       
    55 class GenStaticDataDir(Command):
       
    56     """Create a directory merging all data directory content from cubes and CW.
       
    57     """
       
    58     name = 'gen-static-datadir'
       
    59     arguments = '<instance> [dirpath]'
       
    60     min_args = 1
       
    61     max_args = 2
       
    62 
       
    63     options = ()
       
    64 
       
    65     def run(self, args):
       
    66         appid = args.pop(0)
       
    67         config = cwcfg.config_for(appid)
       
    68         if args:
       
    69             dest = args[0]
       
    70         else:
       
    71             dest = osp.join(config.appdatahome, 'data')
       
    72         if osp.exists(dest):
       
    73             raise ExecutionError('Directory %s already exists. '
       
    74                                  'Remove it first.' % dest)
       
    75         config.quick_start = True # notify this is not a regular start
       
    76         # list all resources (no matter their order)
       
    77         resources = set()
       
    78         for datadir in self._datadirs(config):
       
    79             for dirpath, dirnames, filenames in os.walk(datadir):
       
    80                 rel_dirpath = dirpath[len(datadir)+1:]
       
    81                 resources.update(osp.join(rel_dirpath, f) for f in filenames)
       
    82         # locate resources and copy them to destination
       
    83         for resource in resources:
       
    84             dirname = osp.dirname(resource)
       
    85             dest_resource = osp.join(dest, dirname)
       
    86             if not osp.isdir(dest_resource):
       
    87                 os.makedirs(dest_resource)
       
    88             resource_dir, resource_path = config.locate_resource(resource)
       
    89             copy(osp.join(resource_dir, resource_path), dest_resource)
       
    90         print ('You can use apache rewrite rule below :\n'
       
    91                'RewriteRule ^/data/(.*) %s/$1 [L]' % dest)
       
    92 
       
    93     def _datadirs(self, config):
       
    94         repo = config.repository()
       
    95         if config._cubes is None:
       
    96             # web only config
       
    97             config.init_cubes(repo.get_cubes())
       
    98         for cube in repo.get_cubes():
       
    99             cube_datadir = osp.join(cwcfg.cube_dir(cube), 'data')
       
   100             if osp.isdir(cube_datadir):
       
   101                 yield cube_datadir
       
   102         yield osp.join(config.shared_dir(), 'data')
       
   103 
       
   104 CWCTL.register(GenStaticDataDir)