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

Move away from deprecated pkg_resources #210

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 2 additions & 7 deletions src/correctionlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@

if sys.platform.startswith("win32"):
import ctypes
import os.path

import pkg_resources
from .util import this_module_path

ctypes.CDLL(
pkg_resources.resource_filename(
"correctionlib", os.path.join("lib", "correctionlib.dll")
)
)
ctypes.CDLL(str(this_module_path() / "lib" / "correctionlib.dll"))


from .binding import register_pyroot_binding
Expand Down
23 changes: 9 additions & 14 deletions src/correctionlib/binding.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
def register_pyroot_binding() -> None:
import os.path
import sys

import pkg_resources
from cppyy import gbl

from .util import this_module_path

base_path = this_module_path()
lib = base_path / "lib"

# maybe not the most robust solution?
if sys.platform.startswith("win32"):
lib = pkg_resources.resource_filename(
"correctionlib", os.path.join("lib", "correctionlib.dll")
)
lib = lib / "correctionlib.dll"
elif sys.platform.startswith("darwin"):
lib = pkg_resources.resource_filename(
"correctionlib", os.path.join("lib", "libcorrectionlib.dylib")
)
lib = lib / "libcorrectionlib.dylib"
else:
lib = pkg_resources.resource_filename(
"correctionlib", os.path.join("lib", "libcorrectionlib.so")
)
lib = lib / "libcorrectionlib.so"
gbl.gSystem.Load(lib)
gbl.gInterpreter.AddIncludePath(
pkg_resources.resource_filename("correctionlib", "include")
)
gbl.gInterpreter.AddIncludePath(base_path / "include")
gbl.gROOT.ProcessLine('#include "correction.h"')
15 changes: 7 additions & 8 deletions src/correctionlib/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,27 +130,26 @@ def setup_merge(subparsers: Any) -> None:


def config(console: Console, args: argparse.Namespace) -> int:
import pkg_resources
from .util import this_module_path

incdir = pkg_resources.resource_filename("correctionlib", "include")
libdir = pkg_resources.resource_filename("correctionlib", "lib")
base_dir = this_module_path()
incdir = base_dir / "include"
libdir = base_dir / "lib"
out = []
if args.version:
out.append(correctionlib.version.version)
if args.incdir:
out.append(incdir)
out.append(str(incdir))
if args.cflags:
out.append(f"-std=c++17 -I{incdir}")
if args.libdir:
out.append(libdir)
out.append(str(libdir))
if args.ldflags:
out.append(f"-L{libdir} -lcorrectionlib")
if args.rpath:
out.append(f"-Wl,-rpath,{libdir}")
if args.cmake:
out.append(
f"-Dcorrectionlib_DIR={pkg_resources.resource_filename('correctionlib', 'cmake')}"
)
out.append(f"-Dcorrectionlib_DIR={base_dir / 'cmake'}")
console.out(" ".join(out), highlight=False)
return 0

Expand Down
15 changes: 15 additions & 0 deletions src/correctionlib/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pathlib
import sys


def this_module_path() -> pathlib.Path:
# TODO: this package could be a zipball, in which case these paths are temporary
# We could warn but there is an almost negligible chance this is the case
if sys.version_info < (3, 9):
# use deprecated API
import pkg_resources

return pathlib.Path(pkg_resources.resource_filename("correctionlib", ""))
import importlib.resources

return importlib.resources.files("correctionlib")
Loading