[domain] checks versions specified in __deps__ variable of domain module

This commit is contained in:
Maxime Alves LIRMM 2022-03-21 09:45:35 +01:00
parent 63d6d1e8ea
commit b63b0f52c6
5 changed files with 30 additions and 4 deletions

View File

@ -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

View File

@ -25,6 +25,7 @@ timing-asgi = ">=0.2.1,<1"
schema = ">=0.7.4,<1"
toml = "*"
pip = "*"
packaging = "*"
[scripts]
halfapi = "python -m halfapi"

View File

@ -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=[

View File

@ -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

View File

@ -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)),
)