Skip to content

Commit

Permalink
If version is undetermined use ANY_VERSION
Browse files Browse the repository at this point in the history
Also makes required_version non-optional
  • Loading branch information
Wout Feys committed Sep 16, 2024
1 parent 532c73f commit 9bc76de
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 31 deletions.
5 changes: 4 additions & 1 deletion aikido_zen/background_process/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

MAX_REPORT_TRIES = 5

# If any version is supported, this constant can be used
ANY_VERSION = "0.0.0"

def pkg_compat_check(pkg_name, required_version="0.0.0"):

def pkg_compat_check(pkg_name, required_version):
"""Reports a newly wrapped package to the bg process"""
# Fetch package version :
try:
Expand Down
8 changes: 4 additions & 4 deletions aikido_zen/background_process/packages_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from unittest.mock import MagicMock
from .packages import pkg_compat_check
from .packages import pkg_compat_check, ANY_VERSION


@pytest.fixture
Expand Down Expand Up @@ -34,7 +34,7 @@ def test_pkg_compat_check_success(mock_get_comms, mocker):
)

# Call the function under test
pkg_compat_check(pkg_name)
pkg_compat_check(pkg_name, ANY_VERSION)

# Assert that the version was retrieved and the send_data_to_bg_process was called
assert mock_class.action == "WRAPPED_PACKAGE"
Expand Down Expand Up @@ -70,7 +70,7 @@ def test_pkg_compat_check_retry(mock_get_comms, mocker):
)

# Call the function under test
pkg_compat_check(pkg_name)
pkg_compat_check(pkg_name, ANY_VERSION)

# Assert that send_data_to_bg_process was called MAX_REPORT_TRIES times
assert mock_class.send_data_to_bg_process.call_count == 5
Expand Down Expand Up @@ -98,7 +98,7 @@ def test_pkg_compat_check_partial_success(mock_get_comms, mocker):
)

# Call the function under test
pkg_compat_check(pkg_name)
pkg_compat_check(pkg_name, ANY_VERSION)

# Assert that send_data_to_bg_process was called twice
assert mock_class.send_data_to_bg_process.call_count == 2
6 changes: 2 additions & 4 deletions aikido_zen/sources/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import copy
import aikido_zen.importhook as importhook
from aikido_zen.helpers.logging import logger
from aikido_zen.background_process.packages import pkg_compat_check
from aikido_zen.background_process.packages import pkg_compat_check, ANY_VERSION
from ..functions.request_handler import request_handler
from .run_init_stage import run_init_stage
from .pre_response_middleware import pre_response_middleware

REQUIRED_DJANGO_VERSION = "0.0.0"


@importhook.on_import("django.core.handlers.base")
def on_django_gunicorn_import(django):
Expand All @@ -19,7 +17,7 @@ def on_django_gunicorn_import(django):
# https://github.com/django/django/blob/5865ff5adcf64da03d306dc32b36e87ae6927c85/django/core/handlers/base.py#L174
Returns : Modified django.core.handlers.base object
"""
if not pkg_compat_check("django", REQUIRED_DJANGO_VERSION):
if not pkg_compat_check("django", required_version=ANY_VERSION):
return django

Check warning on line 21 in aikido_zen/sources/django/__init__.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sources/django/__init__.py#L20-L21

Added lines #L20 - L21 were not covered by tests
modified_django = importhook.copy_module(django)

Expand Down
7 changes: 2 additions & 5 deletions aikido_zen/sources/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import aikido_zen.importhook as importhook
from aikido_zen.helpers.logging import logger
from aikido_zen.context import Context
from aikido_zen.background_process.packages import pkg_compat_check
from aikido_zen.background_process.packages import pkg_compat_check, ANY_VERSION
from aikido_zen.context import get_current_context
from .functions.request_handler import request_handler

Expand Down Expand Up @@ -70,17 +70,14 @@ def aikido___call__(flask_app, environ, start_response):
return res


REQUIRED_FLASK_VERSION = "0.0.0"


@importhook.on_import("flask.app")
def on_flask_import(flask):
"""
Hook 'n wrap on `flask.app`. Flask class |-> App class |-> Scaffold class
@app.route |-> `add_url_rule` |-> self.view_functions. these get called via
full_dispatch_request, which we wrap. We also wrap __call__ to run our middleware.
"""
if not pkg_compat_check("flask", REQUIRED_FLASK_VERSION):
if not pkg_compat_check("flask", required_version=ANY_VERSION):
return flask

Check warning on line 81 in aikido_zen/sources/flask.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sources/flask.py#L80-L81

Added lines #L80 - L81 were not covered by tests
modified_flask = importhook.copy_module(flask)
former_fdr = copy.deepcopy(flask.Flask.full_dispatch_request)
Expand Down
4 changes: 2 additions & 2 deletions aikido_zen/sources/gunicorn.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Gunicorn Module, report if module was found"""

import aikido_zen.importhook as importhook
from aikido_zen.background_process.packages import pkg_compat_check
from aikido_zen.background_process.packages import pkg_compat_check, ANY_VERSION


@importhook.on_import("gunicorn")
def on_gunicorn_import(gunicorn):
"""Report to the core when gunicorn gets imported"""
pkg_compat_check("gunicorn")
pkg_compat_check("gunicorn", required_version=ANY_VERSION)

Check warning on line 10 in aikido_zen/sources/gunicorn.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sources/gunicorn.py#L10

Added line #L10 was not covered by tests
return gunicorn
6 changes: 2 additions & 4 deletions aikido_zen/sources/lxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
from aikido_zen.helpers.extract_data_from_xml_body import (
extract_data_from_xml_body,
)
from aikido_zen.background_process.packages import pkg_compat_check

REQUIRED_LXML_VERSION = "0.0.0"
from aikido_zen.background_process.packages import pkg_compat_check, ANY_VERSION


@importhook.on_import("lxml.etree")
Expand All @@ -20,7 +18,7 @@ def on_lxml_import(eltree):
- Wrap on
Returns : Modified `lxml.etree` object
"""
if not pkg_compat_check("lxml", REQUIRED_LXML_VERSION):
if not pkg_compat_check("lxml", required_version=ANY_VERSION):
return eltree

Check warning on line 22 in aikido_zen/sources/lxml.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sources/lxml.py#L21-L22

Added lines #L21 - L22 were not covered by tests
modified_eltree = importhook.copy_module(eltree)

Expand Down
7 changes: 2 additions & 5 deletions aikido_zen/sources/quart.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import aikido_zen.importhook as importhook
from aikido_zen.helpers.logging import logger
from aikido_zen.context import Context, get_current_context
from aikido_zen.background_process.packages import pkg_compat_check
from aikido_zen.background_process.packages import pkg_compat_check, ANY_VERSION
from .functions.request_handler import request_handler


Expand Down Expand Up @@ -81,16 +81,13 @@ async def send_status_code_and_text(send, pre_response):
)


REQUIRED_QUART_VERSION = "0.0.0"


@importhook.on_import("quart.app")
def on_quart_import(quart):
"""
Hook 'n wrap on `quart.app`
Our goal is to wrap the __call__, handle_request, asgi_app functios of the "Quart" class
"""
if not pkg_compat_check("quart", REQUIRED_QUART_VERSION):
if not pkg_compat_check("quart", required_version=ANY_VERSION):
return quart

Check warning on line 91 in aikido_zen/sources/quart.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sources/quart.py#L90-L91

Added lines #L90 - L91 were not covered by tests
modified_quart = importhook.copy_module(quart)

Expand Down
6 changes: 2 additions & 4 deletions aikido_zen/sources/starlette/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
"""

import aikido_zen.importhook as importhook
from aikido_zen.background_process.packages import pkg_compat_check

REQUIRED_STARLETTE_VERSION = "0.0.0"
from aikido_zen.background_process.packages import pkg_compat_check, ANY_VERSION


@importhook.on_import("starlette")
Expand All @@ -23,7 +21,7 @@ def on_starlette_import(starlette):
This checks for the package version of starlette so you don't have to do it twice,
once in starlette_applications and once in starlette_applications.
"""
if not pkg_compat_check("starlette", REQUIRED_STARLETTE_VERSION):
if not pkg_compat_check("starlette", required_version=ANY_VERSION):
return starlette

Check warning on line 25 in aikido_zen/sources/starlette/__init__.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sources/starlette/__init__.py#L24-L25

Added lines #L24 - L25 were not covered by tests
# Package is compatible, start wrapping :
import aikido_zen.sources.starlette.starlette_applications
Expand Down
4 changes: 2 additions & 2 deletions aikido_zen/sources/uwsgi.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""UWSGI Module, report if module was found"""

import aikido_zen.importhook as importhook
from aikido_zen.background_process.packages import pkg_compat_check
from aikido_zen.background_process.packages import pkg_compat_check, ANY_VERSION


@importhook.on_import("uwsgi")
def on_uwsgi_import(uwsgi):
"""Report to the core when uwsgi gets imported"""
pkg_compat_check("uwsgi")
pkg_compat_check("uwsgi", required_version=ANY_VERSION)

Check warning on line 10 in aikido_zen/sources/uwsgi.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sources/uwsgi.py#L10

Added line #L10 was not covered by tests
return uwsgi

0 comments on commit 9bc76de

Please sign in to comment.