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
CodeWithEmad committed Feb 26, 2024
1 parent 97b316a commit 5d5241d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Please respect the following instructions:
## 2024-01-01

- [Improvement] Happy New Year!
- Fix compatibility issue with Python 3.12 by removing dependency on `pkg_resources`.
- Cookiecutter hooks to check input data validation.
- Various licenses support.
- New documentation format.
Expand Down
11 changes: 4 additions & 7 deletions docs/migrating-from-v0-plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ The plugin API was upgraded from ``v0`` to ``v1`` in `Tutor v13.2.0 <https://git
####### Boilerplate code
# Add the "templates" folder as a template root
hooks.Filters.ENV_TEMPLATE_ROOTS.add_item(
pkg_resources.resource_filename("tutoryourplugin", "templates")
str(importlib_resources.files("tutoryourplugin") / "templates")
)
# Render the "build" and "apps" folders
hooks.Filters.ENV_TEMPLATE_TARGETS.add_items(
Expand All @@ -94,10 +94,7 @@ The plugin API was upgraded from ``v0`` to ``v1`` in `Tutor v13.2.0 <https://git
)
# Load patches from files
for path in glob(
os.path.join(
pkg_resources.resource_filename("tutoryourplugin", "patches"),
"*",
)
str(importlib_resources.files("tutoryourplugin") / "patches" / "*")
):
with open(path, encoding="utf-8") as patch_file:
hooks.Filters.ENV_PATCHES.add_item((os.path.basename(path), patch_file.read()))
Expand All @@ -118,8 +115,8 @@ The plugin API was upgraded from ``v0`` to ``v1`` in `Tutor v13.2.0 <https://git
# For each task added to MY_INIT_TASKS, 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, priority in MY_INIT_TASKS:
full_path: str = pkg_resources.resource_filename(
"tutoryourplugin", os.path.join("templates", *template_path)
full_path: str = str(importlib_resources.files(
"tutoryourplugin") / os.path.join("templates", *template_path)
)
with open(full_path, encoding="utf-8") as init_task_file:
init_task: str = init_task_file.read()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from glob import glob

import click
import pkg_resources
import importlib_resources
from tutor import hooks

from .__about__ import __version__
Expand Down Expand Up @@ -63,8 +63,9 @@
# 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(
"{{ cookiecutter.module_name }}", os.path.join("templates", *template_path)
full_path: str = str(
importlib_resources.files("{{ cookiecutter.module_name }}")
/ os.path.join("templates", *template_path)
)
with open(full_path, encoding="utf-8") as init_task_file:
init_task: str = init_task_file.read()
Expand Down Expand Up @@ -131,7 +132,7 @@
hooks.Filters.ENV_TEMPLATE_ROOTS.add_items(
# Root paths for template files, relative to the project root.
[
pkg_resources.resource_filename("{{ cookiecutter.module_name }}", "templates"),
str(importlib_resources.files("{{ cookiecutter.module_name }}") / "templates"),
]
)

Expand All @@ -156,12 +157,7 @@

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

Expand Down

0 comments on commit 5d5241d

Please sign in to comment.