Skip to content

Commit a42be05

Browse files
authored
Move pygmt.show_versions function to _show_versions.py (#3277)
1 parent 09e2352 commit a42be05

File tree

2 files changed

+140
-128
lines changed

2 files changed

+140
-128
lines changed

pygmt/__init__.py

Lines changed: 1 addition & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,10 @@
1919
"""
2020

2121
import atexit as _atexit
22-
import sys
23-
from importlib.metadata import version
24-
25-
# Get semantic version through setuptools-scm
26-
__version__ = f'v{version("pygmt")}' # e.g. v0.1.2.dev3+g0ab3cd78
27-
__commit__ = __version__.split("+g")[-1] if "+g" in __version__ else "" # 0ab3cd78
2822

2923
# Import modules to make the high-level GMT Python API
3024
from pygmt import datasets
25+
from pygmt._show_versions import __commit__, __version__, show_versions
3126
from pygmt.accessors import GMTDataArrayAccessor
3227
from pygmt.figure import Figure, set_display
3328
from pygmt.io import load_dataarray
@@ -75,125 +70,3 @@
7570
_begin()
7671
# Tell Python to run _end when shutting down
7772
_atexit.register(_end)
78-
79-
80-
def show_versions(file=sys.stdout):
81-
"""
82-
Print various dependency versions which are useful when submitting bug reports.
83-
84-
This includes information about:
85-
86-
- PyGMT itself
87-
- System information (Python version, Operating System)
88-
- Core dependency versions (NumPy, Pandas, Xarray, etc)
89-
- GMT library information
90-
91-
It also warns users if the installed Ghostscript version has serious bugs or is
92-
incompatible with the installed GMT version.
93-
"""
94-
95-
import importlib
96-
import platform
97-
import shutil
98-
import subprocess
99-
100-
from packaging.requirements import Requirement
101-
from packaging.version import Version
102-
103-
def _get_clib_info() -> dict:
104-
"""
105-
Return information about the GMT shared library.
106-
"""
107-
from pygmt.clib import Session
108-
109-
with Session() as ses:
110-
return ses.info
111-
112-
def _get_module_version(modname: str) -> str | None:
113-
"""
114-
Get version information of a Python module.
115-
"""
116-
try:
117-
if modname in sys.modules:
118-
module = sys.modules[modname]
119-
else:
120-
module = importlib.import_module(modname)
121-
122-
try:
123-
return module.__version__
124-
except AttributeError:
125-
return module.version
126-
except ImportError:
127-
return None
128-
129-
def _get_ghostscript_version() -> str | None:
130-
"""
131-
Get Ghostscript version.
132-
"""
133-
match sys.platform:
134-
case "linux" | "darwin":
135-
cmds = ["gs"]
136-
case os_name if os_name.startswith("freebsd"):
137-
cmds = ["gs"]
138-
case "win32":
139-
cmds = ["gswin64c.exe", "gswin32c.exe"]
140-
case _:
141-
return None
142-
143-
for gs_cmd in cmds:
144-
if (gsfullpath := shutil.which(gs_cmd)) is not None:
145-
return subprocess.check_output(
146-
[gsfullpath, "--version"], universal_newlines=True
147-
).strip()
148-
return None
149-
150-
def _check_ghostscript_version(gs_version: str) -> str | None:
151-
"""
152-
Check if the Ghostscript version is compatible with GMT versions.
153-
"""
154-
match Version(gs_version):
155-
case v if v < Version("9.53"):
156-
return (
157-
f"Ghostscript v{gs_version} is too old and may have serious bugs. "
158-
"Please consider upgrading your Ghostscript."
159-
)
160-
case v if Version("10.00") <= v < Version("10.02"):
161-
return (
162-
f"Ghostscript v{gs_version} has known bugs. "
163-
"Please consider upgrading to version v10.02 or later."
164-
)
165-
case v if v >= Version("10.02"):
166-
from pygmt.clib import __gmt_version__
167-
168-
if Version(__gmt_version__) < Version("6.5.0"):
169-
return (
170-
f"GMT v{__gmt_version__} doesn't support Ghostscript "
171-
"v{gs_version}. Please consider upgrading to GMT>=6.5.0 or "
172-
"downgrading to Ghostscript v9.56."
173-
)
174-
return None
175-
176-
sys_info = {
177-
"python": sys.version.replace("\n", " "),
178-
"executable": sys.executable,
179-
"machine": platform.platform(),
180-
}
181-
deps = [Requirement(v).name for v in importlib.metadata.requires("pygmt")]
182-
gs_version = _get_ghostscript_version()
183-
184-
lines = []
185-
lines.append("PyGMT information:")
186-
lines.append(f" version: {__version__}")
187-
lines.append("System information:")
188-
lines.extend([f" {key}: {val}" for key, val in sys_info.items()])
189-
lines.append("Dependency information:")
190-
lines.extend([f" {modname}: {_get_module_version(modname)}" for modname in deps])
191-
lines.append(f" ghostscript: {gs_version}")
192-
lines.append("GMT library information:")
193-
lines.extend([f" {key}: {val}" for key, val in _get_clib_info().items()])
194-
195-
if warnmsg := _check_ghostscript_version(gs_version):
196-
lines.append("WARNING:")
197-
lines.append(f" {warnmsg}")
198-
199-
print("\n".join(lines), file=file)

pygmt/_show_versions.py

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
"""
2+
Utility methods to print system info for debugging.
3+
4+
Adapted from :func:`rioxarray.show_versions` and :func:`pandas.show_versions`.
5+
"""
6+
7+
import importlib
8+
import platform
9+
import shutil
10+
import sys
11+
from importlib.metadata import version
12+
13+
# Get semantic version through setuptools-scm
14+
__version__ = f'v{version("pygmt")}' # e.g. v0.1.2.dev3+g0ab3cd78
15+
__commit__ = __version__.split("+g")[-1] if "+g" in __version__ else "" # 0ab3cd78
16+
17+
18+
def _get_clib_info() -> dict:
19+
"""
20+
Return information about the GMT shared library.
21+
"""
22+
from pygmt.clib import Session
23+
24+
with Session() as ses:
25+
return ses.info
26+
27+
28+
def _get_module_version(modname: str) -> str | None:
29+
"""
30+
Get version information of a Python module.
31+
"""
32+
try:
33+
if modname in sys.modules:
34+
module = sys.modules[modname]
35+
else:
36+
module = importlib.import_module(modname)
37+
38+
try:
39+
return module.__version__
40+
except AttributeError:
41+
return module.version
42+
except ImportError:
43+
return None
44+
45+
46+
def _get_ghostscript_version() -> str | None:
47+
"""
48+
Get Ghostscript version.
49+
"""
50+
import subprocess
51+
52+
match sys.platform:
53+
case "linux" | "darwin":
54+
cmds = ["gs"]
55+
case os_name if os_name.startswith("freebsd"):
56+
cmds = ["gs"]
57+
case "win32":
58+
cmds = ["gswin64c.exe", "gswin32c.exe"]
59+
case _:
60+
return None
61+
62+
for gs_cmd in cmds:
63+
if (gsfullpath := shutil.which(gs_cmd)) is not None:
64+
return subprocess.check_output(
65+
[gsfullpath, "--version"], universal_newlines=True
66+
).strip()
67+
return None
68+
69+
70+
def _check_ghostscript_version(gs_version: str) -> str | None:
71+
"""
72+
Check if the Ghostscript version is compatible with GMT versions.
73+
"""
74+
from packaging.version import Version
75+
76+
match Version(gs_version):
77+
case v if v < Version("9.53"):
78+
return (
79+
f"Ghostscript v{gs_version} is too old and may have serious bugs. "
80+
"Please consider upgrading your Ghostscript."
81+
)
82+
case v if Version("10.00") <= v < Version("10.02"):
83+
return (
84+
f"Ghostscript v{gs_version} has known bugs. "
85+
"Please consider upgrading to version v10.02 or later."
86+
)
87+
case v if v >= Version("10.02"):
88+
from pygmt.clib import __gmt_version__
89+
90+
if Version(__gmt_version__) < Version("6.5.0"):
91+
return (
92+
f"GMT v{__gmt_version__} doesn't support Ghostscript "
93+
"v{gs_version}. Please consider upgrading to GMT>=6.5.0 or "
94+
"downgrading to Ghostscript v9.56."
95+
)
96+
return None
97+
98+
99+
def show_versions(file=sys.stdout):
100+
"""
101+
Print various dependency versions which are useful when submitting bug reports.
102+
103+
This includes information about:
104+
105+
- PyGMT itself
106+
- System information (Python version, Operating System)
107+
- Core dependency versions (NumPy, Pandas, Xarray, etc)
108+
- GMT library information
109+
110+
It also warns users if the installed Ghostscript version has serious bugs or is
111+
incompatible with the installed GMT version.
112+
"""
113+
114+
from packaging.requirements import Requirement
115+
116+
sys_info = {
117+
"python": sys.version.replace("\n", " "),
118+
"executable": sys.executable,
119+
"machine": platform.platform(),
120+
}
121+
deps = [Requirement(v).name for v in importlib.metadata.requires("pygmt")]
122+
gs_version = _get_ghostscript_version()
123+
124+
lines = []
125+
lines.append("PyGMT information:")
126+
lines.append(f" version: {__version__}")
127+
lines.append("System information:")
128+
lines.extend([f" {key}: {val}" for key, val in sys_info.items()])
129+
lines.append("Dependency information:")
130+
lines.extend([f" {modname}: {_get_module_version(modname)}" for modname in deps])
131+
lines.append(f" ghostscript: {gs_version}")
132+
lines.append("GMT library information:")
133+
lines.extend([f" {key}: {val}" for key, val in _get_clib_info().items()])
134+
135+
if warnmsg := _check_ghostscript_version(gs_version):
136+
lines.append("WARNING:")
137+
lines.append(f" {warnmsg}")
138+
139+
print("\n".join(lines), file=file)

0 commit comments

Comments
 (0)