Add HACS, Themes
This commit is contained in:
37
custom_components/hacs/validate/README.md
Normal file
37
custom_components/hacs/validate/README.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# Repository validation
|
||||
|
||||
This is where the validation rules that run against the various repository categories live.
|
||||
|
||||
## Structure
|
||||
|
||||
- There is one file pr. rule.
|
||||
- All rule needs tests to verify every possible outcome for the rule.
|
||||
- It's better with multiple files than a big rule.
|
||||
- All rules uses `ValidationBase` or `ActionValidationBase` as the base class.
|
||||
- The `ActionValidationBase` are for checks that will breaks compatibility with with existing repositories (default), so these are only run in github actions.
|
||||
- Only use `validate` or `async_validate` methods to define validation rules.
|
||||
- If a rule should fail, raise `ValidationException` with the failure message.
|
||||
|
||||
|
||||
## Example
|
||||
|
||||
```python
|
||||
from .base import (
|
||||
ActionValidationBase,
|
||||
ValidationBase,
|
||||
ValidationException,
|
||||
)
|
||||
|
||||
|
||||
class AwesomeRepository(ValidationBase):
|
||||
def validate(self):
|
||||
if self.repository != "awesome":
|
||||
raise ValidationException("The repository is not awesome")
|
||||
|
||||
class SuperAwesomeRepository(ActionValidationBase):
|
||||
category = "integration"
|
||||
|
||||
async def async_validate(self):
|
||||
if self.repository != "super-awesome":
|
||||
raise ValidationException("The repository is not super-awesome")
|
||||
```
|
||||
1
custom_components/hacs/validate/__init__.py
Normal file
1
custom_components/hacs/validate/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Initialize validation."""
|
||||
58
custom_components/hacs/validate/base.py
Normal file
58
custom_components/hacs/validate/base.py
Normal file
@@ -0,0 +1,58 @@
|
||||
"""Base class for validation."""
|
||||
from __future__ import annotations
|
||||
|
||||
from time import monotonic
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from ..exceptions import HacsException
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..repositories.base import HacsRepository
|
||||
|
||||
|
||||
class ValidationException(HacsException):
|
||||
"""Raise when there is a validation issue."""
|
||||
|
||||
|
||||
class ValidationBase:
|
||||
"""Base class for validation."""
|
||||
|
||||
action_only: bool = False
|
||||
category: str = "common"
|
||||
|
||||
def __init__(self, repository: HacsRepository) -> None:
|
||||
self.hacs = repository.hacs
|
||||
self.repository = repository
|
||||
self.failed = False
|
||||
|
||||
@property
|
||||
def slug(self) -> str:
|
||||
"""Return the check slug."""
|
||||
return self.__class__.__module__.rsplit(".", maxsplit=1)[-1]
|
||||
|
||||
async def execute_validation(self, *_, **__) -> None:
|
||||
"""Execute the task defined in subclass."""
|
||||
self.hacs.log.debug("Validation<%s> Starting validation", self.slug)
|
||||
|
||||
start_time = monotonic()
|
||||
self.failed = False
|
||||
|
||||
try:
|
||||
if task := getattr(self, "validate", None):
|
||||
await self.hacs.hass.async_add_executor_job(task)
|
||||
elif task := getattr(self, "async_validate", None):
|
||||
await task() # pylint: disable=not-callable
|
||||
except ValidationException as exception:
|
||||
self.failed = True
|
||||
self.hacs.log.error("Validation<%s> failed: %s", self.slug, exception)
|
||||
|
||||
else:
|
||||
self.hacs.log.debug(
|
||||
"Validation<%s> took %.3f seconds to complete", self.slug, monotonic() - start_time
|
||||
)
|
||||
|
||||
|
||||
class ActionValidationBase(ValidationBase):
|
||||
"""Base class for action validation."""
|
||||
|
||||
action_only = True
|
||||
16
custom_components/hacs/validate/hacs_manifest.py
Normal file
16
custom_components/hacs/validate/hacs_manifest.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from ..enums import RepositoryFile
|
||||
from ..repositories.base import HacsRepository
|
||||
from .base import ActionValidationBase, ValidationException
|
||||
|
||||
|
||||
async def async_setup_validator(repository: HacsRepository) -> Validator:
|
||||
"""Set up this validator."""
|
||||
return Validator(repository=repository)
|
||||
|
||||
|
||||
class Validator(ActionValidationBase):
|
||||
def validate(self):
|
||||
if RepositoryFile.HACS_JSON not in [x.filename for x in self.repository.tree]:
|
||||
raise ValidationException(f"The repository has no '{RepositoryFile.HACS_JSON}' file")
|
||||
20
custom_components/hacs/validate/integration_manifest.py
Normal file
20
custom_components/hacs/validate/integration_manifest.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from ..enums import RepositoryFile
|
||||
from ..repositories.base import HacsRepository
|
||||
from .base import ActionValidationBase, ValidationException
|
||||
|
||||
|
||||
async def async_setup_validator(repository: HacsRepository) -> Validator:
|
||||
"""Set up this validator."""
|
||||
return Validator(repository=repository)
|
||||
|
||||
|
||||
class Validator(ActionValidationBase):
|
||||
category = "integration"
|
||||
|
||||
def validate(self):
|
||||
if RepositoryFile.MAINIFEST_JSON not in [x.filename for x in self.repository.tree]:
|
||||
raise ValidationException(
|
||||
f"The repository has no '{RepositoryFile.MAINIFEST_JSON}' file"
|
||||
)
|
||||
77
custom_components/hacs/validate/manager.py
Normal file
77
custom_components/hacs/validate/manager.py
Normal file
@@ -0,0 +1,77 @@
|
||||
"""Hacs validation manager."""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from importlib import import_module
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from custom_components.hacs.repositories.base import HacsRepository
|
||||
|
||||
from .base import ValidationBase
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..base import HacsBase
|
||||
|
||||
|
||||
class ValidationManager:
|
||||
"""Hacs validation manager."""
|
||||
|
||||
def __init__(self, hacs: HacsBase, hass: HomeAssistant) -> None:
|
||||
"""Initialize the setup manager class."""
|
||||
self.hacs = hacs
|
||||
self.hass = hass
|
||||
self._validatiors: dict[str, ValidationBase] = {}
|
||||
|
||||
@property
|
||||
def validatiors(self) -> dict[str, ValidationBase]:
|
||||
"""Return all list of all tasks."""
|
||||
return list(self._validatiors.values())
|
||||
|
||||
async def async_load(self, repository: HacsRepository) -> None:
|
||||
"""Load all tasks."""
|
||||
self._validatiors = {}
|
||||
validator_files = Path(__file__).parent
|
||||
validator_modules = (
|
||||
module.stem
|
||||
for module in validator_files.glob("*.py")
|
||||
if module.name not in ("base.py", "__init__.py", "manager.py")
|
||||
)
|
||||
|
||||
async def _load_module(module: str):
|
||||
task_module = import_module(f"{__package__}.{module}")
|
||||
if task := await task_module.async_setup_validator(repository=repository):
|
||||
self._validatiors[task.slug] = task
|
||||
|
||||
await asyncio.gather(*[_load_module(task) for task in validator_modules])
|
||||
self.hacs.log.debug("Loaded %s validators for %s", len(self.validatiors), repository)
|
||||
|
||||
async def async_run_repository_checks(self, repository: HacsRepository) -> None:
|
||||
"""Run all validators for a repository."""
|
||||
if not self.hacs.system.running:
|
||||
return
|
||||
|
||||
await self.async_load(repository)
|
||||
|
||||
await asyncio.gather(
|
||||
*[
|
||||
validator.execute_validation()
|
||||
for validator in self.validatiors or []
|
||||
if (self.hacs.system.action or not validator.action_only)
|
||||
and (
|
||||
validator.category == "common" or validator.category == repository.data.category
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
total = len([x for x in self.validatiors if self.hacs.system.action or not x.action_only])
|
||||
failed = len([x for x in self.validatiors if x.failed])
|
||||
|
||||
if failed != 0:
|
||||
repository.logger.error("%s %s/%s checks failed", repository.string, failed, total)
|
||||
if self.hacs.system.action:
|
||||
exit(1)
|
||||
else:
|
||||
repository.logger.debug("%s All (%s) checks passed", repository.string, total)
|
||||
15
custom_components/hacs/validate/repository_description.py
Normal file
15
custom_components/hacs/validate/repository_description.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from ..repositories.base import HacsRepository
|
||||
from .base import ActionValidationBase, ValidationException
|
||||
|
||||
|
||||
async def async_setup_validator(repository: HacsRepository) -> Validator:
|
||||
"""Set up this validator."""
|
||||
return Validator(repository=repository)
|
||||
|
||||
|
||||
class Validator(ActionValidationBase):
|
||||
def validate(self):
|
||||
if not self.repository.data.description:
|
||||
raise ValidationException("The repository has no description")
|
||||
@@ -0,0 +1,24 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from ..repositories.base import HacsRepository
|
||||
from .base import ActionValidationBase, ValidationException
|
||||
|
||||
|
||||
async def async_setup_validator(repository: HacsRepository) -> Validator:
|
||||
"""Set up this validator."""
|
||||
return Validator(repository=repository)
|
||||
|
||||
|
||||
class Validator(ActionValidationBase):
|
||||
async def async_validate(self):
|
||||
filenames = [x.filename.lower() for x in self.repository.tree]
|
||||
if "readme" in filenames:
|
||||
pass
|
||||
elif "readme.md" in filenames:
|
||||
pass
|
||||
elif "info" in filenames:
|
||||
pass
|
||||
elif "info.md" in filenames:
|
||||
pass
|
||||
else:
|
||||
raise ValidationException("The repository has no information file")
|
||||
15
custom_components/hacs/validate/repository_topics.py
Normal file
15
custom_components/hacs/validate/repository_topics.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from ..repositories.base import HacsRepository
|
||||
from .base import ActionValidationBase, ValidationException
|
||||
|
||||
|
||||
async def async_setup_validator(repository: HacsRepository) -> Validator:
|
||||
"""Set up this validator."""
|
||||
return Validator(repository=repository)
|
||||
|
||||
|
||||
class Validator(ActionValidationBase):
|
||||
def validate(self):
|
||||
if not self.repository.data.topics:
|
||||
raise ValidationException("The repository has no topics")
|
||||
Reference in New Issue
Block a user