From 19dda28eed628417fac9f98193f91fe7ec0a9610 Mon Sep 17 00:00:00 2001 From: Kesara Rathnayake Date: Sun, 3 Nov 2024 12:20:44 +0000 Subject: [PATCH] fix: Use importlib --- ietf/__init__.py | 15 --------------- ietf/api/tests.py | 4 ++-- ietf/api/views.py | 9 ++++++++- ietf/settings.py | 2 ++ 4 files changed, 12 insertions(+), 18 deletions(-) diff --git a/ietf/__init__.py b/ietf/__init__.py index 950926ccf8..26124c3c67 100644 --- a/ietf/__init__.py +++ b/ietf/__init__.py @@ -35,21 +35,6 @@ __release_branch__ = branch __release_hash__ = git_hash -# important libraries -__version_extra__ = {} -# xml2rfc -try: - import xml2rfc - __version_extra__['xml2rfc'] = xml2rfc.__version__ -except Exception: - pass - -# weasyprint -try: - import weasyprint - __version_extra__['weasyprint'] = weasyprint.__version__ -except Exception: - pass # This will make sure the app is always imported when # Django starts so that shared_task will use this app. diff --git a/ietf/api/tests.py b/ietf/api/tests.py index 4eecde2fc1..d37871c6a9 100644 --- a/ietf/api/tests.py +++ b/ietf/api/tests.py @@ -936,8 +936,8 @@ def test_api_version(self): r = self.client.get(url) data = r.json() self.assertEqual(data['version'], ietf.__version__+ietf.__patch__) - self.assertEqual(data['other']['xml2rfc'], ietf.__version_extra__['xml2rfc']) - self.assertEqual(data['other']['weasyprint'], ietf.__version_extra__['weasyprint']) + for lib in settings.IMPORTANT_LIBRARIES: + self.assertIn(lib, data['other']) self.assertEqual(data['dumptime'], "2022-08-31 07:10:01 +0000") DumpInfo.objects.update(tz='PST8PDT') r = self.client.get(url) diff --git a/ietf/api/views.py b/ietf/api/views.py index 2b5c2d5993..e4b17d6f47 100644 --- a/ietf/api/views.py +++ b/ietf/api/views.py @@ -23,6 +23,7 @@ from django.views.decorators.gzip import gzip_page from django.views.generic.detail import DetailView from email.message import EmailMessage +from importlib.metadata import version as metadata_version from jwcrypto.jwk import JWK from tastypie.exceptions import BadRequest from tastypie.serializers import Serializer @@ -240,10 +241,16 @@ def version(request): if dumpinfo.tz != "UTC": dumpdate = pytz.timezone(dumpinfo.tz).localize(dumpinfo.date.replace(tzinfo=None)) dumptime = dumpdate.strftime('%Y-%m-%d %H:%M:%S %z') if dumpinfo else None + + # important libraries + __version_extra__ = {} + for lib in settings.IMPORTANT_LIBRARIES: + __version_extra__[lib] = metadata_version(lib) + return HttpResponse( json.dumps({ 'version': ietf.__version__+ietf.__patch__, - 'other': ietf.__version_extra__, + 'other': __version_extra__, 'dumptime': dumptime, }), content_type='application/json', diff --git a/ietf/settings.py b/ietf/settings.py index a1a7fee102..6e0923347e 100644 --- a/ietf/settings.py +++ b/ietf/settings.py @@ -1279,6 +1279,8 @@ def skip_unreadable_post(record): PUBLISH_IPR_STATES = ['posted', 'removed', 'removed_objfalse'] +IMPORTANT_LIBRARIES = ["django", "celery", "lxml", "markdown", "psycopg2", "pyang", "rfc2html", "weasyprint", "xml2rfc"] + # We provide a secret key only for test and development modes. It's # absolutely vital that django fails to start in production mode unless a # secret key has been provided elsewhere, not in this file which is