Skip to content

Commit

Permalink
Move away from deprecated pkg_resources (#210)
Browse files Browse the repository at this point in the history
* Move away from deprecated pkg_resources

I followed the migration guide:
https://importlib-resources.readthedocs.io/en/latest/migration.html.

Unfortunately the new APIs are only available as of Python 3.9.
There are other ones, also in `importlib.resources`, that are
available since 3.7 but are deprecated and 3.11.

* Use deprecated pkg_resources on old python versions

---------

Co-authored-by: Nick Smith <nick.smith@cern.ch>
  • Loading branch information
eguiraud and nsmith- authored Feb 2, 2024
1 parent 2ab0000 commit 9b96ada
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 29 deletions.
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")

0 comments on commit 9b96ada

Please sign in to comment.