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.
regisb committed Feb 12, 2024
1 parent ef2a157 commit 2a7bc16
Showing 5 changed files with 14 additions and 20 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)
33 changes: 13 additions & 20 deletions tutorjupyter/plugin.py
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
from glob import glob
from secrets import token_bytes

import pkg_resources
import importlib_resources
from tutor import hooks
from tutor.__about__ import __version_suffix__

@@ -66,14 +66,14 @@ def jupyterhub_crypt_key(size: int) -> str:
# INITIALIZATION TASKS
########################################

MY_INIT_TASKS: list[tuple[str, tuple[str, ...]]] = [
("mysql", ("jupyter", "jobs", "init", "mysql.sh")),
("jupyterhub", ("jupyter", "jobs", "init", "jupyterhub.sh")),
]

for service, template_path in MY_INIT_TASKS:
full_path: str = pkg_resources.resource_filename(
"tutorjupyter", os.path.join("templates", *template_path)
for service in ["mysql", "jupyterhub"]:
full_path: str = str(
importlib_resources.files("tutorjupyter")
/ "templates"
/ "jupyter"
/ "tasks"
/ service
/ "init"
)
with open(full_path, encoding="utf-8") as init_task_file:
init_task: str = init_task_file.read()
@@ -118,11 +118,9 @@ def jupyterhub_crypt_key(size: int) -> str:
# TEMPLATE RENDERING
########################################

hooks.Filters.ENV_TEMPLATE_ROOTS.add_items(
# Root paths for template files, relative to the project root.
[
pkg_resources.resource_filename("tutorjupyter", "templates"),
]
hooks.Filters.ENV_TEMPLATE_ROOTS.add_item(
# Root path for template files, relative to the project root.
str(importlib_resources.files("tutorjupyter") / "templates")
)

hooks.Filters.ENV_TEMPLATE_TARGETS.add_items(
@@ -138,11 +136,6 @@ def jupyterhub_crypt_key(size: int) -> str:

# For each file in tutorjupyter/patches,
# apply a patch based on the file's name and contents.
for path in glob(
os.path.join(
pkg_resources.resource_filename("tutorjupyter", "patches"),
"*",
)
):
for path in glob(str(importlib_resources.files("tutorjupyter") / "patches" / "*")):
with open(path, encoding="utf-8") as patch_file:
hooks.Filters.ENV_PATCHES.add_item((os.path.basename(path), patch_file.read()))
Empty file.

0 comments on commit 2a7bc16

Please sign in to comment.