cubicweb/cwctl.py
changeset 12692 8673da7c2f85
parent 12685 84a8a8915512
child 12696 eb83daa69495
--- a/cubicweb/cwctl.py	Mon Jul 22 10:54:22 2019 +0200
+++ b/cubicweb/cwctl.py	Tue May 21 16:47:13 2019 +0200
@@ -97,6 +97,16 @@
     return [drop_prefix(cube) for cube in cwcfg.available_cubes()]
 
 
+def get_pdb():
+    try:
+        import ipdb
+    except ImportError:
+        import pdb
+        return pdb
+    else:
+        return ipdb
+
+
 class InstanceCommand(Command):
     """base class for command taking one instance id as arguments"""
     arguments = '<instance>'
@@ -111,6 +121,11 @@
           'help': 'force command without asking confirmation',
           }
          ),
+        ("pdb",
+         {'action': 'store_true', 'default': False,
+          'help': 'launch pdb on exception',
+          }
+         ),
     )
     actionverb = None
 
@@ -130,6 +145,7 @@
         except Exception as ex:
             import traceback
             traceback.print_exc()
+
             sys.stderr.write('instance %s not %s: %s\n' % (
                 appid, self.actionverb, ex))
             status = 8
@@ -138,6 +154,11 @@
             sys.stderr.write('%s aborted\n' % self.name)
             status = 2  # specific error code
 
+        if status != 0 and self.config.pdb:
+            exception_type, exception, traceback_ = sys.exc_info()
+            pdb = get_pdb()
+            pdb.post_mortem(traceback_)
+
         sys.exit(status)