64 lines
2.0 KiB
Python
64 lines
2.0 KiB
Python
import re
|
|
from dataclasses import asdict, dataclass
|
|
|
|
from .wordpress import LOGIN, VERSION
|
|
|
|
RELEASE = re.compile(r"[0-9]+(?:\.[0-9]+){0,5}", re.ASCII)
|
|
DATABASE_NAME = re.compile(r"[a-z0-9_]{1,55}", re.ASCII)
|
|
KINDS = ("core", "plugin", "theme")
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class WordPressUpdate:
|
|
kind: str
|
|
slug: str
|
|
installed: str
|
|
version: str
|
|
|
|
def __post_init__(self):
|
|
if (
|
|
self.kind not in KINDS
|
|
or not isinstance(self.slug, str)
|
|
or not LOGIN.fullmatch(self.slug)
|
|
or (self.kind == "core" and self.slug != "wordpress")
|
|
or not isinstance(self.installed, str)
|
|
or not VERSION.fullmatch(self.installed)
|
|
or not isinstance(self.version, str)
|
|
or len(self.version) > 30
|
|
or not RELEASE.fullmatch(self.version)
|
|
or self.installed == self.version
|
|
):
|
|
raise ValueError("Invalid WordPress update selection.")
|
|
|
|
@classmethod
|
|
def from_manifest(cls, data):
|
|
if not isinstance(data, dict) or data.keys() != {
|
|
"kind",
|
|
"slug",
|
|
"installed",
|
|
"version",
|
|
}:
|
|
raise ValueError("Invalid WordPress update selection.")
|
|
return cls(**data)
|
|
|
|
def manifest(self):
|
|
return asdict(self)
|
|
|
|
|
|
def validate_update_inventory(reply):
|
|
database = reply.get("database_name", "")
|
|
updates = reply.get("updates")
|
|
if (
|
|
not isinstance(database, str)
|
|
or (database and not DATABASE_NAME.fullmatch(database))
|
|
or not isinstance(updates, list)
|
|
or len(updates) > 120
|
|
):
|
|
raise ValueError("The web node returned an invalid update inventory.")
|
|
entries = [WordPressUpdate.from_manifest(item) for item in updates]
|
|
if len(
|
|
{(entry.kind, entry.slug, entry.version) for entry in entries}
|
|
) != len(entries):
|
|
raise ValueError("The web node returned duplicate WordPress updates.")
|
|
return {"database_name": database, "updates": updates}
|