Skip to content

Commit ec5e92c

Browse files
seismanweiji14
andauthored
Add show_versions() function for printing information useful when submitting bug reports (#466)
The `show_versions` function reports - PyGMT version - system information - dependency version - GMT library information The GitHub issue template and the install instruction are also updated. Co-authored-by: Wei Ji <[email protected]>
1 parent 4bfebcd commit ec5e92c

File tree

5 files changed

+95
-17
lines changed

5 files changed

+95
-17
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,8 @@ PASTE ERROR MESSAGE HERE
2222

2323
**System information**
2424

25-
* Operating system:
26-
* Python installation (Anaconda, system, ETS):
27-
* Version of GMT:
28-
* Version of Python:
29-
* Version of this package:
30-
* If using conda, paste the output of `conda list` below:
31-
32-
<details>
33-
<summary>output of conda list</summary>
34-
<pre>
35-
PASTE OUTPUT OF CONDA LIST HERE
36-
</pre>
37-
</details>
25+
Please paste the output of `python -c "import pygmt; pygmt.show_versions()"`:
26+
27+
```
28+
PASTE THE OUTPUT HERE
29+
```

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test:
2424
# Run a tmp folder to make sure the tests are run on the installed version
2525
mkdir -p $(TESTDIR)
2626
@echo ""
27-
@cd $(TESTDIR); python -c "import $(PROJECT); $(PROJECT).print_clib_info()"
27+
@cd $(TESTDIR); python -c "import $(PROJECT); $(PROJECT).show_versions()"
2828
@echo ""
2929
cd $(TESTDIR); pytest $(PYTEST_ARGS) $(PROJECT)
3030
cp $(TESTDIR)/.coverage* .

doc/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Miscellaneous
9292
which
9393
test
9494
print_clib_info
95+
show_versions
9596

9697

9798
.. automodule:: pygmt.datasets

doc/install.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ well (be sure to have your conda env activated)::
117117
Test your installation by running the following inside a Python interpreter::
118118

119119
import pygmt
120-
pygmt.print_clib_info()
120+
pygmt.show_versions()
121121
pygmt.test()
122122

123123

pygmt/__init__.py

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,98 @@ def print_clib_info():
4141
"""
4242
from .clib import Session
4343

44-
lines = ["Loaded libgmt:"]
44+
lines = ["GMT library information:"]
4545
with Session() as ses:
4646
for key in sorted(ses.info):
4747
lines.append(" {}: {}".format(key, ses.info[key]))
4848
print("\n".join(lines))
4949

5050

51+
def show_versions():
52+
"""
53+
Prints various dependency versions useful when submitting bug reports. This
54+
includes information about:
55+
56+
- PyGMT itself
57+
- System information (Python version, Operating System)
58+
- Core dependency versions (Numpy, Pandas, Xarray, etc)
59+
- GMT library information
60+
"""
61+
62+
import sys
63+
import platform
64+
import importlib
65+
import subprocess
66+
67+
def _get_module_version(modname):
68+
"""Get version information of a Python module."""
69+
try:
70+
if modname in sys.modules:
71+
module = sys.modules[modname]
72+
else:
73+
module = importlib.import_module(modname)
74+
75+
try:
76+
return module.__version__
77+
except AttributeError:
78+
return module.version
79+
except ImportError:
80+
return None
81+
82+
def _get_ghostscript_version():
83+
"""Get ghostscript version."""
84+
os_name = sys.platform
85+
if os_name.startswith("linux") or os_name == "darwin":
86+
cmds = ["gs"]
87+
elif os_name == "win32":
88+
cmds = ["gswin64c.exe", "gswin32c.exe"]
89+
else:
90+
return None
91+
92+
for gs_cmd in cmds:
93+
try:
94+
version = subprocess.check_output(
95+
[gs_cmd, "--version"], universal_newlines=True
96+
).strip()
97+
return version
98+
except FileNotFoundError:
99+
continue
100+
return None
101+
102+
def _get_gmt_version():
103+
"""Get GMT version."""
104+
try:
105+
version = subprocess.check_output(
106+
["gmt", "--version"], universal_newlines=True
107+
).strip()
108+
return version
109+
except FileNotFoundError:
110+
return None
111+
112+
sys_info = {
113+
"python": sys.version.replace("\n", " "),
114+
"executable": sys.executable,
115+
"machine": platform.platform(),
116+
}
117+
118+
deps = ["numpy", "pandas", "xarray", "netCDF4", "packaging"]
119+
120+
print("PyGMT information:")
121+
print(f" version: {__version__}")
122+
123+
print("System information:")
124+
for k, v in sys_info.items():
125+
print(f" {k}: {v}")
126+
127+
print("Dependency information:")
128+
for modname in deps:
129+
print(f" {modname}: {_get_module_version(modname)}")
130+
print(f" ghostscript: {_get_ghostscript_version()}")
131+
print(f" gmt: {_get_gmt_version()}")
132+
133+
print_clib_info()
134+
135+
51136
def test(doctest=True, verbose=True, coverage=False, figures=True):
52137
"""
53138
Run the test suite.
@@ -81,7 +166,7 @@ def test(doctest=True, verbose=True, coverage=False, figures=True):
81166
"""
82167
import pytest
83168

84-
print_clib_info()
169+
show_versions()
85170

86171
package = __name__
87172

0 commit comments

Comments
 (0)