Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
46 changes: 14 additions & 32 deletions pygmt/_show_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
Adapted from :func:`rioxarray.show_versions` and :func:`pandas.show_versions`.
"""

import importlib
import platform
import shutil
import subprocess
import sys
from importlib.metadata import version
from importlib.metadata import PackageNotFoundError, requires, version
from typing import TextIO

from packaging.requirements import Requirement
Expand All @@ -25,25 +24,17 @@ def _get_clib_info() -> dict[str, str]:
"""
Get information about the GMT shared library.
"""
with Session() as ses:
return ses.info
with Session() as lib:
return lib.info


def _get_module_version(modname: str) -> str | None:
"""
Get version information of a Python module.
"""
try:
if modname in sys.modules:
module = sys.modules[modname]
else:
module = importlib.import_module(modname)

try:
return module.__version__
except AttributeError:
return module.version
except ImportError:
return version(modname)
except PackageNotFoundError:
return None


Expand Down Expand Up @@ -85,13 +76,12 @@ def _check_ghostscript_version(gs_version: str | None) -> str | None:
f"Ghostscript v{gs_version} has known bugs. "
"Please consider upgrading to version v10.02 or later."
)
case v if v >= Version("10.02"):
if Version(__gmt_version__) < Version("6.5.0"):
return (
f"GMT v{__gmt_version__} doesn't support Ghostscript "
f"v{gs_version}. Please consider upgrading to GMT>=6.5.0 or "
"downgrading to Ghostscript v9.56."
)
case v if v >= Version("10.02") and Version(__gmt_version__) < Version("6.5.0"):
return (
f"GMT v{__gmt_version__} doesn't support Ghostscript v{gs_version}. "
"Please consider upgrading to GMT>=6.5.0 or downgrading to Ghostscript "
"v9.56."
)
return None


Expand All @@ -109,22 +99,14 @@ def show_versions(file: TextIO | None = sys.stdout):
It also warns users if the installed Ghostscript version has serious bugs or is
incompatible with the installed GMT version.
"""

sys_info = {
"python": sys.version.replace("\n", " "),
"executable": sys.executable,
"machine": platform.platform(),
}
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(),
}
)
requirements = [Requirement(v).name for v in requires("pygmt")] + ["gdal"] # type: ignore[union-attr]
Copy link
Member Author

Choose a reason for hiding this comment

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

Previously, we have to use _get_module_version("osgeo.gdal"), now we can use _get_module_version("gdal").

Copy link
Member

Choose a reason for hiding this comment

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

Nice to see that importlib.metadata.version("gdal") works!

dep_info = {name: _get_module_version(name) for name in requirements}
dep_info.update({"ghostscript": _get_ghostscript_version()})

lines = []
lines.append("PyGMT information:")
Expand Down
42 changes: 34 additions & 8 deletions pygmt/tests/test_show_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import io
from unittest import mock

import pygmt
import pytest
Expand Down Expand Up @@ -33,15 +34,40 @@ def test_show_versions():
(None, "6.5.0"),
],
)
def test_show_versions_ghostscript_warnings(gs_version, gmt_version, monkeypatch):
def test_show_versions_ghostscript_warnings(gs_version, gmt_version):
"""
Check that pygmt.show_versions reports warnings for GMT-Ghostscript incompatibility.
"""
monkeypatch.setattr("pygmt._show_versions.__gmt_version__", gmt_version)
monkeypatch.setattr(
"pygmt._show_versions._get_ghostscript_version", lambda: gs_version
)
with (
mock.patch("pygmt._show_versions.__gmt_version__", gmt_version),
mock.patch(
"pygmt._show_versions._get_ghostscript_version", return_value=gs_version
),
):
buf = io.StringIO()
pygmt.show_versions(file=buf)
assert "WARNING:" in buf.getvalue()

buf = io.StringIO()
pygmt.show_versions(file=buf)
assert "WARNING:" in buf.getvalue()

def test_show_versions_ghostscript_unsupported_os():
"""
Check that pygmt.show_versions reports ghostscript version is None for an
unsupported operating system.
"""
with mock.patch("sys.platform", new="unsupported_os"):
buf = io.StringIO()
pygmt.show_versions(file=buf)
assert "ghostscript: None" in buf.getvalue()
assert "WARNING:" in buf.getvalue()


def test_show_versions_ghostscript_not_found():
"""
Check that pygmt.show_versions reports ghostscript version is None when ghostscript
is not found in the system.
"""
with mock.patch("shutil.which", return_value=None):
buf = io.StringIO()
pygmt.show_versions(file=buf)
assert "ghostscript: None" in buf.getvalue()
assert "WARNING:" in buf.getvalue()