Skip to content

The animation engine for agents: blazingly fast math and 3D videos with Manim's Python API, on a Rust GPU core.

Python

Enforce license and file hygiene

apoogdk182de6bf6985
diff --git a/tests/test_licenses.py b/tests/test_licenses.py
new file mode 100644
index 00000000..59681bea
--- /dev/null
+++ b/tests/test_licenses.py
@@ -0,0 +1,52 @@
+"""What manimgx's wheels hold is licensed, and says so: their third-party notice,
+`LICENSE-THIRD-PARTY`, is current, and manimgx's `license` covers all it names
+(scripts/release/licenses.py); the lavapipe's, `LICENSE-LAVAPIPE`, is of the Mesa the Linux wheels are
+built with; and a module holding code ported from Manim CE carries CE's copyright lines,
+wherever that code moves."""
+
+import re
+import subprocess
+import sys
+from pathlib import Path
+
+ROOT = Path(__file__).resolve().parent.parent
+# fragments of Manim CE's code, as manimgx's modules hold them: `Mobject.move_to`'s mask,
+# `color_gradient`'s last step, `ApplyWave`'s phases (a port's own fragment goes here)
+PORTED_FROM_CE = ("coor_mask", "alphas_mod1", "phases = ripples * 2")
+CE_COPYRIGHT = "SPDX-FileCopyrightText: 2024 the Manim Community Developers"
+
+
+def test_the_third_party_notice_is_current_and_covered_by_the_license() -> None:
+ check = subprocess.run(
+ [sys.executable, str(ROOT / "scripts" / "release" / "licenses.py"), "--check"],
+ capture_output=True,
+ text=True,
+ encoding="utf-8",
+ check=False,
+ )
+ assert check.returncode == 0, check.stderr
+
+
+def test_the_lavapipe_notice_is_of_the_mesa_it_is_built_from() -> None:
+ script = (ROOT / "scripts" / "release" / "build_lavapipe.sh").read_text(
+ encoding="utf-8"
+ )
+ built = re.search(r"^mesa=(\S+)$", script, re.MULTILINE)
+ notice = (ROOT / "LICENSE-LAVAPIPE").read_text(encoding="utf-8")
+ described = re.search(r"^Mesa (\S+) \(", notice, re.MULTILINE)
+ assert built
+ assert described
+ assert described[1] == built[1], "LICENSE-LAVAPIPE describes another Mesa"
+
+
+def test_code_ported_from_manim_ce_says_whose_it_is() -> None:
+ sources = ROOT / "src"
+ texts = {p: p.read_text(encoding="utf-8") for p in sorted(sources.rglob("*.py"))}
+ unsaid = [
+ p.relative_to(sources).as_posix()
+ for p, text in texts.items()
+ if CE_COPYRIGHT not in text and any(f in text for f in PORTED_FROM_CE)
+ ]
+ gone = [f for f in PORTED_FROM_CE if not any(f in text for text in texts.values())]
+ assert not unsaid, f"Manim CE's code without its copyright lines: {unsaid}"
+ assert not gone, f"no module holds {gone} any more: drop it from PORTED_FROM_CE"
diff --git a/tests/test_text_files.py b/tests/test_text_files.py
new file mode 100644
index 00000000..4cc72ce7
--- /dev/null
+++ b/tests/test_text_files.py
@@ -0,0 +1,42 @@
+"""Every text file manimgx reads or writes (its packages, tests, docs and scripts) names its
+encoding. Without one, Python takes the locale's, which on Windows is its ANSI code page: a
+subtitle, a page or a scene with characters beyond it would fail to write, or read wrong.
+"""
+
+import ast
+from pathlib import Path
+
+ROOT = Path(__file__).resolve().parent.parent
+
+
+def _unnamed(tree: ast.Module) -> list[int]:
+ """The lines of the text reads and writes that name no encoding."""
+ lines = []
+ for node in ast.walk(tree):
+ if not isinstance(node, ast.Call) or any(
+ k.arg == "encoding" for k in node.keywords
+ ):
+ continue
+ func = node.func
+ if isinstance(func, ast.Attribute) and (
+ func.attr == "read_text" or (func.attr == "write_text" and node.args)
+ ):
+ lines.append(node.lineno)
+ elif isinstance(func, ast.Name) and func.id == "open":
+ mode = next((k.value for k in node.keywords if k.arg == "mode"), None)
+ mode = node.args[1] if len(node.args) > 1 else mode
+ if not (isinstance(mode, ast.Constant) and "b" in str(mode.value)):
+ lines.append(node.lineno)
+ return lines
+
+
+def test_every_text_file_is_read_and_written_as_utf_8() -> None:
+ unnamed = [
+ f"{path.relative_to(ROOT)}:{line}"
+ for folder in ("src", "fonts", "tests", "docs", "scripts")
+ for path in (ROOT / folder).rglob("*.py")
+ # the corpus's scenes are Manim code, as users write it: what they read is theirs
+ if "cases" not in path.relative_to(ROOT).parts
+ for line in _unnamed(ast.parse(path.read_text(encoding="utf-8")))
+ ]
+ assert not unnamed, f"text read or written without an encoding: {unnamed}"