Validate component usage (#23037)

* Update manifest validator

* Update circle

* Update text

* Typo

* fix link to codeowners

* Merge CODEOWNERS into hassfest

* Annotate errors with fixable

* Convert error to warning

* Lint

* Make abs path

* Python 3.5...

* Typo

* Fix tests
This commit is contained in:
Paulus Schoutsen 2019-04-13 13:17:01 -07:00 committed by GitHub
parent fc481133e7
commit e8343452cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 415 additions and 220 deletions

View file

@ -0,0 +1,65 @@
"""Validate dependencies."""
import pathlib
import re
from typing import Set, Dict
from .model import Integration
def grep_dir(path: pathlib.Path, glob_pattern: str, search_pattern: str) \
-> Set[str]:
"""Recursively go through a dir and it's children and find the regex."""
pattern = re.compile(search_pattern)
found = set()
for fil in path.glob(glob_pattern):
if not fil.is_file():
continue
for match in pattern.finditer(fil.read_text()):
found.add(match.groups()[0])
return found
# These components will always be set up
ALLOWED_USED_COMPONENTS = {
'persistent_notification',
}
def validate_dependencies(integration: Integration):
"""Validate all dependencies."""
# Find usage of hass.components
referenced = grep_dir(integration.path, "**/*.py",
r"hass\.components\.(\w+)")
referenced -= ALLOWED_USED_COMPONENTS
referenced -= set(integration.manifest['dependencies'])
if referenced:
for domain in sorted(referenced):
print("Warning: {} references integration {} but it's not a "
"dependency".format(integration.domain, domain))
# Not enforced yet.
# integration.add_error(
# 'dependencies',
# "Using component {} but it's not a dependency".format(domain)
# )
def validate(integrations: Dict[str, Integration], config):
"""Handle dependencies for integrations."""
# check for non-existing dependencies
for integration in integrations.values():
if not integration.manifest:
continue
validate_dependencies(integration)
# check that all referenced dependencies exist
for dep in integration.manifest['dependencies']:
if dep not in integrations:
integration.add_error(
'dependencies',
"Dependency {} does not exist"
)