Skip to content

Commit af5a169

Browse files
author
Wu Jianxiao
committed
Add command line interface with crash function
1 parent 47c3958 commit af5a169

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

pydra/scripts/cli.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from pathlib import Path
2+
import pdb
3+
import sys
4+
5+
from IPython.core import ultratb
6+
import click
7+
import cloudpickle as cp
8+
9+
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
10+
ExistingFilePath = click.Path(exists=True, dir_okay=False, resolve_path=True)
11+
12+
13+
@click.group(context_settings=CONTEXT_SETTINGS)
14+
def cli():
15+
pass
16+
17+
18+
@cli.command(context_settings=CONTEXT_SETTINGS)
19+
@click.argument("crashfile", type=ExistingFilePath)
20+
@click.option(
21+
"-r", "--rerun", is_flag=True, flag_value=True, help="Rerun crashed code."
22+
)
23+
@click.option(
24+
"-d",
25+
"--debugger",
26+
type=click.Choice([None, "ipython", "pdb"]),
27+
help="Debugger to use when rerunning",
28+
)
29+
def crash(crashfile, rerun, debugger=None):
30+
"""Display a crash file and rerun if required."""
31+
if crashfile.endswith(("pkl", "pklz")):
32+
with open(crashfile, "rb") as f:
33+
crash_content = cp.load(f)
34+
print("".join(crash_content["error message"]))
35+
36+
if rerun:
37+
jobfile = Path(crashfile).parent / "_job.pklz"
38+
if jobfile.exists():
39+
with open(jobfile, "rb") as f:
40+
job_obj = cp.load(f)
41+
42+
if debugger == "ipython":
43+
sys.excepthook = ultratb.FormattedTB(
44+
mode="Verbose", theme_name="Linux", call_pdb=True
45+
)
46+
47+
try:
48+
job_obj.run(rerun=True)
49+
except Exception: # noqa: E722
50+
if debugger == "pdb":
51+
pdb.post_mortem()
52+
elif debugger == "ipython":
53+
raise
54+
else:
55+
raise FileNotFoundError(f"Job file {jobfile} not found")
56+
else:
57+
raise ValueError("Only pickled crashfiles are supported")

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies = [
1414
"filelock >=3.0.0",
1515
"fileformats >=0.15.0a7",
1616
"platformdirs >=2",
17+
"ipython",
1718
]
1819
license = { file = "LICENSE" }
1920
authors = [{ name = "Nipype developers", email = "[email protected]" }]
@@ -43,7 +44,6 @@ doc = [
4344
"fileformats-medimage >= v0.10.0a2",
4445
"fileformats-medimage-extras >= v0.10.0a2",
4546
"furo>=2022.2.14.1",
46-
"ipython",
4747
"ipykernel",
4848
"ipywidgets",
4949
"matplotlib",
@@ -114,6 +114,9 @@ documentation = "https://nipype.github.io/pydra/"
114114
homepage = "https://nipype.github.io/pydra/"
115115
repository = "https://github.com/nipype/pydra.git"
116116

117+
[project.scripts]
118+
pydracli = "pydra.scripts.cli:cli"
119+
117120
[tool.hatch.build]
118121
packages = ["pydra"]
119122
exclude = ["tests"]

0 commit comments

Comments
 (0)