diff --git a/c2cciutils/security.py b/c2cciutils/security.py index d9e35c1c8..ea653e6a4 100644 --- a/c2cciutils/security.py +++ b/c2cciutils/security.py @@ -8,6 +8,13 @@ import markdown from markdown.extensions.tables import TableExtension +HEADER_VERSION = "Version" +HEADER_ALTERNATE_TAG = "Alternate Tag" +HEADER_SUPPORT_UNTIL = "Supported Until" +SUPPORT_TO_BE_DEFINED = "To be defined" +SUPPORT_BEST_EFFORT = "Best effort" +SUPPORT_UNSUPPORTED = "Unsupported" + class Security: """ @@ -18,12 +25,13 @@ class Security: data: list[list[str]] _row: Optional[list[str]] = None - def __init__(self, status: str): + def __init__(self, status: str, check: bool = True): """ Initialize. Arguments: status: the content of the SECURITY.md file. + check: Set to `False` to skip the check. """ self.headers = [] @@ -40,6 +48,52 @@ def __init__(self, status: str): for row in self.data: row.append("") + self.version_index = self.headers.index(HEADER_VERSION) if HEADER_VERSION in self.headers else -1 + self.alternate_tag_index = ( + self.headers.index(HEADER_ALTERNATE_TAG) if HEADER_ALTERNATE_TAG in self.headers else -1 + ) + self.support_until_index = ( + self.headers.index(HEADER_SUPPORT_UNTIL) if HEADER_SUPPORT_UNTIL in self.headers else -1 + ) + + if check: + if not self.check(verbose=0): + raise ValueError("SECURITY.md file is not valid.") + + def check(self, verbose: int = -1) -> bool: + """ + Check the content. + + Arguments: + verbose: the verbosity level, `-1` for no output, `0` for errors only, `1` for all. + + Return: + `True` if the content is valid, `False` otherwise. + """ + + success = True + if self.version_index == -1: + if verbose >= 0: + print("`Version` column not found.") + success = False + elif verbose >= 1: + print(f"`Version` column found at index {self.version_index}.") + + if self.alternate_tag_index == -1: + if verbose >= 1: + print("Optional `Alternate Tag` column not found.") + elif verbose >= 1: + print(f"`Alternate Tag` column found at index {self.alternate_tag_index}.") + + if self.support_until_index == -1: + if verbose >= 0: + print("`Support Until` column not found.") + success = False + elif verbose >= 1: + print(f"`Support Until` column found at index {self.support_until_index}.") + + return success + def _pe(self, elem: xml.etree.ElementTree.Element) -> None: """ Parse the HTML table. diff --git a/pyproject.toml b/pyproject.toml index 759fbf7b3..81ec91550 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -47,12 +47,13 @@ exclude = ["c2cciutils/node_modules/**/test"] [tool.poetry.scripts] c2cciutils = "c2cciutils.scripts.main:main" c2cciutils-env = "c2cciutils.scripts.env:main" -c2cciutils-checks = "c2cciutils.scripts.env:main" -c2cciutils-pull-request-checks = "c2cciutils.scripts.pr_checks:main" -c2cciutils-audit = "c2cciutils.scripts.audit:main" c2cciutils-publish = "c2cciutils.scripts.publish:main" c2cciutils-version = "c2cciutils.scripts.version:main" c2cciutils-clean = "c2cciutils.scripts.clean:main" +c2cciutils-security-md = "c2cciutils.scripts.security_md:main" +c2cciutils-checks = "c2cciutils.scripts.env:main" +c2cciutils-pull-request-checks = "c2cciutils.scripts.pr_checks:main" +c2cciutils-audit = "c2cciutils.scripts.audit:main" c2cciutils-google-calendar = "c2cciutils.publish:main_calendar" c2cciutils-k8s-install = "c2cciutils.scripts.k8s.install:main" c2cciutils-k8s-db = "c2cciutils.scripts.k8s.db:main"