diff --git a/changelog.d/20240212_115536_regis_pkg_resources.md b/changelog.d/20240212_115536_regis_pkg_resources.md new file mode 100644 index 0000000..35b6d20 --- /dev/null +++ b/changelog.d/20240212_115536_regis_pkg_resources.md @@ -0,0 +1 @@ +- [Bugfix] Make plugin compatible with Python 3.12 by removing dependency on `pkg_resources`. (by @regisb) diff --git a/tutorjupyter/plugin.py b/tutorjupyter/plugin.py index 54e0bf1..638ffa8 100644 --- a/tutorjupyter/plugin.py +++ b/tutorjupyter/plugin.py @@ -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())) diff --git a/tutorjupyter/templates/jupyter/jobs/init/.gitignore b/tutorjupyter/templates/jupyter/jobs/init/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/tutorjupyter/templates/jupyter/jobs/init/jupyterhub.sh b/tutorjupyter/templates/jupyter/tasks/jupyterhub/init similarity index 100% rename from tutorjupyter/templates/jupyter/jobs/init/jupyterhub.sh rename to tutorjupyter/templates/jupyter/tasks/jupyterhub/init diff --git a/tutorjupyter/templates/jupyter/jobs/init/mysql.sh b/tutorjupyter/templates/jupyter/tasks/mysql/init similarity index 100% rename from tutorjupyter/templates/jupyter/jobs/init/mysql.sh rename to tutorjupyter/templates/jupyter/tasks/mysql/init