updating to latest
This commit is contained in:
38
custom_components/hacs/validate/README.md
Normal file
38
custom_components/hacs/validate/README.md
Normal file
@@ -0,0 +1,38 @@
|
||||
# Repository validation
|
||||
|
||||
This is where the validation rules that run against the various repository categories live.
|
||||
|
||||
## Structure
|
||||
|
||||
- All validation rules are in the directory for their category.
|
||||
- Validation rules that aplies to all categories are in the `common` directory.
|
||||
- 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.
|
||||
- The class name should describe what the check does.
|
||||
- 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 custom_components.hacs.validate.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")
|
||||
```
|
||||
51
custom_components/hacs/validate/__init__.py
Normal file
51
custom_components/hacs/validate/__init__.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import asyncio
|
||||
import glob
|
||||
import importlib
|
||||
from os.path import dirname, join, sep
|
||||
|
||||
from custom_components.hacs.share import SHARE, get_hacs
|
||||
|
||||
|
||||
def _initialize_rules():
|
||||
rules = glob.glob(join(dirname(__file__), "**/*.py"))
|
||||
for rule in rules:
|
||||
rule = rule.replace(sep, "/")
|
||||
rule = rule.split("custom_components/hacs")[-1]
|
||||
rule = f"custom_components/hacs{rule}".replace("/", ".")[:-3]
|
||||
importlib.import_module(rule)
|
||||
|
||||
|
||||
async def async_initialize_rules():
|
||||
hass = get_hacs().hass
|
||||
await hass.async_add_executor_job(_initialize_rules)
|
||||
|
||||
|
||||
async def async_run_repository_checks(repository):
|
||||
hacs = get_hacs()
|
||||
if not SHARE["rules"]:
|
||||
await async_initialize_rules()
|
||||
if not hacs.system.running:
|
||||
return
|
||||
checks = []
|
||||
for check in SHARE["rules"].get("common", []):
|
||||
checks.append(check(repository))
|
||||
for check in SHARE["rules"].get(repository.data.category, []):
|
||||
checks.append(check(repository))
|
||||
|
||||
await asyncio.gather(
|
||||
*[
|
||||
check._async_run_check()
|
||||
for check in checks or []
|
||||
if hacs.system.action or not check.action_only
|
||||
]
|
||||
)
|
||||
|
||||
total = len([x for x in checks if hacs.system.action or not x.action_only])
|
||||
failed = len([x for x in checks if x.failed])
|
||||
|
||||
if failed != 0:
|
||||
repository.logger.error("%s %s/%s checks failed", repository, failed, total)
|
||||
if hacs.system.action:
|
||||
exit(1)
|
||||
else:
|
||||
repository.logger.debug("%s All (%s) checks passed", repository, total)
|
||||
48
custom_components/hacs/validate/base.py
Normal file
48
custom_components/hacs/validate/base.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from custom_components.hacs.share import SHARE, get_hacs
|
||||
|
||||
|
||||
class ValidationException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ValidationBase:
|
||||
def __init__(self, repository) -> None:
|
||||
self.repository = repository
|
||||
self.hacs = get_hacs()
|
||||
self.failed = False
|
||||
self.logger = repository.logger
|
||||
|
||||
def __init_subclass__(cls, category="common", **kwargs) -> None:
|
||||
"""Initialize a subclass, register if possible."""
|
||||
super().__init_subclass__(**kwargs)
|
||||
if SHARE["rules"].get(category) is None:
|
||||
SHARE["rules"][category] = []
|
||||
if cls not in SHARE["rules"][category]:
|
||||
SHARE["rules"][category].append(cls)
|
||||
|
||||
@property
|
||||
def action_only(self):
|
||||
return False
|
||||
|
||||
async def _async_run_check(self):
|
||||
"""DO NOT OVERRIDE THIS IN SUBCLASSES!"""
|
||||
if self.hacs.system.action:
|
||||
self.logger.info(f"Running check '{self.__class__.__name__}'")
|
||||
try:
|
||||
await self.hacs.hass.async_add_executor_job(self.check)
|
||||
await self.async_check()
|
||||
except ValidationException as exception:
|
||||
self.failed = True
|
||||
self.logger.error(exception)
|
||||
|
||||
def check(self):
|
||||
pass
|
||||
|
||||
async def async_check(self):
|
||||
pass
|
||||
|
||||
|
||||
class ActionValidationBase(ValidationBase):
|
||||
@property
|
||||
def action_only(self):
|
||||
return True
|
||||
10
custom_components/hacs/validate/common/hacs_manifest.py
Normal file
10
custom_components/hacs/validate/common/hacs_manifest.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from custom_components.hacs.validate.base import (
|
||||
ActionValidationBase,
|
||||
ValidationException,
|
||||
)
|
||||
|
||||
|
||||
class HacsManifest(ActionValidationBase):
|
||||
def check(self):
|
||||
if "hacs.json" not in [x.filename for x in self.repository.tree]:
|
||||
raise ValidationException("The repository has no 'hacs.json' file")
|
||||
@@ -0,0 +1,10 @@
|
||||
from custom_components.hacs.validate.base import (
|
||||
ActionValidationBase,
|
||||
ValidationException,
|
||||
)
|
||||
|
||||
|
||||
class RepositoryDescription(ActionValidationBase):
|
||||
def check(self):
|
||||
if not self.repository.data.description:
|
||||
raise ValidationException("The repository has no description")
|
||||
@@ -0,0 +1,19 @@
|
||||
from custom_components.hacs.validate.base import (
|
||||
ActionValidationBase,
|
||||
ValidationException,
|
||||
)
|
||||
|
||||
|
||||
class RepositoryInformationFile(ActionValidationBase):
|
||||
async def async_check(self):
|
||||
filenames = [x.filename.lower() for x in self.repository.tree]
|
||||
if self.repository.data.render_readme and "readme" in filenames:
|
||||
pass
|
||||
elif self.repository.data.render_readme and "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")
|
||||
10
custom_components/hacs/validate/common/repository_topics.py
Normal file
10
custom_components/hacs/validate/common/repository_topics.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from custom_components.hacs.validate.base import (
|
||||
ActionValidationBase,
|
||||
ValidationException,
|
||||
)
|
||||
|
||||
|
||||
class RepositoryTopics(ActionValidationBase):
|
||||
def check(self):
|
||||
if not self.repository.data.topics:
|
||||
raise ValidationException("The repository has no topics")
|
||||
@@ -0,0 +1,10 @@
|
||||
from custom_components.hacs.validate.base import (
|
||||
ActionValidationBase,
|
||||
ValidationException,
|
||||
)
|
||||
|
||||
|
||||
class IntegrationManifest(ActionValidationBase, category="integration"):
|
||||
def check(self):
|
||||
if "manifest.json" not in [x.filename for x in self.repository.tree]:
|
||||
raise ValidationException("The repository has no 'hacs.json' file")
|
||||
Reference in New Issue
Block a user