Add a ``schema`` command to cmd ctrl to generate schema image.
authorPierre-Yves David <pierre-yves.david@logilab.fr>
Tue, 02 Feb 2010 18:22:25 +0100
changeset 4428 03614b377ecd
parent 4427 410c99a917fa
child 4429 0cd207567f37
Add a ``schema`` command to cmd ctrl to generate schema image. This changeset add the new commande and do some refactoring in cwconfig and schema.py to allow the use of CubicWebNoAppConfiguration with CubicWebSchemaLoader.
devtools/devctl.py
--- a/devtools/devctl.py	Tue Feb 02 16:19:48 2010 +0100
+++ b/devtools/devctl.py	Tue Feb 02 18:22:25 2010 +0100
@@ -14,12 +14,16 @@
 from os.path import join, exists, abspath, basename, normpath, split, isdir
 from copy import deepcopy
 from warnings import warn
+from tempfile import NamedTemporaryFile
+from subprocess import Popen
 
 from logilab.common import STD_BLACKLIST
 from logilab.common.modutils import get_module_files
 from logilab.common.textutils import splitstrip
 from logilab.common.shellutils import ASK
-from logilab.common.clcommands import register_commands
+from logilab.common.clcommands import register_commands, pop_arg
+
+from yams import schema2dot
 
 from cubicweb.__pkginfo__ import version as cubicwebversion
 from cubicweb import CW_SOFTWARE_ROOT as BASEDIR, BadCommandUsage
@@ -35,15 +39,10 @@
     cubicweb_appobject_path = ServerConfiguration.cubicweb_appobject_path | WebConfiguration.cubicweb_appobject_path
     cube_appobject_path = ServerConfiguration.cube_appobject_path | WebConfiguration.cube_appobject_path
 
-    def __init__(self, cube):
-        super(DevCubeConfiguration, self).__init__(cube)
-        if cube is None:
-            self._cubes = ()
-        else:
-            self._cubes = self.reorder_cubes(self.expand_cubes(self.my_cubes(cube)))
-
-    def my_cubes(self, cube):
-        return (cube,) + self.cube_dependencies(cube) + self.cube_recommends(cube)
+    def __init__(self, *cubes):
+        super(DevCubeConfiguration, self).__init__(cubes[0])
+        self._cubes = self.reorder_cubes(self.expand_cubes(cubes,
+                                         with_recommends=True))
 
     @property
     def apphome(self):
@@ -61,9 +60,6 @@
     to filter out message ids from cubicweb and dependencies of a cube
     """
 
-    def my_cubes(self, cube):
-        return self.cube_dependencies(cube) + self.cube_recommends(cube)
-
     def default_log_file(self):
         return None
 
@@ -624,9 +620,42 @@
         for clocktime, cputime, occ, rql in stat:
             print '%.2f;%.2f;%.2f;%s;%s' % (clocktime/total_time, clocktime, cputime, occ, rql)
 
+class GenerateSchema(Command):
+    """Generate schema image for the given cube"""
+    name = "schema"
+    arguments = '<cube>'
+    options = [('output-file', {'type':'file', 'default': None,
+                 'metavar': '<file>', 'short':'o', 'help':'output image file',
+                 'input':False}),
+               ('viewer', {'type': 'string', 'default':None,
+                'short': "w", 'metavar':'<cmd>',
+                 'help':'command use to view the generated file (empty for none)'}
+               ),
+              ]
+
+    def run(self, args):
+        from logilab.common.textutils import splitstrip
+        cubes = splitstrip(pop_arg(args, 1))
+
+        dev_conf = DevCubeConfiguration(*cubes)
+        schema = dev_conf.load_schema()
+
+
+        out, viewer = self['output-file'], self['viewer']
+        if out is None:
+            tmp_file = NamedTemporaryFile(suffix=".svg")
+            out = tmp_file.name
+        schema2dot.schema2dot(schema, out,
+            #skiptypes=("identity",)
+            )
+        if viewer:
+            p = Popen((viewer, out))
+            p.wait()
+
 register_commands((UpdateCubicWebCatalogCommand,
                    UpdateTemplateCatalogCommand,
                    LiveServerCommand,
                    NewCubeCommand,
                    ExamineLogCommand,
+                   GenerateSchema,
                    ))