|
19 | 19 | """
|
20 | 20 |
|
21 | 21 | 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 |
28 | 22 |
|
29 | 23 | # Import modules to make the high-level GMT Python API
|
30 | 24 | from pygmt import datasets
|
| 25 | +from pygmt._show_versions import __commit__, __version__, show_versions |
31 | 26 | from pygmt.accessors import GMTDataArrayAccessor
|
32 | 27 | from pygmt.figure import Figure, set_display
|
33 | 28 | from pygmt.io import load_dataarray
|
|
75 | 70 | _begin()
|
76 | 71 | # Tell Python to run _end when shutting down
|
77 | 72 | _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) |
0 commit comments