From b63b0f52c601e2f0f3bfa1fb451ea01e5cabe6a2 Mon Sep 17 00:00:00 2001 From: Maxime Alves LIRMM Date: Mon, 21 Mar 2022 09:45:35 +0100 Subject: [PATCH] [domain] checks versions specified in __deps__ variable of domain module --- Dockerfile | 5 +---- Pipfile | 1 + halfapi/half_domain.py | 16 ++++++++++++++++ halfapi/testing/test_domain.py | 6 ++++++ tests/dummy_domain/__init__.py | 6 ++++++ 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index ea04b12..05f2b99 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,9 +4,6 @@ COPY . /halfapi WORKDIR /halfapi RUN apt-get update > /dev/null && apt-get -y install git > /dev/null RUN pip install gunicorn uvicorn -# Install package with extra_requires for testing -RUN pip install ".[tests]" -ENV PYTHONPATH=./tests -RUN pytest +RUN pip install . CMD gunicorn halfapi.app diff --git a/Pipfile b/Pipfile index 0e728cc..b51a41c 100644 --- a/Pipfile +++ b/Pipfile @@ -25,6 +25,7 @@ timing-asgi = ">=0.2.1,<1" schema = ">=0.7.4,<1" toml = "*" pip = "*" +packaging = "*" [scripts] halfapi = "python -m halfapi" diff --git a/halfapi/half_domain.py b/halfapi/half_domain.py index b9559a2..9e5700c 100644 --- a/halfapi/half_domain.py +++ b/halfapi/half_domain.py @@ -3,6 +3,8 @@ import inspect import os import re +from packaging.specifiers import SpecifierSet +from packaging.version import Version from typing import Coroutine, Dict, Iterator, List, Tuple from types import ModuleType, FunctionType @@ -42,6 +44,8 @@ class HalfDomain(Starlette): # TODO: Check if given domain halfapi_version matches with __version__ self.halfapi_version = getattr(self.m_domain, '__halfapi_version__', __version__) + self.deps = getattr(self.m_domain, '__deps__', tuple()) + if not router: self.router = getattr('__router__', domain, '.routers') else: @@ -54,6 +58,18 @@ class HalfDomain(Starlette): self.config = { **app.config } logger.info('HalfDomain creation %s %s', domain, self.config) + + for elt in self.deps: + package, version = elt + specifier = SpecifierSet(version) + package_module = importlib.import_module(package) + if Version(package_module.__version__) not in specifier: + raise Exception( + 'Wrong version for package {} version {} (excepting {})'.format( + package, package_module.__version__, specifier + )) + + super().__init__( routes=self.gen_domain_routes(), middleware=[ diff --git a/halfapi/testing/test_domain.py b/halfapi/testing/test_domain.py index 5732a5f..b39f76b 100644 --- a/halfapi/testing/test_domain.py +++ b/halfapi/testing/test_domain.py @@ -3,6 +3,7 @@ import functools import os import sys import json +from json.decoder import JSONDecodeError from unittest import TestCase from starlette.testclient import TestClient from click.testing import CliRunner @@ -84,6 +85,11 @@ class TestDomain(TestCase): print(f'Stdout {result.stdout}') print(f'Stderr {result.stderr}') raise exc + except JSONDecodeError as exc: + print(f'Result {result}') + print(f'Stdout {result.stdout}') + raise exc + return result_d diff --git a/tests/dummy_domain/__init__.py b/tests/dummy_domain/__init__.py index fed7f0f..cfb9459 100644 --- a/tests/dummy_domain/__init__.py +++ b/tests/dummy_domain/__init__.py @@ -1,6 +1,12 @@ +from halfapi import __version__ as halfapi_version + __name__ = 'dummy_domain' __version__ = '0.0.0' __patch_release__ = '0.0.0' __routers__ = '.routers' __id__ = '8b88e60a625369235b36c2d6d70756a0c02c1c7fb169fcee6dc820bcf9723f5a' +__deps__ = ( + ('halfapi', '=={}'.format(halfapi_version)), +) +