Skip to content

Commit

Permalink
Remove pkg_resources usage (#2505)
Browse files Browse the repository at this point in the history
Close #2491 

Similar to dib-lab/screed#100 but also dealing
with `entry_points` (and the differences in stdlib for
[`importlib.metadata`](https://pypi.org/project/backports.entry-points-selectable/)...)
  • Loading branch information
luizirber authored Mar 5, 2023
1 parent ba03b21 commit e9a7f1f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 31 deletions.
4 changes: 2 additions & 2 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
from pkg_resources import get_distribution
release = get_distribution('sourmash').version
from importlib.metadata import version
release = version('sourmash')
version = '.'.join(release.split('.')[:2])

# The language for content autogenerated by Sphinx. Refer to documentation
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ dependencies = [
"deprecation>=2.0.6",
"cachetools>=4,<6",
"bitstring>=3.1.9,<5",
"importlib_metadata;python_version<'3.10'"
"importlib_metadata>=3.6;python_version<'3.10'"
]

requires-python = ">=3.8"
Expand Down Expand Up @@ -144,7 +144,7 @@ features = ["maturin"]
locked = true

[tool.isort]
known_third_party = ["deprecation", "hypothesis", "mmh3", "numpy", "pkg_resources", "pytest", "screed", "setuptools", "sourmash_tst_utils"]
known_third_party = ["deprecation", "hypothesis", "mmh3", "numpy", "pytest", "screed", "sourmash_tst_utils"]
multi_line_output = 3
include_trailing_comma = true
force_grid_wrap = 0
Expand Down
15 changes: 2 additions & 13 deletions src/sourmash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class MinHash - hash sketch class
BSD 3-Clause license.
"""
from deprecation import deprecated
from importlib.metadata import version

__all__ = ['MinHash', 'SourmashSignature',
'load_one_signature',
Expand All @@ -33,19 +34,7 @@ class MinHash - hash sketch class

ffi.init_once(lib.sourmash_init, "init")

from pkg_resources import get_distribution, DistributionNotFound

try:
VERSION = get_distribution(__name__).version
except DistributionNotFound: # pragma: no cover
try:
from .version import version as VERSION # noqa
except ImportError: # pragma: no cover
raise ImportError(
"Failed to find (autogenerated) version.py. "
"This might be because you are installing from GitHub's tarballs, "
"use the PyPI ones."
)
VERSION = version(__name__)

from .minhash import MinHash, get_minhash_default_seed, get_minhash_max_hash

Expand Down
29 changes: 16 additions & 13 deletions tests/sourmash_tst_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@
import subprocess
import collections
import pprint

import pkg_resources
from pkg_resources import Requirement, resource_filename, ResolutionError
import traceback
from io import open # pylint: disable=redefined-builtin
from io import StringIO

from importlib import resources
from importlib.metadata import entry_points

# Remove when we drop support for 3.8
if sys.version_info < (3, 9):
import importlib_resources as resources

# Remove when we drop support for 3.9
if sys.version_info < (3, 10):
from importlib_metadata import entry_points


SIG_FILES = [os.path.join('demo', f) for f in (
"SRR2060939_1.sig", "SRR2060939_2.sig", "SRR2241509_1.sig",
Expand Down Expand Up @@ -44,9 +52,10 @@ def _runscript(scriptname):
namespace['sys'] = globals()['sys']

try:
pkg_resources.load_entry_point("sourmash", 'console_scripts', scriptname)()
(script,) = entry_points(name=scriptname, group="console_scripts")
script.load()()
return 0
except pkg_resources.ResolutionError:
except ValueError:
pass

path = scriptpath()
Expand Down Expand Up @@ -129,14 +138,8 @@ def runscript(scriptname, args, **kwargs):


def get_test_data(filename):
filepath = None
try:
filepath = resource_filename(
Requirement.parse("sourmash"), "sourmash/sourmash/test-data/"\
+ filename)
except ResolutionError:
pass
if not filepath or not os.path.isfile(filepath):
filepath = resources.files("sourmash") / "tests" / "test-data" / filename
if not filepath.exists() or not os.path.isfile(filepath):
filepath = os.path.join(os.path.dirname(__file__), 'test-data',
filename)
return filepath
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ commands =

[testenv:docs]
description = invoke sphinx-build to build the HTML docs
basepython = python3.8
basepython = python3.10
extras = doc
whitelist_externals = pandoc
passenv = HOME
Expand Down

0 comments on commit e9a7f1f

Please sign in to comment.