Skip to content

Commit

Permalink
fix: remove pkg_resources for compatibility with python 3.12
Browse files Browse the repository at this point in the history
pkg_resources is a package that is unavailable in python 3.12, unless
setuptools is explicitely installed. Turns out, there are replacement
functions coming from importlib_resources, which can be obtained from
the importlib-resources pypi package. This package will be installed
with tutor starting from 17.0.2.
  • Loading branch information
regisb committed Feb 12, 2024
1 parent 02c7719 commit d1c67f4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
1 change: 1 addition & 0 deletions changelog.d/20240212_115536_regis_pkg_resources.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- [Bugfix] Make plugin compatible with Python 3.12 by removing dependency on `pkg_resources`. (by @regisb)
41 changes: 14 additions & 27 deletions tutorxqueue/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
from typing import Any, Literal, Optional, Union

import click
import pkg_resources
import importlib_resources
import requests # type: ignore
from tutor import config as tutor_config
from tutor.__about__ import __version_suffix__
from tutor import exceptions
from tutor import hooks as tutor_hooks
from tutor.__about__ import __version_suffix__
Expand Down Expand Up @@ -39,22 +38,17 @@
}

# Initialization hooks

# To add a custom initialization task, create a bash script template under:
# tutorxqueue/templates/xqueue/tasks/
# and then add it to the MY_INIT_TASKS list. Each task is in the format:
# ("<service>", ("<path>", "<to>", "<script>", "<template>"))
MY_INIT_TASKS: list[tuple[str, tuple[str, ...]]] = [
("mysql", ("xqueue", "tasks", "mysql", "init")),
("xqueue", ("xqueue", "tasks", "xqueue", "init")),
]

# For each task added to MY_INIT_TASKS, we load the task template
# For each service that needs to be initialized, we load the task template
# and add it to the CLI_DO_INIT_TASKS filter, which tells Tutor to
# run it as part of the `init` job.
for service, template_path in MY_INIT_TASKS:
full_path: str = pkg_resources.resource_filename(
"tutorxqueue", os.path.join("templates", *template_path)
for service in ["mysql", "xqueue"]:
full_path: str = str(
importlib_resources.files("tutorxqueue")
/ "templates"
/ "xqueue"
/ "tasks"
/ service
/ "init"
)
with open(full_path, encoding="utf-8") as init_task_file:
init_task: str = init_task_file.read()
Expand Down Expand Up @@ -185,7 +179,7 @@ def __init__(self, user_config: dict[str, Any], url: str = "") -> None:
if not self.base_url:
scheme = "https" if user_config["ENABLE_HTTPS"] else "http"
host = user_config["XQUEUE_HOST"]
self.base_url = "{}://{}".format(scheme, host)
self.base_url = f"{scheme}://{host}"
self.login()

@property
Expand All @@ -208,9 +202,7 @@ def login(self) -> None:
message = response.get("content")
if message != "Logged in":
raise exceptions.TutorError(
"Could not login to xqueue server at {}. Response: '{}'".format(
self.base_url, message
)
f"Could not login to xqueue server at {self.base_url}. Response: '{message}'"
)

def show_submission(self, queue: str) -> Union[dict[str, Any], Any]:
Expand Down Expand Up @@ -280,7 +272,7 @@ def request(

# Add the "templates" folder as a template root
tutor_hooks.Filters.ENV_TEMPLATE_ROOTS.add_item(
pkg_resources.resource_filename("tutorxqueue", "templates")
str(importlib_resources.files("tutorxqueue") / "templates")
)
# Render the "build" and "apps" folders
tutor_hooks.Filters.ENV_TEMPLATE_TARGETS.add_items(
Expand All @@ -290,12 +282,7 @@ def request(
],
)
# Load patches from files
for path in glob(
os.path.join(
pkg_resources.resource_filename("tutorxqueue", "patches"),
"*",
)
):
for path in glob(str(importlib_resources.files("tutorxqueue") / "patches" / "*")):
with open(path, encoding="utf-8") as patch_file:
tutor_hooks.Filters.ENV_PATCHES.add_item(
(os.path.basename(path), patch_file.read())
Expand Down

0 comments on commit d1c67f4

Please sign in to comment.