Skip to content

Commit

Permalink
- added function to check and log missing plugin dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
rptmat57 committed Feb 13, 2025
1 parent 2787a97 commit dbaff28
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions NEMO/plugins/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from logging import getLogger
from typing import List, Optional, Tuple, Union

from django.core.exceptions import FieldDoesNotExist
from django.conf import settings
from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
from django.db.models import Field
from django.http import HttpResponse
from django.shortcuts import render
Expand All @@ -20,6 +21,29 @@ def render_combine_responses(request, original_response: HttpResponse, template_
return original_response


def check_extra_dependencies(app_name, extra_names: List[str]):
"""
Checks if the extra dependencies for a given app are satisfied. If not, it either
raises an error or logs a warning depending on the application settings.
Parameters:
app_name (str): Name of the application for which dependencies are validated.
extra_names (List[str]): List of extra dependency group names to be checked.
Raises:
ImproperlyConfigured: If the required dependencies are not satisfied and the
dependency check setting is enabled.
"""
requirements = get_extra_requires(app_name, extra_names)
if not is_any_requirement_satisfied(requirements):
message = f"{app_name} requires one of following dependencies: {' or '.join(str(requirement) for requirement in requirements)}"
if not settings.DEBUG and getattr(settings, "PLUGIN_DEPENDENCY_CHECK", True):
raise ImproperlyConfigured(message)
else:
utils_logger.warning(message)


# Checks that at least one requirement in the list is satisfied and returns the first one
def is_any_requirement_satisfied(
requirements: List[Union[Requirement, str]], prereleases=False
Expand All @@ -45,14 +69,15 @@ def check_version_satisfies(installed_version, requirement, prereleases=False) -
return True


def get_extra_requires(app_name, extra_name: str) -> List[Requirement]:
def get_extra_requires(app_name, extra_names: List[str]) -> List[Requirement]:
requirements: List[Requirement] = []
distribution = importlib.metadata.distribution(app_name)
requires = distribution.requires or []
for req in requires:
requirement = Requirement(req)
if requirement.marker and requirement.marker.evaluate({"extra": extra_name}):
requirements.append(requirement)
for extra_name in extra_names:
if requirement.marker and requirement.marker.evaluate({"extra": extra_name}):
requirements.append(requirement)
return requirements


Expand Down

0 comments on commit dbaff28

Please sign in to comment.