Skip to content

Commit

Permalink
Merge pull request #2798 from hmpf/replace-pkgresources
Browse files Browse the repository at this point in the history
Replace pkg_resources with importlib
  • Loading branch information
hmpf authored Feb 27, 2024
2 parents 8ea18f5 + 9a871c9 commit de2a90b
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 20 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ classifiers = [
dynamic = ["version"]
dependencies = [
"pydantic >= 2.0",
"importlib_metadata; python_version < '3.8'",
"importlib_resources; python_version < '3.9'",
]


Expand Down
12 changes: 9 additions & 3 deletions python/nav/buildconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@
# pylint: disable=invalid-name
import os
import sysconfig
import pkg_resources

try:
from importlib import metadata as _impmeta
except ImportError:
import importlib_metadata as _impmeta


datadir = os.path.join(sysconfig.get_config_var('datarootdir'), 'nav')
localstatedir = os.path.join(datadir, 'var')
webrootdir = os.path.join(datadir, "www")
djangotmpldir = os.path.join(datadir, "templates")
docdir = os.path.join(datadir, "doc")


try:
VERSION = pkg_resources.get_distribution("nav").version
except pkg_resources.DistributionNotFound:
VERSION = _impmeta.version("nav")
except _impmeta.PackageNotFoundError:
# If we're not installed, try to get the current version from Git tags
import setuptools_scm

Expand Down
19 changes: 11 additions & 8 deletions python/nav/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
import pwd
import stat
import configparser
import pkg_resources
from pathlib import Path

from nav.errors import GeneralException
from nav.util import resource_files, resource_bytes
from . import buildconf

_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -247,13 +248,15 @@ def _config_resource_walk(source=''):
from available nav package resources. All paths returned will be relative to
the etc top directory.
"""
current_path = os.path.join('etc', source)
for name in pkg_resources.resource_listdir('nav', current_path):
full_name = os.path.join(current_path, name)
relative_name = os.path.join(source, name)
if pkg_resources.resource_isdir('nav', full_name):
source = Path(source)
current_path = Path('etc') / source
for path in resource_files('nav').joinpath(current_path).iterdir():
name = path.name
full_name = current_path / name
relative_name = str(source / name)
if resource_files('nav').joinpath(full_name).is_dir():
for path in _config_resource_walk(source=relative_name):
yield path
yield str(path)
else:
yield relative_name

Expand All @@ -272,7 +275,7 @@ def _install_single_config_resource_(source, target, overwrite=False):
if not overwrite and os.path.exists(target_file):
return False

content = pkg_resources.resource_string('nav', resource_path)
content = resource_bytes('nav', resource_path)
with open(target_file, 'wb') as handle:
handle.write(content)
return target_file
Expand Down
18 changes: 10 additions & 8 deletions python/nav/pgsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
import subprocess
from textwrap import wrap
from errno import ENOENT, EACCES
from pathlib import Path
import psycopg2
from pkg_resources import resource_listdir, resource_string

from nav.db import ConnectionParameters
from nav.colors import colorize, print_color
from nav.colors import COLOR_CYAN, COLOR_YELLOW, COLOR_RED, COLOR_GREEN
from nav.util import resource_files, resource_bytes


def main():
Expand Down Expand Up @@ -546,7 +547,7 @@ def execute_sql_file(self, filename):
print_color("OK", COLOR_GREEN)

def _read_sql_file(self, filename):
return resource_string(self.resource_module, filename)
return resource_bytes(self.resource_module, filename)


class ChangeScriptFinder(list):
Expand All @@ -562,12 +563,13 @@ def __init__(self, resource_module):
self._find_change_scripts()

def _find_change_scripts(self):
changes_dir = 'sql/changes'
scripts = [
os.path.join(changes_dir, f)
for f in resource_listdir(self.resource_module, changes_dir)
if self.script_pattern.match(f)
]
changes_dir = Path('sql/changes')
scripts = []
sql_path = resource_files(self.resource_module).joinpath(changes_dir)
for path in sql_path.iterdir():
filename = path.name
if self.script_pattern.match(str(filename)):
scripts.append(str(changes_dir / filename))
self[:] = scripts

def get_missing_changes(self, versions):
Expand Down
2 changes: 1 addition & 1 deletion python/nav/statemon/checker/RadiusChecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# License along with NAV. If not, see <http://www.gnu.org/licenses/>.
#
"""RADIUS service checker"""
from pkg_resources import resource_filename
from nav.util import resource_filename

# Python-radius specific modules. pyrad found at
# http://www.wiggy.net/code/pyrad/ by Wichert Akkermann
Expand Down
26 changes: 26 additions & 0 deletions python/nav/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@

import IPy

try:
from importlib.resources import as_file, files as resource_files
except ImportError: # Python 3.7!
from importlib_resources import as_file, files as resource_files


def gradient(start, stop, steps):
"""Create and return a sequence of steps representing an integer
Expand Down Expand Up @@ -509,3 +514,24 @@ def _range_to_str(x, y):
return str(x)
else:
return "{}-{}".format(x, y)


def resource_filename(package, filename):
"""Return the path of the filename as it is inside the package
package: either a dotted path to a module or a module object
filename: str or pathlib.Path
"""
ref = resource_files(package) / filename
with as_file(ref) as path:
return str(path)


def resource_bytes(package, filename):
"""Read and return a bytes-object of the filename found in the package
package: either a dotted path to a module or a module object
filename: str or pathlib.Path
"""
ref = resource_files(package) / filename
return ref.read_bytes()
2 changes: 2 additions & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ libsass==0.15.1
napalm==3.4.1

backports.zoneinfo ; python_version < '3.9'
importlib_metadata ; python_version < '3.8'
importlib_resources ; python_version < '3.9'
git+https://github.com/Uninett/[email protected]#egg=drf-oidc-auth

0 comments on commit de2a90b

Please sign in to comment.