diff --git a/doc/requirements.txt b/doc/requirements.txt index 5722af62..0b15d3aa 100644 --- a/doc/requirements.txt +++ b/doc/requirements.txt @@ -1,6 +1,6 @@ pygmsh seaborn -sphinx == 5.3 +sphinx sphinx-argparse sphinx-gallery pydata-sphinx-theme diff --git a/doc/source/conf.py b/doc/source/conf.py index 86e617ba..c63e2143 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -17,7 +17,7 @@ import toughio project = "toughio" -copyright = "2022, Keurfon Luu" +copyright = "2023, Keurfon Luu" author = "Keurfon Luu" version = toughio.__version__ diff --git a/tests/test_run.py b/tests/test_run.py new file mode 100644 index 00000000..bc47df4d --- /dev/null +++ b/tests/test_run.py @@ -0,0 +1,87 @@ +import os +import platform +import sys + +import pytest + +import toughio + + +@pytest.mark.parametrize( + "exec, workers, docker, wsl, cmd", + [ + ("tough-exec", None, None, False, "tough-exec INFILE INFILE.out"), + ("tough-exec", 8, None, False, "mpiexec -n 8 tough-exec INFILE INFILE.out"), + ], +) +def test_run(exec, workers, docker, wsl, cmd): + status = toughio.run( + exec, + {}, + workers=workers, + docker=docker, + wsl=wsl, + use_temp=sys.version_info + >= (3, 8), # dirs_exist_ok only works with Python > 3.8 + silent=True, + ) + + assert status.args == cmd + + +@pytest.mark.skipif( + not platform.system().startswith("Win"), reason="requires Windows platform" +) +@pytest.mark.parametrize( + "exec, workers, docker, wsl, cmd", + [ + ( + "tough-exec", + None, + "docker-image", + False, + "docker run --rm -v PLACEHOLDER:/shared -w /shared docker-image tough-exec INFILE INFILE.out", + ), + ("tough-exec", None, None, True, "bash -c 'tough-exec INFILE INFILE.out'"), + ( + "tough-exec", + 8, + "docker-image", + True, + """bash -c 'docker run --rm -v PLACEHOLDER:/shared -w /shared docker-image mpiexec -n 8 tough-exec INFILE INFILE.out'""", + ), + ], +) +def test_run_windows(exec, workers, docker, wsl, cmd): + if os.getenv("ComSpec").endswith("cmd.exe"): + cmd = cmd.replace("PLACEHOLDER", '"%cd%"') + + else: + cmd = cmd.replace("PLACEHOLDER", "${PWD}") + + test_run(exec, workers, docker, wsl, cmd) + + +@pytest.mark.skipif( + platform.system().startswith("Win"), reason="requires Unix platform" +) +@pytest.mark.parametrize( + "exec, workers, docker, cmd", + [ + ( + "tough-exec", + None, + "docker-image", + "docker run --rm PLACEHOLDER -v ${PWD}:/shared -w /shared docker-image tough-exec INFILE INFILE.out", + ), + ( + "tough-exec", + 8, + "docker-image", + "docker run --rm PLACEHOLDER -v ${PWD}:/shared -w /shared docker-image mpiexec -n 8 tough-exec INFILE INFILE.out", + ), + ], +) +def test_run_unix(exec, workers, docker, cmd): + cmd = cmd.replace("PLACEHOLDER", f"-e LOCAL_USER_ID={os.getuid()}") + test_run(exec, workers, docker, False, cmd) diff --git a/toughio/VERSION b/toughio/VERSION index f88cf52e..da38e07b 100644 --- a/toughio/VERSION +++ b/toughio/VERSION @@ -1 +1 @@ -1.13.0 \ No newline at end of file +1.13.1 \ No newline at end of file diff --git a/toughio/_io/output/tough/_tough.py b/toughio/_io/output/tough/_tough.py index e25a2836..939ece99 100644 --- a/toughio/_io/output/tough/_tough.py +++ b/toughio/_io/output/tough/_tough.py @@ -126,15 +126,7 @@ def _read_table(f, file_type): if line[:nwsp].strip() and not line.strip().startswith("ELEM"): if first: # Find first floating point - for xf in line.split()[::-1]: - try: - _ = int(xf) - - except ValueError: - x = xf - continue - - break + x = line.split()[-headers[::-1].index("INDEX")] # Find end of label(s) tmp = line[: line.index(x)].rstrip() diff --git a/toughio/_run.py b/toughio/_run.py index 0fc8a01b..cc90fe0e 100644 --- a/toughio/_run.py +++ b/toughio/_run.py @@ -152,20 +152,26 @@ def run( cmd = f"mpiexec -n {workers} {cmd}" # Use Docker + is_windows = platform.system().startswith("Win") + if docker: - if platform.system().startswith("Win") and os.getenv("ComSpec").endswith( - "cmd.exe" - ): - cwd = "%cd%" + if is_windows and os.getenv("ComSpec").endswith("cmd.exe"): + cwd = '"%cd%"' else: cwd = "${PWD}" - cmd = f"docker run --rm -v {cwd}:/work -w /work {docker} {cmd}" + try: + uid = f"-e LOCAL_USER_ID={os.getuid()}" + + except AttributeError: + uid = "" + + cmd = f"docker run --rm {uid} -v {cwd}:/shared -w /shared {docker} {cmd}" # Use WSL - if wsl and platform.system().startswith("Win"): - cmd = f'bash -c "{cmd}"' + if wsl and is_windows: + cmd = f"bash -c '{cmd}'" kwargs = {} if silent: @@ -184,7 +190,7 @@ def run( simulation_dir, working_dir, ignore=shutil.ignore_patterns(*ignore_patterns), - dirs_exist_ok=True, + dirs_exist_ok=True, # Doesn't work with Python 3.7 ) shutil.rmtree(simulation_dir, ignore_errors=True) os.remove(working_dir / "tempdir.txt")