1
mirror of https://github.com/home-assistant/core synced 2024-07-27 18:58:57 +02:00

Whole-string match reqs in comment_requirement (#55192)

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
Ian 2021-08-30 10:20:02 -05:00 committed by GitHub
parent cbc68e45cd
commit de5a22953d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 15 deletions

View File

@ -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():

View File

@ -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)