Skip to content

Commit

Permalink
Merge branch 'rm-snakemake' into 'master'
Browse files Browse the repository at this point in the history
Refactored Snakemake tests into pytest

See merge request ogs/ogs!5058
  • Loading branch information
bilke committed Aug 14, 2024
2 parents 9dd681a + 4e9d192 commit 06368bc
Show file tree
Hide file tree
Showing 78 changed files with 571 additions and 668 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ repos:
- id: vale
args: [--output=line, --minAlertLevel=error]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.4.2"
rev: "v0.5.0"
hooks:
- id: ruff
types_or: [python, pyi, jupyter]
Expand All @@ -90,7 +90,7 @@ repos:
# Run this hook (and all other manual hooks if any) with:
# pre-commit run --hook-stage manual
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.4.2"
rev: "v0.5.0"
hooks:
- id: ruff
types_or: [python, pyi, jupyter]
Expand Down
47 changes: 47 additions & 0 deletions Applications/Python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,58 @@
# wheel: Install into Python module root dir (enables 'import ogs.simulator')
set(_py_install_location ogs)
set(_py_build_location ogs)
if(NOT OGS_BUILD_WHEEL)
set(_py_install_location
"${CMAKE_INSTALL_LIBDIR}/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages/ogs"
)
set(_py_build_location "${PROJECT_BINARY_DIR}/site-packages/ogs")
endif()

add_subdirectory(ogs)
add_subdirectory(ogs.simulator)
add_subdirectory(ogs.mesh)
add_subdirectory(ogs.callbacks)

if(OGS_USE_PIP)
set_target_properties(
simulator mesh callbacks PROPERTIES LIBRARY_OUTPUT_DIRECTORY
${_py_build_location}
)
file(
COPY ogs/.
DESTINATION ${_py_build_location}
PATTERN "__pycache__" EXCLUDE
PATTERN "CMakeLists.txt" EXCLUDE
)
# Fails with libGitInfoLib.so: undefined symbol: __asan_report_load8
if(NOT ENABLE_ASAN)
add_test(
NAME pytest
COMMAND
${CMAKE_COMMAND} -DEXECUTABLE=pytest
"-DEXECUTABLE_ARGS=-c;${PROJECT_SOURCE_DIR}/pyproject.toml" # Quoted
# because
# passed as list see https://stackoverflow.com/a/33248574/80480
-DBINARY_PATH=${_binary_path}
-DWORKING_DIRECTORY=${PROJECT_SOURCE_DIR}
"-DLOG_ROOT=${PROJECT_BINARY_DIR}/logs"
"-DLOG_FILE_BASENAME=pytest.txt"
"-DTEST_COMMAND_IS_EXPECTED_TO_SUCCEED=TRUE" -P
${PROJECT_SOURCE_DIR}/scripts/cmake/test/AddTestWrapper.cmake
)
set_tests_properties(
pytest PROPERTIES LABELS "default;python" COST 10
)

add_dependencies(
ctest
GMSH2OGS
Layers2Grid
AddFaultToVoxelGrid
ExtractBoundary
vtkdiff
generateStructuredMesh
)
endif()

endif()
15 changes: 15 additions & 0 deletions Applications/Python/ogs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# cmake-lint: disable=E1126
set(_config_content
"OGS_USE_PETSC = '@OGS_USE_PETSC@'" "OGS_VERSION = '@OGS_VERSION@'"
"OGS_USE_MKL = '@OGS_USE_MKL@'"
)
string(REPLACE ";" "\n" _config_content "${_config_content}")
set(_config_file ${PROJECT_BINARY_DIR}/site-packages/ogs/config.py)
file(CONFIGURE OUTPUT ${_config_file} CONTENT "${_config_content}")

if(OGS_BUILD_WHEEL)
install(FILES ${_config_file} DESTINATION ${SKBUILD_PLATLIB_DIR}/ogs)
else()
install(FILES ${_config_file} DESTINATION ${_py_install_location})
endif()

if(OGS_BUILD_WHEEL)
return()
endif()
Expand Down
12 changes: 12 additions & 0 deletions Applications/Python/ogs/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
import importlib.util
import os

from ._internal.wrap_cli_tools import cli # noqa: F401

if "PEP517_BUILD_BACKEND" in os.environ:
# on wheel build config.py is not generated at the beginning of the build
# but later after CMake has run
config_spec = importlib.util.find_spec(".config", package="ogs")
if config_spec is not None:
from .config import * # noqa: F403
else:
from .config import * # noqa: F403
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

from . import OGS_USE_PATH

# Here, we assume that this script is installed, e.g., in a virtual environment
# alongside a "bin" directory.
OGS_BIN_DIR = Path(__file__).parent.parent.parent / "bin"

binaries_list = [
"addDataToRaster",
"AddElementQuality",
Expand Down Expand Up @@ -112,14 +108,20 @@ def ogs_with_args(argv):


if "PEP517_BUILD_BACKEND" not in os.environ:
# Here, we assume that this script is installed, e.g., in a virtual environment
# alongside a "bin" directory.
OGS_BIN_DIR = Path(__file__).parent.parent.parent / "bin" # installed wheel
if not OGS_BIN_DIR.exists():
OGS_BIN_DIR = OGS_BIN_DIR.parent # build directory

if platform.system() == "Windows":
os.add_dll_directory(OGS_BIN_DIR)

def _program(name, args):
exe = OGS_BIN_DIR / name
if OGS_USE_PATH:
exe = name
print(f"OGS_USE_PATH is true: {name} from $PATH is used!")
print(f"OGS_USE_PATH is true: {name} from $PATH is used!")
return subprocess.run([exe] + args).returncode # noqa: PLW1510

FUNC_TEMPLATE = """def {0}(): raise SystemExit(_program("{0}", sys.argv[1:]))"""
Expand Down
24 changes: 20 additions & 4 deletions Applications/Python/ogs/_internal/wrap_cli_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

# Here, we assume that this script is installed, e.g., in a virtual environment
# alongside a "bin" directory.
OGS_BIN_DIR = Path(__file__).parent.parent.parent / "bin"
OGS_BIN_DIR = Path(__file__).parent.parent.parent / "bin" # installed wheel
if not OGS_BIN_DIR.exists():
OGS_BIN_DIR = OGS_BIN_DIR.parent # build directory


class CLI:
Expand All @@ -32,7 +34,7 @@ def ogs(self, *args, **kwargs):
>>> cli = CLI()
>>> cli.ogs("--help") # prints a help text
...
>>> cli.ogs(help=None) # special, does the same
>>> cli.ogs(help=True) # flags without any additional value can be set to True
...
A more useful example. The following will create a line mesh:
Expand All @@ -52,12 +54,25 @@ def ogs(self, *args, **kwargs):
@staticmethod
def _format_kv(kwargs):
for key, v in kwargs.items():
# Convert None to True
if v is None:
# TODO: Remove after 2025/08
print(
f"Deprecation warning: Setting {v} to `None` is deprecated, set to `True` instead!"
)
v = True # noqa: PLW2901

# If value is False then ignore
if isinstance(v, bool) and not v:
continue

if len(key) == 1:
yield f"-{key}"
else:
yield f"--{key}"

if v is not None:
# Pass value if not bool
if not isinstance(v, bool):
yield f"{v}"

@staticmethod
Expand All @@ -74,6 +89,7 @@ def run_cmd(*args, **kwargs):
cmdline = CLI._get_cmdline(cmd, *args, **kwargs)
return subprocess.call(cmdline)

# TODO: Only arguments with underscores work. Arguments with dashes not.
run_cmd.__doc__ = f"""
This function wraps the commandline tool {attr} for easy use from Python.
Expand All @@ -89,7 +105,7 @@ def run_cmd(*args, **kwargs):
>>> cli = CLI()
>>> cli.{attr}("--help") # prints a help text
...
>>> cli.{attr}(help=None) # special, does the same
>>> cli.ogs(help=True) # flags without any additional value can be set to True
...
A more useful example. The following will create a line mesh:
Expand Down
44 changes: 0 additions & 44 deletions Applications/Utils/ExtractBoundary.smk

This file was deleted.

86 changes: 0 additions & 86 deletions Applications/Utils/GMSH2OGS_ExtractBoundary.smk

This file was deleted.

Loading

0 comments on commit 06368bc

Please sign in to comment.