Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions pygmt/_show_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Adapted from :func:`rioxarray.show_versions` and :func:`pandas.show_versions`.
"""

import ctypes
import platform
import shutil
import subprocess
Expand Down Expand Up @@ -38,6 +39,28 @@ def _get_module_version(modname: str) -> str | None:
return None


def _get_gdal_version() -> str | None:
"""
Get GDAL version by calling the GDAL C API via ctypes.
"""
match sys.platform:
case name if name == "linux" or name.startswith("freebsd"):
libname = "libgdal.so"
case "darwin":
libname = "libgdal.dylib"
case "win32":
libname = "gdal.dll"
case _:
return None

try:
lib = ctypes.CDLL(libname)
lib.GDALVersionInfo.restype = ctypes.c_char_p
return lib.GDALVersionInfo(b"RELEASE_NAME").decode("utf-8") # e.g., 3.6.3
except (OSError, AttributeError):
return None


def _get_ghostscript_version() -> str | None:
"""
Get Ghostscript version.
Expand Down Expand Up @@ -94,6 +117,7 @@ def show_versions(file: TextIO | None = sys.stdout) -> None:
- PyGMT itself
- System information (Python version, Operating System)
- Core dependency versions (NumPy, pandas, Xarray, etc)
- GDAL and Ghostscript versions
- GMT library information

It also warns users if the installed Ghostscript version has serious bugs or is
Expand All @@ -104,9 +128,11 @@ def show_versions(file: TextIO | None = sys.stdout) -> None:
"executable": sys.executable,
"machine": platform.platform(),
}
requirements = [Requirement(v).name for v in requires("pygmt")] + ["gdal"] # type: ignore[union-attr]
requirements = [Requirement(v).name for v in requires("pygmt")] # type: ignore[union-attr]
dep_info = {name: _get_module_version(name) for name in requirements}
dep_info.update({"ghostscript": _get_ghostscript_version()})
dep_info.update(
{"gdal": _get_gdal_version(), "ghostscript": _get_ghostscript_version()}
)

lines = []
lines.append("PyGMT information:")
Expand Down
6 changes: 5 additions & 1 deletion pygmt/tests/test_show_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ def test_show_versions():
assert "System information:" in output
assert "Dependency information:" in output
assert "GMT library information:" in output
assert "WARNING:" not in output # No GMT-Ghostscript incompatibility warnings.
# No GMT-Ghostscript incompatibility warnings.
assert "WARNING:" not in output
# GDAL version is correctly reported.
assert "gdal:" in output
assert "gdal: None" not in output


@pytest.mark.parametrize(
Expand Down
Loading