Skip to content

Commit

Permalink
handle datasets not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
lobis committed Dec 2, 2023
1 parent 51ca6c6 commit 3ed3c49
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .binder/postBuild
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

mkdir -p /home/${USER}/examples
echo "cd /home/${USER}/examples" > /home/${USER}/.bashrc

python -c "import geant4_python_application; geant4_python_application.install_datasets()"
2 changes: 1 addition & 1 deletion .github/workflows/wheels.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
- name: Build the wheel
run: |
export CMAKE_PREFIX_PATH=${CONDA_PREFIX}
mamba install build
python -m pip install build
python -m build --wheel .
- uses: actions/upload-artifact@v3
Expand Down
36 changes: 35 additions & 1 deletion src/geant4_python_application/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,41 @@ def install_datasets(always: bool = False) -> None:
subprocess.run([geant4_config, "--install-datasets"], check=True)


_minimum_geant4_version = "11.1.0"


def _parse_geant4_version(version: str) -> (int, int, int):
return tuple([int(part) for part in version.split(".")])


try:
available_geant4_version_parsed = _parse_geant4_version(_geant4_version)
minimum_geant4_version_parsed = _parse_geant4_version(_minimum_geant4_version)
if available_geant4_version_parsed < minimum_geant4_version_parsed:
warnings.warn(
f"Geant4 version {_geant4_version} is lower than the minimum required version {_minimum_geant4_version}."
)
except Exception as e:
raise RuntimeError(
f"Error comparing Geant4 version '{_geant4_version}' with '{_minimum_geant4_version}': {e}"
)

load_libs()

# environment variable GEANT4_DATA_DIR is a recent addition to Geant4
if "GEANT4_DATA_DIR" not in os.environ:
os.environ["GEANT4_DATA_DIR"] = os.path.join(
_geant4_prefix, "share", "Geant4", "data"
)

_geant4_data_dir = os.environ["GEANT4_DATA_DIR"]
if not os.path.exists(_geant4_data_dir) or not os.listdir(_geant4_data_dir):
warnings.warn(
f"""Geant4 data directory {_geant4_data_dir} does not exist or is empty. Please set the
GEANT4_DATA_DIR environment variable to the correct directory or install the datasets via `geant4-config
--install-datasets` or calling `geant4_python_application.install_datasets()`."""
)

from geant4_python_application.gdml import basic_gdml
from geant4_python_application.geant4_application import (
Application,
Expand All @@ -89,7 +122,8 @@ def install_datasets(always: bool = False) -> None:

if _geant4_version != __geant4_version__:
warnings.warn(
f"Geant4 version mismatch. Python module was compiled with Geant4 version {__geant4_version__}, but the available Geant4 version is {geant4_version}."
f"""Geant4 version mismatch. Python module was compiled with Geant4 version {__geant4_version__}
, but the available Geant4 version is {_geant4_version}."""
)

__all__ = [
Expand Down

0 comments on commit 3ed3c49

Please sign in to comment.