From 76ca89eec9ee478a57c21d2602de2689ac72cd74 Mon Sep 17 00:00:00 2001 From: Flynn Date: Thu, 27 Jun 2024 14:22:47 -0400 Subject: [PATCH] Switch away from setuptools over to importlibs for finding schemas and templates, so Python unit tests can run. Signed-off-by: Flynn --- build-aux/builder.mk | 2 +- python/ambassador/config/config.py | 7 ++++--- python/ambassador_diag/diagd.py | 11 +++++++---- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/build-aux/builder.mk b/build-aux/builder.mk index ea56825b01..c8da9207cd 100644 --- a/build-aux/builder.mk +++ b/build-aux/builder.mk @@ -155,7 +155,7 @@ pytest: python-integration-test-environment $(MAKE) pytest-run-tests PYTEST_ARGS="$$PYTEST_ARGS python/tests" .PHONY: pytest -pytest-unit-tests: +pytest-unit-tests: python-virtual-environment @printf "$(CYN)==> $(GRN)Running $(BLU)py$(GRN) unit tests$(END)\n" set -e; { \ . $(OSS_HOME)/venv/bin/activate; \ diff --git a/python/ambassador/config/config.py b/python/ambassador/config/config.py index 1412835870..dd1329871a 100644 --- a/python/ambassador/config/config.py +++ b/python/ambassador/config/config.py @@ -19,7 +19,7 @@ from typing import TYPE_CHECKING, Any, ClassVar, Dict, Iterable, List, Optional, Tuple, Union from typing import cast as typecast -from pkg_resources import Requirement, resource_filename +from importlib import resources as importlib_resources from ..resource import Resource from ..utils import RichStatus, dump_json, parse_bool @@ -104,9 +104,10 @@ def __init__(self, schema_dir_path: Optional[str] = None) -> None: self.logger = logging.getLogger("ambassador.config") if not schema_dir_path: - # Note that this "resource_filename" has to do with setuptool packages, not + # Note that this "importlib_resources" has to do with imported packages, not # with our ACResource class. - schema_dir_path = resource_filename(Requirement.parse("ambassador"), "schemas") + + schema_dir_path = importlib_resources.path("ambassador", "schemas") # Once here, we know that schema_dir_path cannot be None. assert that, for mypy's # benefit. diff --git a/python/ambassador_diag/diagd.py b/python/ambassador_diag/diagd.py index f18b0bdd2b..c93b30534f 100644 --- a/python/ambassador_diag/diagd.py +++ b/python/ambassador_diag/diagd.py @@ -34,6 +34,8 @@ from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type, Union from typing import cast as typecast +from importlib import resources as importlib_resources + import click import gunicorn.app.base import jsonpatch @@ -42,7 +44,6 @@ from flask import Flask, Response from flask import json as flask_json from flask import jsonify, render_template, request, send_from_directory -from pkg_resources import Requirement, resource_filename from prometheus_client import CollectorRegistry, Gauge, Info, ProcessCollector, generate_latest from pythonjsonlogger import jsonlogger from typing_extensions import NotRequired, TypedDict @@ -595,8 +596,10 @@ def check_cache(self) -> bool: def get_templates_dir(): res_dir = None try: - # this will fail when not in a distribution - res_dir = resource_filename(Requirement.parse("ambassador"), "templates") + # Note that this "importlib_resources" has to do with imported packages, not + # with our ACResource class. + + res_dir = importlib_resources.path("ambassador", "templates") except: pass @@ -975,7 +978,7 @@ def handle_events(): @app.route("/ambassador/v0/favicon.ico", methods=["GET"]) def favicon(): - template_path = resource_filename(Requirement.parse("ambassador"), "templates") + template_path = get_templates_dir() return send_from_directory(template_path, "favicon.ico")