Skip to content

Commit

Permalink
Remove use of deprecated pkg_resources
Browse files Browse the repository at this point in the history
Ref. eng/recordflux/RecordFlux#1347
  • Loading branch information
treiher committed Jul 14, 2023
1 parent bb9e0de commit 7daa022
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 58 deletions.
2 changes: 0 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ stages:
.setup_python_venv: &setup_python_venv
- python$PYTHON_VERSION -m venv --clear .venv$PYTHON_VERSION
- . .venv$PYTHON_VERSION/bin/activate
- python -m pip install --upgrade pip wheel
- make init install

setup:
Expand Down Expand Up @@ -101,7 +100,6 @@ setup:
- if [ $CLEAN_RECORDFLUX_SETUP -eq 1 ]; then
python$PYTHON_VERSION -m venv --clear .venv;
. .venv/bin/activate;
python -m pip install --upgrade pip wheel;
make init install;
else
. .venv$PYTHON_VERSION/bin/activate;
Expand Down
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ $(GNATPROVE_CACHE_DIR):
.PHONY: install_build_deps install install_devel upgrade_devel install_devel_edge install_git_hooks install_gnat printenv_gnat

install_build_deps:
tools/check_pip_version.py
pip install setuptools_scm wheel build contrib/langkit
pip3 install -U pip>=22.2
pip3 install packaging setuptools_scm wheel build contrib/langkit

install: install_build_deps $(SDIST)
$(MAKE) -C devutils install_devel
Expand All @@ -227,9 +227,6 @@ install_devel: install_build_deps parser
$(MAKE) -C devutils install_devel
pip3 install --force-reinstall -e ".[devel]" --config-settings editable_mode=strict

upgrade_devel:
tools/upgrade_dependencies.py

install_devel_edge: install_devel
$(MAKE) -C devutils install_devel_edge

Expand Down
1 change: 0 additions & 1 deletion doc/development_guide/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ Make targets for common development tasks are:
- ``prove`` Run GNATprove on SPARK tests and example apps
- ``format`` Perform automatic code formatting on Python code
- ``install_devel`` Install project in editable mode
- ``upgrade_devel`` Upgrade all development dependencies (note: ``install_devel`` must be executed before changes in ``setup.py`` take effect)
- ``doc`` Generate HTML documentation
- ``dist`` Create Python package
- ``clean`` Remove all generated files (note: this will also remove the editable installation, so the project must be reinstalled using ``install_devel`` afterwards)
Expand Down
4 changes: 2 additions & 2 deletions rflx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from pkg_resources import get_distribution
from importlib.metadata import version

__version__ = get_distribution("RecordFlux").version
__version__ = version("RecordFlux")
13 changes: 8 additions & 5 deletions rflx/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
import sys
import traceback
from collections.abc import Sequence
from importlib import metadata
from multiprocessing import cpu_count
from pathlib import Path
from typing import Optional, Union

from pkg_resources import get_distribution, resource_filename
import importlib_resources

from rflx import __version__
from rflx.converter import iana
Expand Down Expand Up @@ -291,9 +292,10 @@ def main( # noqa: PLR0915

def version() -> str:
dependencies = [
f"{r.project_name} {get_distribution(r.project_name).version}"
for r in get_distribution("RecordFlux").requires()
if not r.project_name.startswith("RecordFlux")
f"{name} {metadata.version(name)}"
for r in metadata.requires("RecordFlux") or []
if not r.startswith("RecordFlux") and "; extra == " not in r
for name in [r.split()[0]]
]
return "\n".join(
[
Expand Down Expand Up @@ -444,7 +446,8 @@ def validate(args: argparse.Namespace) -> None:


def setup(args: argparse.Namespace) -> None:
gnatstudio_dir = resource_filename("rflx_ide", "gnatstudio/")
# TODO(eng/recordflux/RecordFlux#1359): Replace importlib_resources by importlib.resources
gnatstudio_dir = importlib_resources.files("rflx_ide") / "gnatstudio"
plugins_dir = args.gnat_studio_dir / "plug-ins"
if not plugins_dir.exists():
plugins_dir.mkdir(parents=True, exist_ok=True)
Expand Down
8 changes: 7 additions & 1 deletion rflx/generator/const.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from pathlib import Path
from typing import Final

import importlib_resources

from rflx import ada
from rflx.common import file_name
from rflx.identifier import ID
Expand Down Expand Up @@ -41,7 +46,8 @@
]
]

TEMPLATE_DIR = ("rflx", "templates/")
# TODO(eng/recordflux/RecordFlux#1359): Replace importlib_resources by importlib.resources
TEMPLATE_DIR: Final[Path] = importlib_resources.files("rflx") / "templates"

TYPES = TYPES_PACKAGE
TYPES_BYTE = TYPES * "Byte"
Expand Down
4 changes: 1 addition & 3 deletions rflx/generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from pathlib import Path
from typing import Optional

import pkg_resources

from rflx import __version__, expression as expr
from rflx.ada import (
FALSE,
Expand Down Expand Up @@ -131,7 +129,7 @@ def __init__(
self._debug = debug
self._ignore_unsupported_checksum = ignore_unsupported_checksum
self._executor = ProcessPoolExecutor(max_workers=workers)
self._template_dir = Path(pkg_resources.resource_filename(*const.TEMPLATE_DIR))
self._template_dir = const.TEMPLATE_DIR
assert self._template_dir.is_dir(), "template directory not found"

def generate(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def run(self) -> None:
install_requires=[
"attrs >=20, <22",
"icontract >=2.3.4, <3",
"importlib_resources >=6, <7", # TODO(eng/recordflux/RecordFlux#1359): Remove
"pydantic >=1, <2",
"pydotplus >=2, <3",
"ruamel.yaml >=0.17, <0.18",
Expand All @@ -105,7 +106,6 @@ def run(self) -> None:
"sphinx >=4.5, <5",
"sphinx-rtd-theme >= 1.1.1, <1.2",
"tqdm >=4.61.1, <4.63",
"types-pkg_resources >=0.1.3, <0.2",
]
},
scripts=["bin/rflx"],
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/generator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from dataclasses import dataclass
from pathlib import Path

import pkg_resources
import pytest
from _pytest.capture import CaptureFixture
from _pytest.monkeypatch import MonkeyPatch
Expand Down Expand Up @@ -129,14 +128,14 @@ def test_generate( # noqa: PLR0913

@pytest.mark.skipif(not __debug__, reason="depends on assertion")
def test_generate_missing_template_directory(monkeypatch: MonkeyPatch, tmp_path: Path) -> None:
monkeypatch.setattr(pkg_resources, "resource_filename", lambda *x: "non-existent directory")
monkeypatch.setattr(const, "TEMPLATE_DIR", tmp_path / "non-existent directory")
with pytest.raises(AssertionError, match="^template directory not found"):
Generator().generate(Model(), Integration(), tmp_path)


@pytest.mark.skipif(not __debug__, reason="depends on assertion")
def test_generate_missing_template_files(monkeypatch: MonkeyPatch, tmp_path: Path) -> None:
monkeypatch.setattr(pkg_resources, "resource_filename", lambda *x: tmp_path)
monkeypatch.setattr(const, "TEMPLATE_DIR", tmp_path)
with pytest.raises(AssertionError, match="^template file not found"):
Generator().generate(Model(), Integration(), tmp_path)

Expand Down
20 changes: 12 additions & 8 deletions tools/check_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@
"""Check that all development dependencies of RecordFlux are installed."""

import sys
from importlib.metadata import PackageNotFoundError, requires, version

from pkg_resources import DistributionNotFound, get_distribution
from packaging.markers import Marker
from packaging.requirements import Requirement


def check_dependencies() -> bool:
result = True

requirements = get_distribution("RecordFlux").requires(extras=("devel",))
for r in requirements:
for requirement in requires("RecordFlux") or []:
r = Requirement(requirement)
if r.marker != Marker('extra == "devel"'):
continue
try:
pkg = get_distribution(r.project_name)
if pkg not in r:
print(f"{r.project_name} has version {pkg.version}, should be {r}")
installed_version = version(r.name)
if installed_version not in r.specifier:
print(f"{r.name} has version {installed_version}, should be {r.specifier}")
result = False
except DistributionNotFound:
print(f"{r.project_name} not found")
except PackageNotFoundError:
print(f"{r.name} not found")
result = False

return result
Expand Down
17 changes: 0 additions & 17 deletions tools/check_pip_version.py

This file was deleted.

10 changes: 0 additions & 10 deletions tools/upgrade_dependencies.py

This file was deleted.

0 comments on commit 7daa022

Please sign in to comment.