diff --git a/Makefile b/Makefile index 6da911a0f2..2d5ebda7e9 100644 --- a/Makefile +++ b/Makefile @@ -68,7 +68,7 @@ deps-test: # (Re)install the tool install: - $(PIP) install -U pip wheel + $(PIP) install -U pip wheel setuptools fastentrypoints for mod in $(BUILD_ORDER);do (cd $$mod ; $(PIP_INSTALL) .);done # Install with pip install -e diff --git a/ocrd/ocrd/constants.py b/ocrd/ocrd/constants.py index 1d436a7fa9..2e9c17c649 100644 --- a/ocrd/ocrd/constants.py +++ b/ocrd/ocrd/constants.py @@ -1,7 +1,7 @@ """ Constants for ocrd. """ -from pkg_resources import resource_filename +from ocrd_utils.package_resources import resource_filename __all__ = [ 'TMP_PREFIX', diff --git a/ocrd/ocrd/processor/builtin/dummy_processor.py b/ocrd/ocrd/processor/builtin/dummy_processor.py index 9a1ad511e7..cce37a7430 100644 --- a/ocrd/ocrd/processor/builtin/dummy_processor.py +++ b/ocrd/ocrd/processor/builtin/dummy_processor.py @@ -1,6 +1,6 @@ # pylint: disable=missing-module-docstring,invalid-name from os.path import join, basename -from pkg_resources import resource_string +from ocrd_utils.package_resources import resource_string import click @@ -17,7 +17,7 @@ ) from ocrd_modelfactory import page_from_file -OCRD_TOOL = parse_json_string_with_comments(resource_string(__name__, 'dummy/ocrd-tool.json').decode('utf8')) +OCRD_TOOL = parse_json_string_with_comments(resource_string(__name__, 'ocrd-tool.json').decode('utf8')) class DummyProcessor(Processor): """ diff --git a/ocrd/ocrd/processor/builtin/dummy/ocrd-tool.json b/ocrd/ocrd/processor/builtin/ocrd-tool.json similarity index 100% rename from ocrd/ocrd/processor/builtin/dummy/ocrd-tool.json rename to ocrd/ocrd/processor/builtin/ocrd-tool.json diff --git a/ocrd/ocrd/workspace_bagger.py b/ocrd/ocrd/workspace_bagger.py index 28ae155b9a..ac215fa7e7 100644 --- a/ocrd/ocrd/workspace_bagger.py +++ b/ocrd/ocrd/workspace_bagger.py @@ -6,8 +6,6 @@ import re import tempfile import sys - -from pkg_resources import get_distribution from bagit import Bag, make_manifests # pylint: disable=no-name-in-module from ocrd_utils import ( @@ -22,6 +20,7 @@ from ocrd_validators.constants import BAGIT_TXT, TMP_BAGIT_PREFIX, OCRD_BAGIT_PROFILE_URL from ocrd_modelfactory import page_from_file from ocrd_models.ocrd_page import to_xml +from ocrd_utils.package_resources import get_distribution from .workspace import Workspace diff --git a/ocrd/requirements.txt b/ocrd/requirements.txt index 2da0163b74..ca62ed9370 100644 --- a/ocrd/requirements.txt +++ b/ocrd/requirements.txt @@ -7,4 +7,4 @@ opencv-python-headless Flask jsonschema pyyaml -Deprecated == 1.2.0 +Deprecated == 1.2.0 \ No newline at end of file diff --git a/ocrd/setup.py b/ocrd/setup.py index 0c8c0fa2ae..0269893e28 100644 --- a/ocrd/setup.py +++ b/ocrd/setup.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import fastentrypoints from setuptools import setup, find_packages from ocrd_utils import VERSION diff --git a/ocrd_models/ocrd_models/constants.py b/ocrd_models/ocrd_models/constants.py index 6c8b0e1017..b3fe89a4c9 100644 --- a/ocrd_models/ocrd_models/constants.py +++ b/ocrd_models/ocrd_models/constants.py @@ -1,7 +1,7 @@ """ Constants for ocrd_models. """ -from pkg_resources import resource_string +from ocrd_utils.package_resources import resource_string import re __all__ = [ diff --git a/ocrd_utils/ocrd_utils/constants.py b/ocrd_utils/ocrd_utils/constants.py index 121e5df612..1164025081 100644 --- a/ocrd_utils/ocrd_utils/constants.py +++ b/ocrd_utils/ocrd_utils/constants.py @@ -1,11 +1,12 @@ """ Constants for ocrd_utils. """ -from pkg_resources import get_distribution from re import compile as regex_compile from os import environ from os.path import join, expanduser +from ocrd_utils.package_resources import get_distribution + __all__ = [ 'EXT_TO_MIME', 'LOG_FORMAT', diff --git a/ocrd_utils/ocrd_utils/package_resources.py b/ocrd_utils/ocrd_utils/package_resources.py new file mode 100644 index 0000000000..ee01d046f4 --- /dev/null +++ b/ocrd_utils/ocrd_utils/package_resources.py @@ -0,0 +1,50 @@ +import atexit +from contextlib import ExitStack +from pathlib import Path + +try: + from importlib.resources import path, read_binary +except ImportError: + from importlib_resources import path, read_binary # type: ignore + +try: + from importlib.metadata import distribution as get_distribution +except ImportError: + from importlib_metadata import distribution as get_distribution + +# See https://importlib-resources.readthedocs.io/en/latest/migration.html#pkg-resources-resource-filename +_file_manager = ExitStack() +atexit.register(_file_manager.close) + + +def resource_filename(package: str, resource: str) -> Path: + """ + Reimplementation of the function with the same name from pkg_resources + + Using importlib for better performance + + package : str + The package from where to start looking for resource (often __name__) + resource : str + The resource to look up + """ + parent_package = package.rsplit('.',1)[0] + return _file_manager.enter_context(path(parent_package, resource)) + + +def resource_string(package: str, resource: str) -> bytes: + """ + Reimplementation of the function with the same name from pkg_resources + + Using importlib for better performance + + package : str + The package from where to start looking for resource (often __name__) + resource : str + The resource to look up + """ + parent_package = package.rsplit('.',1)[0] + return read_binary(parent_package, resource) + + +__all__ = ['resource_filename', 'resource_string', 'get_distribution'] diff --git a/ocrd_utils/requirements.txt b/ocrd_utils/requirements.txt index 300ed90949..de4e7adee3 100644 --- a/ocrd_utils/requirements.txt +++ b/ocrd_utils/requirements.txt @@ -3,3 +3,5 @@ Pillow >= 7.2.0 # tensorflow versions might require different versions numpy atomicwrites >= 1.3.0 +importlib_metadata;python_version<'3.8' +importlib_resources;python_version<'3.8' diff --git a/ocrd_validators/ocrd_validators/constants.py b/ocrd_validators/ocrd_validators/constants.py index 25d2e0e53b..5497102f25 100644 --- a/ocrd_validators/ocrd_validators/constants.py +++ b/ocrd_validators/ocrd_validators/constants.py @@ -2,7 +2,7 @@ Constants for ocrd_validators. """ import yaml -from pkg_resources import resource_string, resource_filename +from ocrd_utils.package_resources import resource_string, resource_filename __all__ = [ 'OCRD_TOOL_SCHEMA',