Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update check_satpy to use new show_version to display package versions #2913

Merged
merged 5 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions satpy/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,18 @@ 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"

verduijn marked this conversation as resolved.
Show resolved Hide resolved

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()
sfinkens marked this conversation as resolved.
Show resolved Hide resolved


def test_debug_on(caplog):
Expand Down
85 changes: 64 additions & 21 deletions satpy/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2009-2023 Satpy developers

Check notice on line 1 in satpy/utils.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Lines of Code in a Single File

The lines of code increases from 612 to 644, improve code health by reducing it to 600. The number of Lines of Code in a single file. More Lines of Code lowers the code health.
#
# This file is part of satpy.
#
Expand All @@ -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,30 +478,76 @@
pass
return diagnostic

def _check_package_version(package_name: str) -> Optional[str]:
"""Check the version of `package_name`.

def _check_import(module_names):
"""Import the specified modules and provide status."""
diagnostics = {}
for module_name in module_names:
try:
__import__(module_name)
res = "ok"
except ImportError as err:
res = str(err)
diagnostics[module_name] = res
return diagnostics
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 show_versions(packages=None):
verduijn marked this conversation as resolved.
Show resolved Hide resolved
"""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, extras=None):
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: bool
True if all specified features were successfully loaded.
Returns:
None

"""
from satpy.readers import configs_for_reader
Expand All @@ -517,12 +565,7 @@
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