From f942e555a8da1c949b64218ee563310e5c248efe Mon Sep 17 00:00:00 2001
From: Chris Maffeo <cmaffeo2@illinois.edu>
Date: Thu, 27 Sep 2018 15:48:42 -0500
Subject: [PATCH] Added --debug option to mrdna script

---
 bin/mrdna | 45 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 43 insertions(+), 2 deletions(-)

diff --git a/bin/mrdna b/bin/mrdna
index b16ac40..8c1fc07 100755
--- a/bin/mrdna
+++ b/bin/mrdna
@@ -23,6 +23,8 @@ parser.add_argument('--fine-steps', type=float, default=1e7,
                     help='Simulation steps for fine model (50 fs/step)')
 parser.add_argument('--backbone-scale', type=float, default=1.0,
                     help='Factor to scale DNA backbone in atomic model; try 0.25 to avoid clashes for atomistic simulations')
+parser.add_argument('--debug', action='store_true',
+                    help='Run through the python debugger?')
 
 parser.add_argument('--draw-cylinders', action='store_true',
                     help='Whether or not to draw the cylinders')
@@ -37,8 +39,7 @@ parser.add_argument('input_file', type=str,
 
 args = parser.parse_args()
 
-if __name__ == '__main__':
-    
+def main():
     infile = pathlib.Path(args.input_file)
     try:
         prefix = infile.stem
@@ -100,3 +101,43 @@ if __name__ == '__main__':
 
 
         simulate( **run_args )
+
+if __name__ == '__main__':
+    if args.debug:
+
+        class Restart(Exception):
+            """Causes a debugger to be restarted for the debugged python program."""
+            pass
+
+        import traceback
+        import sys
+        from pdb import Pdb
+        pdb = Pdb()
+
+        while True:
+            try:
+                # pdb._runscript(mainpyfile)
+                pdb.runcall(main)
+                if pdb._user_requested_quit:
+                    break
+                print("The program finished and will be restarted")
+            except Restart:
+                print("Restarting...")
+                print("\t" + " ".join(args))
+            except SystemExit:
+                # In most cases SystemExit does not warrant a post-mortem session.
+                print("The program exited via sys.exit(). Exit status:", end=' ')
+                print(sys.exc_info()[1])
+            except SyntaxError:
+                traceback.print_exc()
+                sys.exit(1)
+            except:
+                traceback.print_exc()
+                print("Uncaught exception. Entering post mortem debugging")
+                print("Running 'cont' or 'step' will restart the program")
+                t = sys.exc_info()[2]
+                pdb.interaction(None, t)
+                print("Post mortem debugger finished. mrdna will be restarted")
+
+    else:
+        main()
-- 
GitLab