Files
github-action/tests/workflow_source.py
T
Tirsvad 78d21b34a3 Remove the scoped publish-wiki workflow and the marker path (2.0.0)
- .gitea/scoped_workflows/publish-wiki.yml is deleted
- tools/publish_wiki.py requires WIKI_PRUNE, reads no marker, and defaults
  WIKI_DIR to docs/wiki; its tests follow
- README, onboarding, troubleshooting and upgrading guides, and the wiki pages
  in English, Danish and Bahasa Malaysia no longer describe the scoped workflow
- changelog entry 2.0.0 with the Breaking group

Task: MIL-009#6
Task: MIL-009#8
2026-10-14 11:45:03 +08:00

83 lines
2.6 KiB
Python

"""Helpers that read workflow files and the Python embedded in them."""
import importlib.util
import re
import textwrap
from pathlib import Path
from typing import Any
REPO_ROOT = Path(__file__).resolve().parent.parent
WORKFLOW_DIRS = (
REPO_ROOT / ".gitea" / "scoped_workflows",
REPO_ROOT / ".gitea" / "workflows",
)
SYNC_WORKFLOW = WORKFLOW_DIRS[0] / "sync-github-metadata.yml"
SYNC_SCRIPT = REPO_ROOT / "tools" / "sync_github_metadata.py"
PUBLISH_SCRIPT = REPO_ROOT / "tools" / "publish_wiki.py"
_HEREDOC = re.compile(
r"^[ \t]*python3 - <<'PY'\r?\n(?P<code>.*?)^[ \t]*PY[ \t]*\r?$",
re.DOTALL | re.MULTILINE,
)
def workflow_files() -> list[Path]:
"""Return every workflow file in the known workflow directories."""
files: list[Path] = []
for directory in WORKFLOW_DIRS:
if directory.is_dir():
files += sorted(directory.glob("*.yml")) + sorted(directory.glob("*.yaml"))
return files
def embedded_python(path: Path) -> list[str]:
"""Return the source of each `python3 - <<'PY'` heredoc in a workflow."""
text = path.read_text(encoding="utf-8")
return [
textwrap.dedent(match.group("code").replace("\r\n", "\n"))
for match in _HEREDOC.finditer(text)
]
class ModuleView:
"""A script module, readable and writable by item and by attribute.
Tests write `sync["request_json"] = fake` or `module.request_json = fake`;
both set the attribute of the real module, so the functions of the script
see the replacement.
"""
def __init__(self, module: Any) -> None:
object.__setattr__(self, "_module", module)
def __getitem__(self, name: str) -> Any:
return getattr(self._module, name)
def __setitem__(self, name: str, value: Any) -> None:
setattr(self._module, name, value)
def __getattr__(self, name: str) -> Any:
return getattr(self._module, name)
def __setattr__(self, name: str, value: Any) -> None:
setattr(self._module, name, value)
def load_script(path: Path, name: str) -> ModuleView:
"""Import a script file without running its `main` (its name is not `__main__`)."""
spec = importlib.util.spec_from_file_location(name, path)
assert spec is not None and spec.loader is not None
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return ModuleView(module)
def load_sync_module(name: str = "sync_github_metadata") -> ModuleView:
"""Import tools/sync_github_metadata.py."""
return load_script(SYNC_SCRIPT, name)
def load_publish_module(name: str = "publish_wiki") -> ModuleView:
"""Import tools/publish_wiki.py."""
return load_script(PUBLISH_SCRIPT, name)