cubicweb/web/webctl.py
changeset 11057 0b59724cb3f2
parent 10589 7c23b7de2b8d
child 11461 f5a4e14d1dd2
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """cubicweb-ctl commands and command handlers common to twisted/modpython
       
    19 web configuration
       
    20 """
       
    21 from __future__ import print_function
       
    22 
       
    23 __docformat__ = "restructuredtext en"
       
    24 
       
    25 import os, os.path as osp
       
    26 from shutil import copy, rmtree
       
    27 
       
    28 from logilab.common.shellutils import ASK
       
    29 
       
    30 from cubicweb import ExecutionError
       
    31 from cubicweb.cwctl import CWCTL
       
    32 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg
       
    33 from cubicweb.toolsutils import Command, CommandHandler, underline_title
       
    34 
       
    35 
       
    36 try:
       
    37     from os import symlink as linkdir
       
    38 except ImportError:
       
    39     from shutil import copytree as linkdir
       
    40 
       
    41 
       
    42 class WebCreateHandler(CommandHandler):
       
    43     cmdname = 'create'
       
    44 
       
    45     def bootstrap(self, cubes, automatic=False, inputlevel=0):
       
    46         """bootstrap this configuration"""
       
    47         if not automatic:
       
    48             print('\n' + underline_title('Generic web configuration'))
       
    49             config = self.config
       
    50             config.input_config('web', inputlevel)
       
    51             if ASK.confirm('Allow anonymous access ?', False):
       
    52                 config.global_set_option('anonymous-user', 'anon')
       
    53                 config.global_set_option('anonymous-password', 'anon')
       
    54 
       
    55     def postcreate(self, *args, **kwargs):
       
    56         """hooks called once instance's initialization has been completed"""
       
    57 
       
    58 
       
    59 class GenStaticDataDirMixIn(object):
       
    60     """Create a directory merging all data directory content from cubes and CW.
       
    61     """
       
    62     def generate_static_dir(self, config, dest=None, ask_clean=False, repo=None):
       
    63         if not dest:
       
    64             dest = config['staticdir-path']
       
    65         if not dest:
       
    66             dest = osp.join(config.appdatahome, 'data')
       
    67         if osp.exists(dest):
       
    68             if (not ask_clean or
       
    69                 not (config.verbosity and
       
    70                      ASK.confirm('Remove existing data directory %s?' % dest))):
       
    71                 raise ExecutionError('Directory %s already exists. '
       
    72                                      'Remove it first.' % dest)
       
    73             rmtree(dest)
       
    74         config.quick_start = True # notify this is not a regular start
       
    75         # list all resources (no matter their order)
       
    76         resources = set()
       
    77         for datadir in self._datadirs(config, repo=repo):
       
    78             for dirpath, dirnames, filenames in os.walk(datadir):
       
    79                 rel_dirpath = dirpath[len(datadir)+1:]
       
    80                 resources.update(osp.join(rel_dirpath, f) for f in filenames)
       
    81         # locate resources and copy them to destination
       
    82         for resource in resources:
       
    83             dest_resource = osp.join(dest, resource)
       
    84             dirname = osp.dirname(dest_resource)
       
    85             if not osp.isdir(dirname):
       
    86                 os.makedirs(dirname)
       
    87             resource_dir, resource_path = config.locate_resource(resource)
       
    88             copy(osp.join(resource_dir, resource_path), dest_resource)
       
    89         # handle md5 version subdirectory
       
    90         linkdir(dest, osp.join(dest, config.instance_md5_version()))
       
    91         print('You can use apache rewrite rule below :\n'
       
    92               'RewriteRule ^/data/(.*) %s/$1 [L]' % dest)
       
    93 
       
    94     def _datadirs(self, config, repo=None):
       
    95         if repo is None:
       
    96             repo = config.repository()
       
    97         if config._cubes is None:
       
    98             # web only config
       
    99             config.init_cubes(repo.get_cubes())
       
   100         for cube in repo.get_cubes():
       
   101             cube_datadir = osp.join(cwcfg.cube_dir(cube), 'data')
       
   102             if osp.isdir(cube_datadir):
       
   103                 yield cube_datadir
       
   104         yield osp.join(config.shared_dir(), 'data')
       
   105 
       
   106 
       
   107 class WebUpgradeHandler(CommandHandler, GenStaticDataDirMixIn):
       
   108     cmdname = 'upgrade'
       
   109 
       
   110     def postupgrade(self, repo):
       
   111         config = self.config
       
   112         if not config['generate-staticdir']:
       
   113             return
       
   114         self.generate_static_dir(config, ask_clean=True, repo=repo)
       
   115 
       
   116 
       
   117 class GenStaticDataDir(Command, GenStaticDataDirMixIn):
       
   118     """Create a directory merging all data directory content from cubes and CW.
       
   119     """
       
   120     name = 'gen-static-datadir'
       
   121     arguments = '<instance> [dirpath]'
       
   122     min_args = 1
       
   123     max_args = 2
       
   124 
       
   125     options = ()
       
   126 
       
   127     def run(self, args):
       
   128         appid = args.pop(0)
       
   129         config = cwcfg.config_for(appid)
       
   130         dest = None
       
   131         if args:
       
   132             dest = args[0]
       
   133         self.generate_static_dir(config, dest)
       
   134 
       
   135 
       
   136 CWCTL.register(GenStaticDataDir)