Skip to content

Commit

Permalink
feat: no entrypoint registration required in tree
Browse files Browse the repository at this point in the history
Signed-off-by: John Andersen <[email protected]>
  • Loading branch information
John Andersen committed Jun 21, 2024
1 parent c5da274 commit b2e0af5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 35 deletions.
16 changes: 16 additions & 0 deletions cve_bin_tool/checkers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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("__"))
}
10 changes: 2 additions & 8 deletions cve_bin_tool/version_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]:
Expand Down
20 changes: 0 additions & 20 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
],
},
)

Expand Down
12 changes: 5 additions & 7 deletions test/test_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -17,6 +18,7 @@
else:
import importlib_metadata


Pattern = type(re.compile("", 0))


Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b2e0af5

Please sign in to comment.