#509981: cw-ctl shell <instance> file.txt runs doctest.testfile
authorNicolas Chauvat <nicolas.chauvat@logilab.fr>
Sun, 08 Nov 2009 21:53:18 +0100
changeset 3810 5b75fd66c80e
parent 3777 3ef8cdb5fb1c
child 3890 d7a270f50f54
#509981: cw-ctl shell <instance> file.txt runs doctest.testfile
common/migration.py
--- a/common/migration.py	Wed Oct 21 17:32:20 2009 +0200
+++ b/common/migration.py	Sun Nov 08 21:53:18 2009 +0100
@@ -268,9 +268,16 @@
         in interactive mode,  display the migration script path, ask for
         confirmation and execute it if confirmed
         """
-        assert migrscript.endswith('.py'), migrscript
-        if self.execscript_confirm(migrscript):
-            scriptlocals = self._create_context().copy()
+        if migrscript.endswith('.py'):
+            script_mode = 'python'
+        elif migrscript.endswith('.txt') or migrscript.endswith('.rst'):
+            script_mode = 'doctest'
+        else:
+            raise Exception('This is not a valid cubicweb shell input')
+        if not self.execscript_confirm(migrscript):
+            return
+        scriptlocals = self._create_context().copy()
+        if script_mode == 'python':
             if funcname is None:
                 pyname = '__main__'
             else:
@@ -286,6 +293,10 @@
                     self.critical('no %s in script %s', funcname, migrscript)
                     return None
                 return func(*args, **kwargs)
+        else: # script_mode == 'doctest'
+            import doctest
+            doctest.testfile(os.path.abspath(migrscript), module_relative=False,
+                             optionflags=doctest.ELLIPSIS, globs=scriptlocals)
 
     def cmd_option_renamed(self, oldname, newname):
         """a configuration option has been renamed"""