Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MRG: switch to importlib.metadata from pkg_resources #546

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 8 additions & 37 deletions src/python/tests/sourmash_tst_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import collections
import pprint

import pkg_resources
from pkg_resources import Requirement, resource_filename, ResolutionError
import importlib.metadata
import traceback
from io import open # pylint: disable=redefined-builtin
from io import StringIO
Expand Down Expand Up @@ -61,46 +60,18 @@ def index_siglist(
return db


def scriptpath(scriptname="sourmash"):
"""Return the path to the scripts, in both dev and install situations."""
# note - it doesn't matter what the scriptname is here, as long as
# it's some script present in this version of sourmash.

path = os.path.join(os.path.dirname(__file__), "../")
if os.path.exists(os.path.join(path, scriptname)):
return path

path = os.path.join(os.path.dirname(__file__), "../../EGG-INFO/")
if os.path.exists(os.path.join(path, scriptname)):
return path

for path in os.environ["PATH"].split(":"):
if os.path.exists(os.path.join(path, scriptname)):
return path


def _runscript(scriptname):
"""Find & run a script with exec (i.e. not via os.system or subprocess)."""
namespace = {"__name__": "__main__"}
namespace["sys"] = globals()["sys"]

try:
pkg_resources.load_entry_point("sourmash", "console_scripts", scriptname)()
return 0
except pkg_resources.ResolutionError:
pass

path = scriptpath()

scriptfile = os.path.join(path, scriptname)
if os.path.isfile(scriptfile):
if os.path.isfile(scriptfile):
exec( # pylint: disable=exec-used
compile(open(scriptfile).read(), scriptfile, "exec"), namespace
)
return 0

return -1
entry_points = importlib.metadata.entry_points(
group="console_scripts", name="sourmash"
)
assert len(entry_points) == 1
smash_cli = tuple(entry_points)[0].load()
smash_cli()
return 0


ScriptResults = collections.namedtuple("ScriptResults", ["status", "out", "err"])
Expand Down
Loading