From fed6b57bbeefd3652aed6e8809a99cd4dbfa7bdd Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 22 Sep 2020 11:57:39 +0200 Subject: [PATCH] import __version__ instead of open file __init__ --- ereuse_devicehub/resources/versions/versions.py | 9 ++++----- setup.py | 7 ++----- tests/test_endpoints.py | 7 +++---- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/ereuse_devicehub/resources/versions/versions.py b/ereuse_devicehub/resources/versions/versions.py index bcdfca06..f2b8d393 100644 --- a/ereuse_devicehub/resources/versions/versions.py +++ b/ereuse_devicehub/resources/versions/versions.py @@ -1,4 +1,3 @@ -import re import flask import json import requests @@ -10,6 +9,7 @@ from flask import make_response, g from teal.resource import Resource, View from ereuse_devicehub.resources.inventory.model import Inventory +from ereuse_devicehub import __version__ def get_tag_version(app): @@ -31,10 +31,9 @@ def get_tag_version(app): class VersionView(View): def get(self, *args, **kwargs): """Get version of DeviceHub and ereuse-tag.""" - with open("ereuse_devicehub/__init__.py", encoding="utf8") as f: - dh_version = re.search(r'__version__ = "(.*?)"', f.read()).group(1) + tag_version = get_tag_version(self.resource_def.app) - versions = {'devicehub': dh_version, "ereuse_tag": "0.0.0"} + versions = {'devicehub': __version__, "ereuse_tag": "0.0.0"} versions.update(tag_version) return json.dumps(versions) @@ -58,7 +57,7 @@ class VersionDef(Resource): super().__init__(app, import_name, static_folder, static_url_path, template_folder, url_prefix, subdomain, url_defaults, root_path, cli_commands) - d = {'devicehub': '0.0.0'} + d = {'devicehub': __version__, "ereuse_tag": "0.0.0"} get = {'GET'} version_view = VersionView.as_view('VersionView', definition=self) diff --git a/setup.py b/setup.py index 618caa30..6a0db9b9 100644 --- a/setup.py +++ b/setup.py @@ -1,10 +1,7 @@ -import re - from pathlib import Path from setuptools import find_packages, setup +from ereuse_devicehub import __version__ -with open("ereuse_devicehub/__init__.py", encoding="utf8") as f: - version = re.search(r'__version__ = "(.*?)"', f.read()).group(1) test_requires = [ 'pytest', @@ -13,7 +10,7 @@ test_requires = [ setup( name='ereuse-devicehub', - version=version, + version=__version__, url='https://github.com/ereuse/devicehub-teal', project_urls={ 'Documentation': 'http://devicehub.ereuse.org', diff --git a/tests/test_endpoints.py b/tests/test_endpoints.py index eb38db9c..9c74a139 100644 --- a/tests/test_endpoints.py +++ b/tests/test_endpoints.py @@ -1,5 +1,4 @@ import datetime -import re import pytest from uuid import UUID @@ -13,6 +12,7 @@ from sqlalchemy.util import OrderedSet from teal.db import ResourceNotFound from teal.enums import Layouts +from ereuse_devicehub import __version__ from ereuse_devicehub.client import Client, UserClient from ereuse_devicehub.db import db from ereuse_devicehub.devicehub import Devicehub @@ -123,9 +123,8 @@ def test_get_version(app: Devicehub, client: Client): """Checks GETting versions of services.""" content, res = client.get("/versions/", None) - with open("ereuse_devicehub/__init__.py", encoding="utf8") as f: - dh_version = re.search(r'__version__ = "(.*?)"', f.read()).group(1) - version = {'devicehub': dh_version, 'ereuse_tag': '0.0.0'} + + version = {'devicehub': __version__, 'ereuse_tag': '0.0.0'} assert res.status_code == 200 assert content == version