python - IPython scripting - Exit script with status code -
i trying use ipython script git pre-commit hooks since has nice syntax run shell commands , converting stdout result list of strings (which makes easy processing).
i need return status code != 0 ipython script git pre-commit hook abort commit.
consider example script
#! /bin/ipython import sys sys.exit(1)
running shell
$ ipython test.ipy
but checking status code $ echo $?
returns 0
is there way make ipython return non-zero status code?
open ipython , try running code interactively.
in [1]: import sys in [2]: sys.exit(1) exception has occurred, use %tb see full traceback. systemexit: 1 exit: use 'exit', 'quit', or ctrl-d.
so, ipython catching systemexit exception. suggest using different interpreter particular job, nice ipython is. alternatively can use:
import os os._exit(1)
however, skips sorts of important cleanup code (e.g. finally
blocks) , bad idea.
edit:
this seems work. after writing can hear alarm bells in distance , there red flags waving. not sure that's about. anyway, create new script /usr/bin/ipysh
with:
#!/usr/bin/python import sys ipython.core import interactiveshell shell = interactiveshell.interactiveshell() shell.safe_execfile(sys.argv[1], {}, raise_exceptions=true, exit_ignore=false)
make executable, set hook's hashbang #!/usr/bin/ipysh
.
Comments
Post a Comment