From b2e0af540e0412f586458f2a7138261065d3ae45 Mon Sep 17 00:00:00 2001 From: John Andersen Date: Fri, 21 Jun 2024 00:21:06 +0000 Subject: [PATCH] feat: no entrypoint registration required in tree Signed-off-by: John Andersen --- cve_bin_tool/checkers/__init__.py | 16 ++++++++++++++++ cve_bin_tool/version_scanner.py | 10 ++-------- setup.py | 20 -------------------- test/test_checkers.py | 12 +++++------- 4 files changed, 23 insertions(+), 35 deletions(-) diff --git a/cve_bin_tool/checkers/__init__.py b/cve_bin_tool/checkers/__init__.py index d9d1d59443..91b6287a83 100644 --- a/cve_bin_tool/checkers/__init__.py +++ b/cve_bin_tool/checkers/__init__.py @@ -6,10 +6,17 @@ import collections import re +import importlib +import sys from cve_bin_tool.error_handler import InvalidCheckerError from cve_bin_tool.util import regex_find +if sys.version_info >= (3, 9): + import importlib.resources as resources +else: + import importlib_resources as resources + __all__ = [ "Checker", "VendorProductPair", @@ -439,3 +446,12 @@ def get_version(self, lines, filename): ) return version_info + + +ALL_CHECKERS = { + checker_path.stem: importlib.import_module( + f"cve_bin_tool.checkers.{checker_path.stem}" + ).Checker + for checker_path in resources.files("cve_bin_tool.checkers").iterdir() + if (checker_path.suffix == ".py" and not checker_path.name.startswith("__")) +} diff --git a/cve_bin_tool/version_scanner.py b/cve_bin_tool/version_scanner.py index 3e617ff07d..63561d5d09 100644 --- a/cve_bin_tool/version_scanner.py +++ b/cve_bin_tool/version_scanner.py @@ -8,7 +8,7 @@ from pathlib import Path, PurePath from typing import Iterator -from cve_bin_tool.checkers import Checker +from cve_bin_tool.checkers import Checker, ALL_CHECKERS from cve_bin_tool.cvedb import CVEDB from cve_bin_tool.egg_updater import IS_DEVELOP, update_egg from cve_bin_tool.error_handler import ErrorMode @@ -84,13 +84,7 @@ def __init__( @classmethod def load_checkers(cls) -> dict[str, type[Checker]]: """Loads CVE checkers""" - checkers = dict( - map( - lambda checker: (checker.name, checker.load()), - importlib_metadata.entry_points().select(group=cls.CHECKER_ENTRYPOINT), - ) - ) - return checkers + return ALL_CHECKERS @classmethod def available_checkers(cls) -> list[str]: diff --git a/setup.py b/setup.py index 5f0ba969c6..93c3437410 100644 --- a/setup.py +++ b/setup.py @@ -69,26 +69,6 @@ "cve-bin-tool = cve_bin_tool.cli:main", "csv2cve = cve_bin_tool.csv2cve:main", ], - "cve_bin_tool.checker": [ - "{} = cve_bin_tool.checkers.{}:{}".format( - filename.replace(".py", ""), - filename.replace(".py", ""), - "".join( - (filename.replace(".py", "") + " checker") - .replace("_", " ") - .title() - .split() - ), - ) - for filename in os.listdir( - os.path.join( - os.path.abspath(os.path.dirname(__file__)), - "cve_bin_tool", - "checkers", - ) - ) - if filename.endswith(".py") and "__init__" not in filename - ], }, ) diff --git a/test/test_checkers.py b/test/test_checkers.py index 10ffb136ce..bb6442d167 100644 --- a/test/test_checkers.py +++ b/test/test_checkers.py @@ -5,10 +5,11 @@ import re import sys +import importlib import pytest -from cve_bin_tool.checkers import Checker, VendorProductPair +from cve_bin_tool.checkers import Checker, VendorProductPair, ALL_CHECKERS from cve_bin_tool.egg_updater import IS_DEVELOP, update_egg from cve_bin_tool.log import LOGGER @@ -17,6 +18,7 @@ else: import importlib_metadata + Pattern = type(re.compile("", 0)) @@ -136,12 +138,8 @@ def setup_class(cls): ) def test_filename_is(self, checker_name, file_name, expected_results): """Test a checker's filename detection""" - checkers = importlib_metadata.entry_points().select( - group="cve_bin_tool.checker" - ) - for checker in checkers: - if checker.name == checker_name: - Checker = checker.load() + for i_checker_name, Checker in ALL_CHECKERS.items(): + if i_checker_name == checker_name: checker = Checker() result = checker.get_version("", file_name)