"""Utility methods to print system info for debugging.Adapted from :func:`rioxarray.show_versions` and :func:`pandas.show_versions`."""importimportlibimportplatformimportshutilimportsysfromimportlib.metadataimportversion# Get semantic version through setuptools-scm__version__=f'v{version("pygmt")}'# e.g. v0.1.2.dev3+g0ab3cd78__commit__=__version__.split("+g")[-1]if"+g"in__version__else""# 0ab3cd78def_get_clib_info()->dict:""" Return information about the GMT shared library. """frompygmt.clibimportSessionwithSession()asses:returnses.infodef_get_module_version(modname:str)->str|None:""" Get version information of a Python module. """try:ifmodnameinsys.modules:module=sys.modules[modname]else:module=importlib.import_module(modname)try:returnmodule.__version__exceptAttributeError:returnmodule.versionexceptImportError:returnNonedef_get_ghostscript_version()->str|None:""" Get Ghostscript version. """importsubprocessmatchsys.platform:case"linux"|"darwin":cmds=["gs"]caseos_nameifos_name.startswith("freebsd"):cmds=["gs"]case"win32":cmds=["gswin64c.exe","gswin32c.exe"]case_:returnNoneforgs_cmdincmds:if(gsfullpath:=shutil.which(gs_cmd))isnotNone:returnsubprocess.check_output([gsfullpath,"--version"],universal_newlines=True).strip()returnNonedef_check_ghostscript_version(gs_version:str)->str|None:""" Check if the Ghostscript version is compatible with GMT versions. """frompackaging.versionimportVersionmatchVersion(gs_version):casevifv<Version("9.53"):return(f"Ghostscript v{gs_version} is too old and may have serious bugs. ""Please consider upgrading your Ghostscript.")casevifVersion("10.00")<=v<Version("10.02"):return(f"Ghostscript v{gs_version} has known bugs. ""Please consider upgrading to version v10.02 or later.")casevifv>=Version("10.02"):frompygmt.clibimport__gmt_version__ifVersion(__gmt_version__)<Version("6.5.0"):return(f"GMT v{__gmt_version__} doesn't support Ghostscript "f"v{gs_version}. Please consider upgrading to GMT>=6.5.0 or ""downgrading to Ghostscript v9.56.")returnNone
[docs]defshow_versions(file=sys.stdout):""" Print various dependency versions which are useful when submitting bug reports. This includes information about: - PyGMT itself - System information (Python version, Operating System) - Core dependency versions (NumPy, Pandas, Xarray, etc) - GMT library information It also warns users if the installed Ghostscript version has serious bugs or is incompatible with the installed GMT version. """frompackaging.requirementsimportRequirementsys_info={"python":sys.version.replace("\n"," "),"executable":sys.executable,"machine":platform.platform(),}deps=[Requirement(v).nameforvinimportlib.metadata.requires("pygmt")]gs_version=_get_ghostscript_version()lines=[]lines.append("PyGMT information:")lines.append(f" version: {__version__}")lines.append("System information:")lines.extend([f" {key}: {val}"forkey,valinsys_info.items()])lines.append("Dependency information:")lines.extend([f" {modname}: {_get_module_version(modname)}"formodnameindeps])lines.append(f" ghostscript: {gs_version}")lines.append("GMT library information:")lines.extend([f" {key}: {val}"forkey,valin_get_clib_info().items()])ifwarnmsg:=_check_ghostscript_version(gs_version):lines.append("WARNING:")lines.append(f" {warnmsg}")print("\n".join(lines),file=file)