From 3b00f6f736ea9d8fcd27f0512a5cf6930f5854c4 Mon Sep 17 00:00:00 2001 From: Dustin Machi Date: Fri, 26 Jan 2024 07:43:37 -0500 Subject: [PATCH] Reworked app.run exit code (#73) * allow run to return exit_code in non-interactive mode * add tests for exit code from run. Add variable substituion test for exec * remove unneeded apptainer def * bump version * validate the substitution in test_exec.sh --- CHANGELOG.md | 1 + docs/hello-world.scif | 3 +++ scif/main/commands.py | 6 +++++- scif/tests/test_exec.sh | 8 ++++++++ scif/tests/test_run.sh | 2 ++ scif/version.py | 2 +- 6 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cab15bc..4113356 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ represented by the pull requests that fixed them. Versions in parentheses coincide with what is available on [pypi](https://pypi.org/project/scif/). ## [xxx](https://github.com/vsoch/scif/tree/master) (master) + - (re)add exit code to app.run (0.0.84) - revert exit code to app.run until further notice (0.0.83) - add exit code to app.run (0.0.82) - install return value non zero should stop build (0.0.81) diff --git a/docs/hello-world.scif b/docs/hello-world.scif index 4ff54a4..18952f7 100644 --- a/docs/hello-world.scif +++ b/docs/hello-world.scif @@ -39,3 +39,6 @@ fi echo "Argument supplied, exiting with ${1}" exit ${1} +%apprun hello-world-exit-code + echo "Exiting with code 155" + exit 155 diff --git a/scif/main/commands.py b/scif/main/commands.py index 739f725..d634896 100644 --- a/scif/main/commands.py +++ b/scif/main/commands.py @@ -89,11 +89,15 @@ def _exec(self, app=None, interactive=False, exit=False): os.system("".join(cmd)) else: - for line in os.popen(cmd): + f = os.popen(cmd) + for line in f.readlines(): try: print(line.rstrip()) except: print(line.rstrip().encode(loc)) + status = f.close() + if status: + sys.exit(os.waitstatus_to_exitcode(status)) def execute(self, app, cmd=None, args=None): diff --git a/scif/tests/test_exec.sh b/scif/tests/test_exec.sh index d29123c..f105f8a 100755 --- a/scif/tests/test_exec.sh +++ b/scif/tests/test_exec.sh @@ -6,3 +6,11 @@ echo "Testing exec of commands:" docker run -it "${CONTAINER_NAME}" exec hello-world-echo echo "Hello World!" docker run -it "${CONTAINER_NAME}" exec hello-world-script ls / docker run -it "${CONTAINER_NAME}" exec hello-world-env env + +OUT=$(docker run -it "${CONTAINER_NAME}" --quiet exec hello-world-env echo [e]OMG | tr -d '\n\t\r ') +if [ "$OUT" == "TACOS" ]; then + echo "Variable Substitution Success: $OUT" +else + echo "Variable subsitution for e[OMG] failed" + exit 1 +fi diff --git a/scif/tests/test_run.sh b/scif/tests/test_run.sh index 93e51e4..db1e539 100755 --- a/scif/tests/test_run.sh +++ b/scif/tests/test_run.sh @@ -7,3 +7,5 @@ docker run -it "${CONTAINER_NAME}" run hello-world-echo docker run -it "${CONTAINER_NAME}" run hello-world-script docker run -it "${CONTAINER_NAME}" run hello-world-custom "Meatball" docker run -it "${CONTAINER_NAME}" run hello-world-env | echo "Success" +docker run -it "${CONTAINER_NAME}" run hello-world-exit-code ; echo "Exit code: $? [should be 155]" +docker run -it "${CONTAINER_NAME}" run hello-world-echo; echo "Exit code: $? [should be 0]" diff --git a/scif/version.py b/scif/version.py index 7b5bce5..0da7eed 100644 --- a/scif/version.py +++ b/scif/version.py @@ -10,7 +10,7 @@ """ -__version__ = "0.0.83" +__version__ = "0.0.84" AUTHOR = "Vanessa Sochat" AUTHOR_EMAIL = "vsochat@stanford.edu" NAME = "scif"