Skip to content

Commit eea0b9a

Browse files
committed
add nipype_crash_search script
1 parent 4b392d5 commit eea0b9a

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

bin/nipype_crash_search.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env python
2+
"""Search for tracebacks inside a folder of nipype crash
3+
log files that match a given regular expression.
4+
5+
Examples:
6+
nipype_crash_search -d nipype/wd/log -r '.*subject123.*'
7+
"""
8+
import re
9+
import os.path as op
10+
from glob import glob
11+
12+
from traits.trait_errors import TraitError
13+
from nipype.utils.filemanip import loadcrash
14+
15+
16+
def load_pklz_traceback(crash_filepath):
17+
""" Return the traceback message in the given crash file."""
18+
try:
19+
data = loadcrash(crash_filepath)
20+
except TraitError as te:
21+
return str(te)
22+
except:
23+
raise
24+
else:
25+
return '\n'.join(data['traceback'])
26+
27+
28+
def iter_tracebacks(logdir):
29+
""" Return an iterator over each file path and
30+
traceback field inside `logdir`.
31+
Parameters
32+
----------
33+
logdir: str
34+
Path to the log folder.
35+
36+
field: str
37+
Field name to be read from the crash file.
38+
39+
Yields
40+
------
41+
path_file: str
42+
43+
traceback: str
44+
"""
45+
crash_files = sorted(glob(op.join(logdir, '*.pkl*')))
46+
47+
for cf in crash_files:
48+
yield cf, load_pklz_traceback(cf)
49+
50+
51+
def display_crash_search(logdir, regex):
52+
rex = re.compile(regex, re.IGNORECASE)
53+
for file, trace in iter_tracebacks(logdir):
54+
if rex.search(trace):
55+
print("-" * len(file))
56+
print(file)
57+
print("-" * len(file))
58+
print(trace)
59+
60+
61+
if __name__ == "__main__":
62+
from argparse import ArgumentParser, RawTextHelpFormatter
63+
defstr = ' (default %(default)s)'
64+
parser = ArgumentParser(prog='nipype_crash_search',
65+
description=__doc__,
66+
formatter_class=RawTextHelpFormatter)
67+
parser.add_argument('-l','--logdir', type=str, dest='logdir',
68+
action="store", default=None,
69+
help='The working directory log file.' + defstr)
70+
parser.add_argument('-r', '--regex', dest='regex',
71+
default='*',
72+
help='Regular expression to be searched in each traceback.' + defstr)
73+
74+
args = parser.parse_args()
75+
76+
display_crash_search(args.logdir, args.regex)

0 commit comments

Comments
 (0)