add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
authorFlorent Cayré <florent.cayre@gmail.com>
Thu, 28 Apr 2011 17:36:04 +0200
changeset 7274 4653a2a5110b
parent 7273 a949fc438029
child 7275 bb3bb8104134
add cwctl command to generate a consolidated data directory with all cubes/ cubicweb data directories content to be delivered statically
web/webctl.py
--- a/web/webctl.py	Thu Apr 28 17:33:56 2011 +0200
+++ b/web/webctl.py	Thu Apr 28 17:36:04 2011 +0200
@@ -21,9 +21,16 @@
 
 __docformat__ = "restructuredtext en"
 
+import os, os.path as osp
+from shutil import copy
+
 from logilab.common.shellutils import ASK
 
-from cubicweb.toolsutils import CommandHandler, underline_title
+from cubicweb import ExecutionError
+from cubicweb.cwctl import CWCTL
+from cubicweb.cwconfig import CubicWebConfiguration as cwcfg
+from cubicweb.toolsutils import Command, CommandHandler, underline_title
+
 
 class WebCreateHandler(CommandHandler):
     cmdname = 'create'
@@ -43,3 +50,55 @@
 
     def postcreate(self, *args, **kwargs):
         """hooks called once instance's initialization has been completed"""
+
+
+class GenStaticDataDir(Command):
+    """Create a directory merging all data directory content from cubes and CW.
+    """
+    name = 'gen-static-datadir'
+    arguments = '<instance> [dirpath]'
+    min_args = 1
+    max_args = 2
+
+    options = ()
+
+    def run(self, args):
+        appid = args.pop(0)
+        config = cwcfg.config_for(appid)
+        if args:
+            dest = args[0]
+        else:
+            dest = osp.join(config.appdatahome, 'data')
+        if osp.exists(dest):
+            raise ExecutionError('Directory %s already exists. '
+                                 'Remove it first.' % dest)
+        config.quick_start = True # notify this is not a regular start
+        # list all resources (no matter their order)
+        resources = set()
+        for datadir in self._datadirs(config):
+            for dirpath, dirnames, filenames in os.walk(datadir):
+                rel_dirpath = dirpath[len(datadir)+1:]
+                resources.update(osp.join(rel_dirpath, f) for f in filenames)
+        # locate resources and copy them to destination
+        for resource in resources:
+            dirname = osp.dirname(resource)
+            dest_resource = osp.join(dest, dirname)
+            if not osp.isdir(dest_resource):
+                os.makedirs(dest_resource)
+            resource_dir, resource_path = config.locate_resource(resource)
+            copy(osp.join(resource_dir, resource_path), dest_resource)
+        print ('You can use apache rewrite rule below :\n'
+               'RewriteRule ^/data/(.*) %s/$1 [L]' % dest)
+
+    def _datadirs(self, config):
+        repo = config.repository()
+        if config._cubes is None:
+            # web only config
+            config.init_cubes(repo.get_cubes())
+        for cube in repo.get_cubes():
+            cube_datadir = osp.join(cwcfg.cube_dir(cube), 'data')
+            if osp.isdir(cube_datadir):
+                yield cube_datadir
+        yield osp.join(config.shared_dir(), 'data')
+
+CWCTL.register(GenStaticDataDir)