Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pygmt.show_versions: Remove _get_gdal_version and create a dictionary for dependency versions #3376

Merged
merged 8 commits into from
Aug 7, 2024
56 changes: 24 additions & 32 deletions pygmt/_show_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@
import importlib
import platform
import shutil
import subprocess
import sys
from importlib.metadata import version
from typing import TextIO

from packaging.requirements import Requirement
from packaging.version import Version
from pygmt.clib import Session, __gmt_version__

# Get semantic version through setuptools-scm
__version__ = f'v{version("pygmt")}' # e.g. v0.1.2.dev3+g0ab3cd78
__commit__ = __version__.split("+g")[-1] if "+g" in __version__ else "" # 0ab3cd78


def _get_clib_info() -> dict:
def _get_clib_info() -> dict[str, str]:
"""
Return information about the GMT shared library.
Get information about the GMT shared library.
"""
from pygmt.clib import Session

with Session() as ses:
return ses.info

Expand All @@ -47,8 +51,6 @@
"""
Get Ghostscript version.
"""
import subprocess

match sys.platform:
case "linux" | "darwin":
cmds = ["gs"]
Expand All @@ -67,11 +69,12 @@
return None


def _check_ghostscript_version(gs_version: str) -> str | None:
def _check_ghostscript_version(gs_version: str | None) -> str | None:
"""
Check if the Ghostscript version is compatible with GMT versions.
"""
from packaging.version import Version
if gs_version is None:
return None

Check warning on line 77 in pygmt/_show_versions.py

View check run for this annotation

Codecov / codecov/patch

pygmt/_show_versions.py#L77

Added line #L77 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If ghostscript version cannot be detected, should there be a warning raised here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seisman marked this conversation as resolved.
Show resolved Hide resolved

match Version(gs_version):
case v if v < Version("9.53"):
Expand All @@ -85,8 +88,6 @@
"Please consider upgrading to version v10.02 or later."
)
case v if v >= Version("10.02"):
from pygmt.clib import __gmt_version__

if Version(__gmt_version__) < Version("6.5.0"):
return (
f"GMT v{__gmt_version__} doesn't support Ghostscript "
Expand All @@ -96,19 +97,7 @@
return None


def _get_gdal_version():
"""
Get GDAL version.
"""
try:
from osgeo import gdal

return gdal.__version__
except ImportError:
return None


def show_versions(file=sys.stdout):
def show_versions(file: TextIO | None = sys.stdout):
"""
Print various dependency versions which are useful when submitting bug reports.

Expand All @@ -123,30 +112,33 @@
incompatible with the installed GMT version.
"""

from packaging.requirements import Requirement

sys_info = {
"python": sys.version.replace("\n", " "),
"executable": sys.executable,
"machine": platform.platform(),
}
deps = [Requirement(v).name for v in importlib.metadata.requires("pygmt")]
gs_version = _get_ghostscript_version()
gdal_version = _get_gdal_version()
dep_info = {
Requirement(v).name: _get_module_version(Requirement(v).name)
for v in importlib.metadata.requires("pygmt") # type: ignore[union-attr]
}
dep_info.update(
{
"gdal": _get_module_version("osgeo.gdal"),
"ghostscript": _get_ghostscript_version(),
}
)

lines = []
lines.append("PyGMT information:")
lines.append(f" version: {__version__}")
lines.append("System information:")
lines.extend([f" {key}: {val}" for key, val in sys_info.items()])
lines.append("Dependency information:")
lines.extend([f" {modname}: {_get_module_version(modname)}" for modname in deps])
lines.append(f" gdal: {gdal_version}")
lines.append(f" ghostscript: {gs_version}")
lines.extend([f" {key}: {val}" for key, val in dep_info.items()])
lines.append("GMT library information:")
lines.extend([f" {key}: {val}" for key, val in _get_clib_info().items()])

if warnmsg := _check_ghostscript_version(gs_version):
if warnmsg := _check_ghostscript_version(dep_info["ghostscript"]):
lines.append("WARNING:")
lines.append(f" {warnmsg}")

Expand Down