From de5a22953d22fc22e3fa7a2656b3a72050c91dc0 Mon Sep 17 00:00:00 2001 From: Ian <83995775+TheBassEngineer@users.noreply.github.com> Date: Mon, 30 Aug 2021 10:20:02 -0500 Subject: [PATCH] Whole-string match reqs in comment_requirement (#55192) Co-authored-by: Franck Nijhof Co-authored-by: Paulus Schoutsen --- script/gen_requirements_all.py | 23 ++++++++++++++++++++++- script/hassfest/requirements.py | 16 ++-------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/script/gen_requirements_all.py b/script/gen_requirements_all.py index f535958412d..6581c89bc63 100755 --- a/script/gen_requirements_all.py +++ b/script/gen_requirements_all.py @@ -45,6 +45,10 @@ COMMENT_REQUIREMENTS = ( "VL53L1X2", ) +COMMENT_REQUIREMENTS_NORMALIZED = { + commented.lower().replace("_", "-") for commented in COMMENT_REQUIREMENTS +} + IGNORE_PIN = ("colorlog>2.1,<3", "urllib3") URL_PIN = ( @@ -108,6 +112,8 @@ IGNORE_PRE_COMMIT_HOOK_ID = ( "python-typing-update", ) +PACKAGE_REGEX = re.compile(r"^(?:--.+\s)?([-_\.\w\d]+).*==.+$") + def has_tests(module: str): """Test if a module has tests. @@ -171,9 +177,24 @@ def gather_recursive_requirements(domain, seen=None): return reqs +def normalize_package_name(requirement: str) -> str: + """Return a normalized package name from a requirement string.""" + # This function is also used in hassfest. + match = PACKAGE_REGEX.search(requirement) + if not match: + return "" + + # pipdeptree needs lowercase and dash instead of underscore as separator + package = match.group(1).lower().replace("_", "-") + + return package + + def comment_requirement(req): """Comment out requirement. Some don't install on all systems.""" - return any(ign.lower() in req.lower() for ign in COMMENT_REQUIREMENTS) + return any( + normalize_package_name(req) == ign for ign in COMMENT_REQUIREMENTS_NORMALIZED + ) def gather_modules(): diff --git a/script/hassfest/requirements.py b/script/hassfest/requirements.py index f72562f7f2f..4d111265b1e 100644 --- a/script/hassfest/requirements.py +++ b/script/hassfest/requirements.py @@ -15,7 +15,7 @@ from tqdm import tqdm from homeassistant.const import REQUIRED_PYTHON_VER import homeassistant.util.package as pkg_util -from script.gen_requirements_all import COMMENT_REQUIREMENTS +from script.gen_requirements_all import COMMENT_REQUIREMENTS, normalize_package_name from .model import Config, Integration @@ -48,18 +48,6 @@ IGNORE_VIOLATIONS = { } -def normalize_package_name(requirement: str) -> str: - """Return a normalized package name from a requirement string.""" - match = PACKAGE_REGEX.search(requirement) - if not match: - return "" - - # pipdeptree needs lowercase and dash instead of underscore as separator - package = match.group(1).lower().replace("_", "-") - - return package - - def validate(integrations: dict[str, Integration], config: Config): """Handle requirements for integrations.""" # Check if we are doing format-only validation. @@ -134,7 +122,7 @@ def validate_requirements(integration: Integration): f"Failed to normalize package name from requirement {req}", ) return - if package in IGNORE_PACKAGES: + if (package == ign for ign in IGNORE_PACKAGES): continue integration_requirements.add(req) integration_packages.add(package)