Skip to content

Commit 8d12796

Browse files
authored
pygmt.show_versions: Remove _get_gdal_version and create a dictionary for dependency versions (#3376)
1 parent dfedf16 commit 8d12796

File tree

1 file changed

+24
-32
lines changed

1 file changed

+24
-32
lines changed

pygmt/_show_versions.py

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,24 @@
77
import importlib
88
import platform
99
import shutil
10+
import subprocess
1011
import sys
1112
from importlib.metadata import version
13+
from typing import TextIO
14+
15+
from packaging.requirements import Requirement
16+
from packaging.version import Version
17+
from pygmt.clib import Session, __gmt_version__
1218

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

1723

18-
def _get_clib_info() -> dict:
24+
def _get_clib_info() -> dict[str, str]:
1925
"""
20-
Return information about the GMT shared library.
26+
Get information about the GMT shared library.
2127
"""
22-
from pygmt.clib import Session
23-
2428
with Session() as ses:
2529
return ses.info
2630

@@ -47,8 +51,6 @@ def _get_ghostscript_version() -> str | None:
4751
"""
4852
Get Ghostscript version.
4953
"""
50-
import subprocess
51-
5254
match sys.platform:
5355
case "linux" | "darwin":
5456
cmds = ["gs"]
@@ -67,11 +69,12 @@ def _get_ghostscript_version() -> str | None:
6769
return None
6870

6971

70-
def _check_ghostscript_version(gs_version: str) -> str | None:
72+
def _check_ghostscript_version(gs_version: str | None) -> str | None:
7173
"""
7274
Check if the Ghostscript version is compatible with GMT versions.
7375
"""
74-
from packaging.version import Version
76+
if gs_version is None:
77+
return "Ghostscript is not detected. Your installation may be broken."
7578

7679
match Version(gs_version):
7780
case v if v < Version("9.53"):
@@ -85,8 +88,6 @@ def _check_ghostscript_version(gs_version: str) -> str | None:
8588
"Please consider upgrading to version v10.02 or later."
8689
)
8790
case v if v >= Version("10.02"):
88-
from pygmt.clib import __gmt_version__
89-
9091
if Version(__gmt_version__) < Version("6.5.0"):
9192
return (
9293
f"GMT v{__gmt_version__} doesn't support Ghostscript "
@@ -96,19 +97,7 @@ def _check_ghostscript_version(gs_version: str) -> str | None:
9697
return None
9798

9899

99-
def _get_gdal_version():
100-
"""
101-
Get GDAL version.
102-
"""
103-
try:
104-
from osgeo import gdal
105-
106-
return gdal.__version__
107-
except ImportError:
108-
return None
109-
110-
111-
def show_versions(file=sys.stdout):
100+
def show_versions(file: TextIO | None = sys.stdout):
112101
"""
113102
Print various dependency versions which are useful when submitting bug reports.
114103
@@ -123,30 +112,33 @@ def show_versions(file=sys.stdout):
123112
incompatible with the installed GMT version.
124113
"""
125114

126-
from packaging.requirements import Requirement
127-
128115
sys_info = {
129116
"python": sys.version.replace("\n", " "),
130117
"executable": sys.executable,
131118
"machine": platform.platform(),
132119
}
133-
deps = [Requirement(v).name for v in importlib.metadata.requires("pygmt")]
134-
gs_version = _get_ghostscript_version()
135-
gdal_version = _get_gdal_version()
120+
dep_info = {
121+
Requirement(v).name: _get_module_version(Requirement(v).name)
122+
for v in importlib.metadata.requires("pygmt") # type: ignore[union-attr]
123+
}
124+
dep_info.update(
125+
{
126+
"gdal": _get_module_version("osgeo.gdal"),
127+
"ghostscript": _get_ghostscript_version(),
128+
}
129+
)
136130

137131
lines = []
138132
lines.append("PyGMT information:")
139133
lines.append(f" version: {__version__}")
140134
lines.append("System information:")
141135
lines.extend([f" {key}: {val}" for key, val in sys_info.items()])
142136
lines.append("Dependency information:")
143-
lines.extend([f" {modname}: {_get_module_version(modname)}" for modname in deps])
144-
lines.append(f" gdal: {gdal_version}")
145-
lines.append(f" ghostscript: {gs_version}")
137+
lines.extend([f" {key}: {val}" for key, val in dep_info.items()])
146138
lines.append("GMT library information:")
147139
lines.extend([f" {key}: {val}" for key, val in _get_clib_info().items()])
148140

149-
if warnmsg := _check_ghostscript_version(gs_version):
141+
if warnmsg := _check_ghostscript_version(dep_info["ghostscript"]):
150142
lines.append("WARNING:")
151143
lines.append(f" {warnmsg}")
152144

0 commit comments

Comments
 (0)