-
Notifications
You must be signed in to change notification settings - Fork 60
Cli #831
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Cli #831
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
af5a169
Add command line interface with crash function
3358a40
Add pydracli crash section in tutorial
75b0e33
Only require user to have Ipython installed for using the ipython deb…
c3b7832
Add test for pydracli crash
93b3310
Make sure to generate error file in test_crash
9327cbf
Add command line interface with crash function
cee8682
Add pydracli crash section in tutorial
e0af48f
Only require user to have Ipython installed for using the ipython deb…
f8988b7
Add test for pydracli crash
72fb240
Make sure to generate error file in test_crash
5d2117e
Make sure to generate error file in docs
e6bb7fa
Merge branch 'cli' of github.com:jadecci/pydra into cli
864a386
Use bash cell for cli command in docs
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from pathlib import Path | ||
import pdb | ||
import sys | ||
|
||
import click | ||
import cloudpickle as cp | ||
|
||
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"]) | ||
ExistingFilePath = click.Path(exists=True, dir_okay=False, resolve_path=True) | ||
|
||
|
||
@click.group(context_settings=CONTEXT_SETTINGS) | ||
def cli(): | ||
pass | ||
|
||
|
||
@cli.command(context_settings=CONTEXT_SETTINGS) | ||
@click.argument("crashfile", type=ExistingFilePath) | ||
@click.option( | ||
"-r", "--rerun", is_flag=True, flag_value=True, help="Rerun crashed code." | ||
) | ||
@click.option( | ||
"-d", | ||
"--debugger", | ||
type=click.Choice([None, "ipython", "pdb"]), | ||
help="Debugger to use when rerunning", | ||
) | ||
def crash(crashfile, rerun, debugger=None): | ||
"""Display a crash file and rerun if required.""" | ||
if crashfile.endswith(("pkl", "pklz")): | ||
with open(crashfile, "rb") as f: | ||
crash_content = cp.load(f) | ||
print("".join(crash_content["error message"])) | ||
|
||
if rerun: | ||
jobfile = Path(crashfile).parent / "_job.pklz" | ||
if jobfile.exists(): | ||
with open(jobfile, "rb") as f: | ||
job_obj = cp.load(f) | ||
|
||
if debugger == "ipython": | ||
try: | ||
from IPython.core import ultratb | ||
|
||
sys.excepthook = ultratb.FormattedTB( | ||
mode="Verbose", theme_name="Linux", call_pdb=True | ||
) | ||
except ImportError: | ||
raise ImportError( | ||
"'Ipython' needs to be installed to use the 'ipython' debugger" | ||
) | ||
|
||
try: | ||
job_obj.run(rerun=True) | ||
except Exception: # noqa: E722 | ||
if debugger == "pdb": | ||
pdb.post_mortem() | ||
elif debugger == "ipython": | ||
raise | ||
else: | ||
raise FileNotFoundError(f"Job file {jobfile} not found") | ||
else: | ||
raise ValueError("Only pickled crashfiles are supported") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import pytest | ||
from pydra.scripts.cli import crash | ||
from pydra.tasks.testing import Divide | ||
from traceback import format_exception | ||
import typing as ty | ||
|
||
|
||
# @pytest.mark.xfail(reason="Need to fix a couple of things after syntax changes") | ||
def test_crash_cli(cli_runner, tmp_path): | ||
divide = Divide(x=15, y=0) | ||
with pytest.raises(ZeroDivisionError): | ||
divide(cache_root=tmp_path) | ||
|
||
result = cli_runner( | ||
crash, | ||
[ | ||
f"{tmp_path}/{divide._checksum}/_error.pklz", | ||
"--rerun", | ||
"--debugger", | ||
"pdb", | ||
], | ||
) | ||
assert result.exit_code == 0, show_cli_trace(result) | ||
|
||
|
||
def show_cli_trace(result: ty.Any) -> str: | ||
"Used in testing to show traceback of CLI output" | ||
return "".join(format_exception(*result.exc_info)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.