Skip to content

Commit

Permalink
Simplify the _get_module_version function and add two more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Oct 19, 2024
1 parent d1ab97c commit d63b345
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
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"]
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
26 changes: 26 additions & 0 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 @@ -45,3 +46,28 @@ def test_show_versions_ghostscript_warnings(gs_version, gmt_version, monkeypatch
buf = io.StringIO()
pygmt.show_versions(file=buf)
assert "WARNING:" in buf.getvalue()


# Test for unsupport operating system.


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()


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()

0 comments on commit d63b345

Please sign in to comment.