Skip to content

Commit

Permalink
Add show_versions to utils and use in check_satpy
Browse files Browse the repository at this point in the history
  • Loading branch information
verduijn committed Sep 27, 2024
1 parent b86f4b6 commit 66712e8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 15 deletions.
18 changes: 11 additions & 7 deletions satpy/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,17 @@ def test_specific_check_satpy(self):
"""Test 'check_satpy' with specific features provided."""
from satpy.utils import check_satpy
with mock.patch("satpy.utils.print") as print_mock:
check_satpy(readers=["viirs_sdr"], extras=("cartopy", "__fake"))
checked_fake = False
for call in print_mock.mock_calls:
if len(call[1]) > 0 and "__fake" in call[1][0]:
assert "ok" not in call[1][1]
checked_fake = True
assert checked_fake, "Did not find __fake module mentioned in checks"
check_satpy(readers=["viirs_sdr"], packages=("cartopy", "__fake"))
checked_fake = any("__fake: not installed" in c[1] for c in print_mock.mock_calls if len(c[1]))
assert checked_fake, "Did not find __fake package mentioned in checks"

class TestShowVersions:
"""Test the 'show_versions' function."""

def test_basic_show_versions(self):
"""Test 'check_satpy' basic functionality."""
from satpy.utils import show_versions
show_versions()


def test_debug_on(caplog):
Expand Down
71 changes: 63 additions & 8 deletions satpy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

import contextlib
import datetime
import importlib.metadata
import logging
import os
import pathlib
import platform
import warnings
from contextlib import contextmanager
from copy import deepcopy
Expand Down Expand Up @@ -476,6 +478,19 @@ def _check_yaml_configs(configs, key):
pass
return diagnostic

def _check_package_version(package_name: str) -> Optional[str]:
"""Check the version of `package_name`.
Args:
package_name (str): the distribution package name.
Returns:
the version number if available else `None`.
"""
try:
return importlib.metadata.version(package_name)
except importlib.metadata.PackageNotFoundError:
return None

def _check_import(module_names):
"""Import the specified modules and provide status."""
Expand All @@ -490,13 +505,58 @@ def _check_import(module_names):
return diagnostics


def check_satpy(readers=None, writers=None, extras=None):
def show_versions(packages=None):
"""Shows version for system, python and common packages (if installed).
Args:
packages (list or None): Limit packages to those specified.
Returns:
None.
"""
packages = (
(
"cartopy",
"geoviews",
"numpy",
"dask",
"xarray",
"gdal",
"rasterio",
"pyproj",
"netcdf4",
"h5py",
"pyhdf",
"h5netcdf",
"fsspec",
)
if packages is None
else packages
)

print("Versions") # noqa: T201
print("======") # noqa: T201
print(f"platform: {platform.platform()}") # noqa: T201
print(f"python: {platform.python_version()}") # noqa: T201
print() # noqa: T201

for package_name in sorted(packages):
package_version = _check_package_version(package_name)
print( # noqa: T201
f"{package_name}: {package_version if package_version else 'not installed'}"
)

print() # noqa: T201


def check_satpy(readers=None, writers=None, packages=None):
"""Check the satpy readers and writers for correct installation.
Args:
readers (list or None): Limit readers checked to those specified
writers (list or None): Limit writers checked to those specified
extras (list or None): Limit extras checked to those specified
packages (list or None): Limit packages checked to those specified
Returns:
None
Expand All @@ -517,12 +577,7 @@ def check_satpy(readers=None, writers=None, extras=None):
print(writer + ": ", res) # noqa: T201
print() # noqa: T201

print("Extras") # noqa: T201
print("======") # noqa: T201
module_names = extras if extras is not None else ("cartopy", "geoviews")
for module_name, res in sorted(_check_import(module_names).items()):
print(module_name + ": ", res) # noqa: T201
print() # noqa: T201
show_versions(packages=packages)


def unify_chunks(*data_arrays: xr.DataArray) -> tuple[xr.DataArray, ...]:
Expand Down

0 comments on commit 66712e8

Please sign in to comment.