web/webctl.py
author Florent Cayré <florent.cayre@gmail.com>
Thu, 28 Apr 2011 17:36:04 +0200
changeset 7274 4653a2a5110b
parent 7140 ba51dac1115d
child 7275 bb3bb8104134
permissions -rw-r--r--
add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7132
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
     1
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5415
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5415
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5415
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5415
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5415
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5415
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5415
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5415
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5415
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5415
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5415
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5415
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5415
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5415
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5415
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
"""cubicweb-ctl commands and command handlers common to twisted/modpython
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
web configuration
7132
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    20
"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
7274
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    24
import os, os.path as osp
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    25
from shutil import copy
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    26
7132
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    27
from logilab.common.shellutils import ASK
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    28
7274
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    29
from cubicweb import ExecutionError
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    30
from cubicweb.cwctl import CWCTL
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    31
from cubicweb.cwconfig import CubicWebConfiguration as cwcfg
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    32
from cubicweb.toolsutils import Command, CommandHandler, underline_title
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    33
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
class WebCreateHandler(CommandHandler):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
    cmdname = 'create'
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1132
diff changeset
    37
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7132
diff changeset
    38
    def bootstrap(self, cubes, automatic=False, inputlevel=0):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
        """bootstrap this configuration"""
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7132
diff changeset
    40
        if not automatic:
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7132
diff changeset
    41
            print '\n' + underline_title('Generic web configuration')
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7132
diff changeset
    42
            config = self.config
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7132
diff changeset
    43
            if config.repo_method == 'pyro' or config.pyro_enabled():
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7132
diff changeset
    44
                print '\n' + underline_title('Pyro configuration')
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7132
diff changeset
    45
                config.input_config('pyro', inputlevel)
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7132
diff changeset
    46
            config.input_config('web', inputlevel)
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7132
diff changeset
    47
            if ASK.confirm('Allow anonymous access ?', False):
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7132
diff changeset
    48
                config.global_set_option('anonymous-user', 'anon')
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7132
diff changeset
    49
                config.global_set_option('anonymous-password', 'anon')
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1132
diff changeset
    50
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7132
diff changeset
    51
    def postcreate(self, *args, **kwargs):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2395
diff changeset
    52
        """hooks called once instance's initialization has been completed"""
7274
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    53
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    54
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    55
class GenStaticDataDir(Command):
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    56
    """Create a directory merging all data directory content from cubes and CW.
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    57
    """
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    58
    name = 'gen-static-datadir'
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    59
    arguments = '<instance> [dirpath]'
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    60
    min_args = 1
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    61
    max_args = 2
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    62
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    63
    options = ()
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    64
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    65
    def run(self, args):
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    66
        appid = args.pop(0)
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    67
        config = cwcfg.config_for(appid)
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    68
        if args:
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    69
            dest = args[0]
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    70
        else:
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    71
            dest = osp.join(config.appdatahome, 'data')
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    72
        if osp.exists(dest):
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    73
            raise ExecutionError('Directory %s already exists. '
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    74
                                 'Remove it first.' % dest)
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    75
        config.quick_start = True # notify this is not a regular start
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    76
        # list all resources (no matter their order)
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    77
        resources = set()
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    78
        for datadir in self._datadirs(config):
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    79
            for dirpath, dirnames, filenames in os.walk(datadir):
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    80
                rel_dirpath = dirpath[len(datadir)+1:]
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    81
                resources.update(osp.join(rel_dirpath, f) for f in filenames)
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    82
        # locate resources and copy them to destination
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    83
        for resource in resources:
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    84
            dirname = osp.dirname(resource)
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    85
            dest_resource = osp.join(dest, dirname)
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    86
            if not osp.isdir(dest_resource):
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    87
                os.makedirs(dest_resource)
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    88
            resource_dir, resource_path = config.locate_resource(resource)
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    89
            copy(osp.join(resource_dir, resource_path), dest_resource)
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    90
        print ('You can use apache rewrite rule below :\n'
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    91
               'RewriteRule ^/data/(.*) %s/$1 [L]' % dest)
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    92
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    93
    def _datadirs(self, config):
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    94
        repo = config.repository()
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    95
        if config._cubes is None:
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    96
            # web only config
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    97
            config.init_cubes(repo.get_cubes())
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    98
        for cube in repo.get_cubes():
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
    99
            cube_datadir = osp.join(cwcfg.cube_dir(cube), 'data')
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
   100
            if osp.isdir(cube_datadir):
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
   101
                yield cube_datadir
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
   102
        yield osp.join(config.shared_dir(), 'data')
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
   103
4653a2a5110b add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
Florent Cayré <florent.cayre@gmail.com>
parents: 7140
diff changeset
   104
CWCTL.register(GenStaticDataDir)