|
1 | 1 | #!/usr/bin/env python
|
2 | 2 | """Displays crash information from Nipype crash files. For certain crash files,
|
3 | 3 | one can rerun a failed node in a temp directory.
|
4 |
| -""" |
5 | 4 |
|
6 |
| -import argparse |
7 |
| -from nipype.utils.filemanip import loadcrash |
| 5 | +Examples: |
| 6 | +
|
| 7 | +nipype_display_crash crashfile.pklz |
| 8 | +nipype_display_crash crashfile.pklz -r -i |
| 9 | +nipype_display_crash crashfile.pklz -r -i |
| 10 | +
|
| 11 | +""" |
8 | 12 |
|
9 |
| -def display_crash_files(crashfile, rerun): |
| 13 | +def display_crash_files(crashfile, rerun, debug, directory): |
10 | 14 | """display crash file content and rerun if required"""
|
11 | 15 |
|
| 16 | + from nipype.utils.filemanip import loadcrash |
12 | 17 | crash_data = loadcrash(crashfile)
|
13 | 18 | node = None
|
14 | 19 | if 'node' in crash_data:
|
15 | 20 | node = crash_data['node']
|
16 | 21 | tb = crash_data['traceback']
|
17 |
| - print "\n" |
18 |
| - print "File: %s"%crashfile |
| 22 | + print("\n") |
| 23 | + print("File: %s" % crashfile) |
19 | 24 | if node:
|
20 |
| - print "Node: %s"%node |
| 25 | + print("Node: %s" % node) |
21 | 26 | if node.base_dir:
|
22 |
| - print "Working directory: %s"%node.output_dir() |
| 27 | + print("Working directory: %s" % node.output_dir()) |
23 | 28 | else:
|
24 |
| - print "Node crashed before execution" |
25 |
| - print "\n" |
26 |
| - print "Node inputs:" |
27 |
| - print node.inputs |
28 |
| - print "\n" |
29 |
| - print "Traceback: " |
30 |
| - print ''.join(tb) |
31 |
| - print "\n" |
| 29 | + print("Node crashed before execution") |
| 30 | + print("\n") |
| 31 | + print("Node inputs:") |
| 32 | + print(node.inputs) |
| 33 | + print("\n") |
| 34 | + print("Traceback: ") |
| 35 | + print(''.join(tb)) |
| 36 | + print ("\n") |
32 | 37 |
|
33 | 38 | if rerun:
|
34 | 39 | if node is None:
|
35 |
| - print "No node in crashfile. Cannot rerun" |
| 40 | + print("No node in crashfile. Cannot rerun") |
36 | 41 | return
|
37 |
| - print "Rerunning node" |
38 |
| - node.base_dir = None |
| 42 | + print("Rerunning node") |
| 43 | + node.base_dir = directory |
39 | 44 | node.config = {'execution': {'crashdump_dir': '/tmp'}}
|
40 |
| - node.run() |
41 |
| - print "\n" |
| 45 | + try: |
| 46 | + node.run() |
| 47 | + except: |
| 48 | + if debug and debug != 'ipython': |
| 49 | + import pdb |
| 50 | + pdb.post_mortem() |
| 51 | + else: |
| 52 | + raise |
| 53 | + print("\n") |
42 | 54 |
|
43 | 55 | if __name__ == "__main__":
|
44 |
| - parser = argparse.ArgumentParser(prog='nipype_display_crash', |
45 |
| - description=__doc__) |
| 56 | + from argparse import ArgumentParser, RawTextHelpFormatter |
| 57 | + defstr = ' (default %(default)s)' |
| 58 | + parser = ArgumentParser(prog='nipype_display_crash', |
| 59 | + description=__doc__, |
| 60 | + formatter_class=RawTextHelpFormatter) |
46 | 61 | parser.add_argument('crashfile', metavar='f', type=str,
|
47 |
| - help='crash file to display') |
| 62 | + help='crash file to display') |
48 | 63 | parser.add_argument('-r','--rerun', dest='rerun',
|
49 | 64 | default=False, action="store_true",
|
50 |
| - help='rerun crashed node') |
| 65 | + help='rerun crashed node' + defstr) |
| 66 | + group = parser.add_mutually_exclusive_group() |
| 67 | + group.add_argument('-d','--debug', dest='debug', |
| 68 | + default=False, action="store_true", |
| 69 | + help='enable python debugger when re-executing' + defstr) |
| 70 | + group.add_argument('-i','--ipydebug', dest='ipydebug', |
| 71 | + default=False, action="store_true", |
| 72 | + help='enable ipython debugger when re-executing' + defstr) |
| 73 | + parser.add_argument('--dir', dest='directory', |
| 74 | + default=None, |
| 75 | + help='Directory to run the node in' + defstr) |
51 | 76 | args = parser.parse_args()
|
52 |
| - |
53 |
| - display_crash_files(args.crashfile, args.rerun) |
| 77 | + print args.debug, args.ipydebug |
| 78 | + debug = 'ipython' if args.ipydebug else args.debug |
| 79 | + if debug == 'ipython': |
| 80 | + import sys |
| 81 | + from IPython.core import ultratb |
| 82 | + sys.excepthook = ultratb.FormattedTB(mode='Verbose', |
| 83 | + color_scheme='Linux', |
| 84 | + call_pdb=1) |
| 85 | + display_crash_files(args.crashfile, args.rerun, |
| 86 | + debug, args.directory) |
0 commit comments