Skip to content

Commit

Permalink
Switch away from setuptools over to importlibs for finding schemas an…
Browse files Browse the repository at this point in the history
…d templates, so Python unit tests can run.

Signed-off-by: Flynn <[email protected]>
  • Loading branch information
kflynn committed Jun 27, 2024
1 parent f4e703a commit 76ca89e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build-aux/builder.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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; \
Expand Down
7 changes: 4 additions & 3 deletions python/ambassador/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 7 additions & 4 deletions python/ambassador_diag/diagd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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")

Expand Down

0 comments on commit 76ca89e

Please sign in to comment.