7
7
import importlib
8
8
import platform
9
9
import shutil
10
+ import subprocess
10
11
import sys
11
12
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__
12
18
13
19
# Get semantic version through setuptools-scm
14
20
__version__ = f'v{ version ("pygmt" )} ' # e.g. v0.1.2.dev3+g0ab3cd78
15
21
__commit__ = __version__ .split ("+g" )[- 1 ] if "+g" in __version__ else "" # 0ab3cd78
16
22
17
23
18
- def _get_clib_info () -> dict :
24
+ def _get_clib_info () -> dict [ str , str ] :
19
25
"""
20
- Return information about the GMT shared library.
26
+ Get information about the GMT shared library.
21
27
"""
22
- from pygmt .clib import Session
23
-
24
28
with Session () as ses :
25
29
return ses .info
26
30
@@ -47,8 +51,6 @@ def _get_ghostscript_version() -> str | None:
47
51
"""
48
52
Get Ghostscript version.
49
53
"""
50
- import subprocess
51
-
52
54
match sys .platform :
53
55
case "linux" | "darwin" :
54
56
cmds = ["gs" ]
@@ -67,11 +69,12 @@ def _get_ghostscript_version() -> str | None:
67
69
return None
68
70
69
71
70
- def _check_ghostscript_version (gs_version : str ) -> str | None :
72
+ def _check_ghostscript_version (gs_version : str | None ) -> str | None :
71
73
"""
72
74
Check if the Ghostscript version is compatible with GMT versions.
73
75
"""
74
- from packaging .version import Version
76
+ if gs_version is None :
77
+ return "Ghostscript is not detected. Your installation may be broken."
75
78
76
79
match Version (gs_version ):
77
80
case v if v < Version ("9.53" ):
@@ -85,8 +88,6 @@ def _check_ghostscript_version(gs_version: str) -> str | None:
85
88
"Please consider upgrading to version v10.02 or later."
86
89
)
87
90
case v if v >= Version ("10.02" ):
88
- from pygmt .clib import __gmt_version__
89
-
90
91
if Version (__gmt_version__ ) < Version ("6.5.0" ):
91
92
return (
92
93
f"GMT v{ __gmt_version__ } doesn't support Ghostscript "
@@ -96,19 +97,7 @@ def _check_ghostscript_version(gs_version: str) -> str | None:
96
97
return None
97
98
98
99
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 ):
112
101
"""
113
102
Print various dependency versions which are useful when submitting bug reports.
114
103
@@ -123,30 +112,33 @@ def show_versions(file=sys.stdout):
123
112
incompatible with the installed GMT version.
124
113
"""
125
114
126
- from packaging .requirements import Requirement
127
-
128
115
sys_info = {
129
116
"python" : sys .version .replace ("\n " , " " ),
130
117
"executable" : sys .executable ,
131
118
"machine" : platform .platform (),
132
119
}
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
+ )
136
130
137
131
lines = []
138
132
lines .append ("PyGMT information:" )
139
133
lines .append (f" version: { __version__ } " )
140
134
lines .append ("System information:" )
141
135
lines .extend ([f" { key } : { val } " for key , val in sys_info .items ()])
142
136
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 ()])
146
138
lines .append ("GMT library information:" )
147
139
lines .extend ([f" { key } : { val } " for key , val in _get_clib_info ().items ()])
148
140
149
- if warnmsg := _check_ghostscript_version (gs_version ):
141
+ if warnmsg := _check_ghostscript_version (dep_info [ "ghostscript" ] ):
150
142
lines .append ("WARNING:" )
151
143
lines .append (f" { warnmsg } " )
152
144
0 commit comments