Skip to content

Commit

Permalink
Fix tests to run on Cairo 1.18.0 (ManimCommunity#3416)
Browse files Browse the repository at this point in the history
* Add a script to build and install cairo

* Update gui tests for cairo 1.18.0

* update script to set env vars

* Make the script run with plain python

* Prefer the recently built one in pkg-config

* Skip the built if it's windows

* CI: build and install latest cairo

* CI: only run when cache is missed

* Disable compiling tests while building cairo

* update poetry lock file

* Display the cairo version when running pytest

* fixup

* tests: skip graphical test when cairo is old

* fix the path to find the pkgconfig files on linux

* set the LD_LIBRARY_PATH too

only then it'll work on linux

* fixup

* small fixup

* Move the script inside `.github/scripts` folder

* Make the minimum cairo version a constant

* Seperate setting env vars to a sperate step

this seem to have broken when cache is hit
  • Loading branch information
naveen521kk authored Oct 25, 2023
1 parent 8320cdd commit b048695
Show file tree
Hide file tree
Showing 227 changed files with 272 additions and 27 deletions.
209 changes: 209 additions & 0 deletions .github/scripts/ci_build_cairo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
# Logic is as follows:
# 1. Download cairo source code: https://cairographics.org/releases/cairo-<version>.tar.xz
# 2. Verify the downloaded file using the sha256sums file: https://cairographics.org/releases/cairo-<version>.tar.xz.sha256sum
# 3. Extract the downloaded file.
# 4. Create a virtual environment and install meson and ninja.
# 5. Run meson build in the extracted directory. Also, set required prefix.
# 6. Run meson compile -C build.
# 7. Run meson install -C build.

import hashlib
import logging
import os
import subprocess
import sys
import tarfile
import tempfile
import typing
import urllib.request
from contextlib import contextmanager
from pathlib import Path
from sys import stdout

CAIRO_VERSION = "1.18.0"
CAIRO_URL = f"https://cairographics.org/releases/cairo-{CAIRO_VERSION}.tar.xz"
CAIRO_SHA256_URL = f"{CAIRO_URL}.sha256sum"

VENV_NAME = "meson-venv"
BUILD_DIR = "build"
INSTALL_PREFIX = Path(__file__).parent.parent / "third_party" / "cairo"

logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
logger = logging.getLogger(__name__)


def is_ci():
return os.getenv("CI", None) is not None


def download_file(url, path):
logger.info(f"Downloading {url} to {path}")
block_size = 1024 * 1024
with urllib.request.urlopen(url) as response, open(path, "wb") as file:
while True:
data = response.read(block_size)
if not data:
break
file.write(data)


def verify_sha256sum(path, sha256sum):
with open(path, "rb") as file:
file_hash = hashlib.sha256(file.read()).hexdigest()
if file_hash != sha256sum:
raise Exception("SHA256SUM does not match")


def extract_tar_xz(path, directory):
with tarfile.open(path) as file:
file.extractall(directory)


def run_command(command, cwd=None, env=None):
process = subprocess.Popen(command, cwd=cwd, env=env)
process.communicate()
if process.returncode != 0:
raise Exception("Command failed")


@contextmanager
def gha_group(title: str) -> typing.Generator:
if not is_ci():
yield
return
print(f"\n::group::{title}")
stdout.flush()
try:
yield
finally:
print("::endgroup::")
stdout.flush()


def set_env_var_gha(name: str, value: str) -> None:
if not is_ci():
return
env_file = os.getenv("GITHUB_ENV", None)
if env_file is None:
return
with open(env_file, "a") as file:
file.write(f"{name}={value}\n")
stdout.flush()


def get_ld_library_path(prefix: Path) -> str:
# given a prefix, the ld library path can be found at
# <prefix>/lib/* or sometimes just <prefix>/lib
# this function returns the path to the ld library path

# first, check if the ld library path exists at <prefix>/lib/*
ld_library_paths = list(prefix.glob("lib/*"))
if len(ld_library_paths) == 1:
return ld_library_paths[0].absolute().as_posix()

# if the ld library path does not exist at <prefix>/lib/*,
# return <prefix>/lib
ld_library_path = prefix / "lib"
if ld_library_path.exists():
return ld_library_path.absolute().as_posix()
return ""


def main():
if sys.platform == "win32":
logger.info("Skipping build on windows")
return

with tempfile.TemporaryDirectory() as tmpdir:
with gha_group("Downloading and Extracting Cairo"):
logger.info(f"Downloading cairo version {CAIRO_VERSION}")
download_file(CAIRO_URL, os.path.join(tmpdir, "cairo.tar.xz"))

logger.info("Downloading cairo sha256sum")
download_file(CAIRO_SHA256_URL, os.path.join(tmpdir, "cairo.sha256sum"))

logger.info("Verifying cairo sha256sum")
with open(os.path.join(tmpdir, "cairo.sha256sum")) as file:
sha256sum = file.read().split()[0]
verify_sha256sum(os.path.join(tmpdir, "cairo.tar.xz"), sha256sum)

logger.info("Extracting cairo")
extract_tar_xz(os.path.join(tmpdir, "cairo.tar.xz"), tmpdir)

with gha_group("Installing meson and ninja"):
logger.info("Creating virtual environment")
run_command([sys.executable, "-m", "venv", os.path.join(tmpdir, VENV_NAME)])

logger.info("Installing meson and ninja")
run_command(
[
os.path.join(tmpdir, VENV_NAME, "bin", "pip"),
"install",
"meson",
"ninja",
]
)

env_vars = {
# add the venv bin directory to PATH so that meson can find ninja
"PATH": f"{os.path.join(tmpdir, VENV_NAME, 'bin')}{os.pathsep}{os.environ['PATH']}",
}

with gha_group("Building and Installing Cairo"):
logger.info("Running meson setup")
run_command(
[
os.path.join(tmpdir, VENV_NAME, "bin", "meson"),
"setup",
BUILD_DIR,
f"--prefix={INSTALL_PREFIX.absolute().as_posix()}",
"--buildtype=release",
"-Dtests=disabled",
],
cwd=os.path.join(tmpdir, f"cairo-{CAIRO_VERSION}"),
env=env_vars,
)

logger.info("Running meson compile")
run_command(
[
os.path.join(tmpdir, VENV_NAME, "bin", "meson"),
"compile",
"-C",
BUILD_DIR,
],
cwd=os.path.join(tmpdir, f"cairo-{CAIRO_VERSION}"),
env=env_vars,
)

logger.info("Running meson install")
run_command(
[
os.path.join(tmpdir, VENV_NAME, "bin", "meson"),
"install",
"-C",
BUILD_DIR,
],
cwd=os.path.join(tmpdir, f"cairo-{CAIRO_VERSION}"),
env=env_vars,
)

logger.info(f"Successfully built cairo and installed it to {INSTALL_PREFIX}")


if __name__ == "__main__":
if "--set-env-vars" in sys.argv:
with gha_group("Setting environment variables"):
# append the pkgconfig directory to PKG_CONFIG_PATH
set_env_var_gha(
"PKG_CONFIG_PATH",
f"{Path(get_ld_library_path(INSTALL_PREFIX), 'pkgconfig').as_posix()}{os.pathsep}"
f'{os.getenv("PKG_CONFIG_PATH", "")}',
)
set_env_var_gha(
"LD_LIBRARY_PATH",
f"{get_ld_library_path(INSTALL_PREFIX)}{os.pathsep}"
f'{os.getenv("LD_LIBRARY_PATH", "")}',
)
sys.exit(0)
main()
20 changes: 16 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ jobs:
# start xvfb in background
sudo /usr/bin/Xvfb $DISPLAY -screen 0 1280x1024x24 &
- name: Setup Cairo Cache
uses: actions/cache@v3
id: cache-cairo
if: runner.os == 'Linux' || runner.os == 'macOS'
with:
path: ${{ github.workspace }}/third_party
key: ${{ runner.os }}-dependencies-cairo-${{ hashFiles('.github/scripts/ci_build_cairo.py') }}

- name: Build and install Cairo (Linux and macOS)
if: (runner.os == 'Linux' || runner.os == 'macOS') && steps.cache-cairo.outputs.cache-hit != 'true'
run: python .github/scripts/ci_build_cairo.py

- name: Set env vars for Cairo (Linux and macOS)
if: runner.os == 'Linux' || runner.os == 'macOS'
run: python .github/scripts/ci_build_cairo.py --set-env-vars

- name: Setup macOS cache
uses: actions/cache@v3
id: cache-macos
Expand Down Expand Up @@ -103,10 +119,6 @@ jobs:
export PATH="$oriPath"
echo "Completed TinyTeX"
- name: Install cairo (MacOS)
if: runner.os == 'macOS'
run: brew install cairo

- name: Add macOS dependencies to PATH
if: runner.os == 'macOS'
shell: bash
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,6 @@ dist/

/media_dir.txt
# ^TODO: Remove the need for this with a proper config file

# Ignore the built dependencies
third_party/*
2 changes: 2 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
except ModuleNotFoundError: # windows
pass

import cairo
import moderngl

# If it is running Doctest the current directory
Expand Down Expand Up @@ -39,6 +40,7 @@ def pytest_report_header(config):
info = ctx.info
ctx.release()
return (
f"\nCairo Version: {cairo.cairo_version()}",
"\nOpenGL information",
"------------------",
f"vendor: {info['GL_VENDOR'].strip()}",
Expand Down
9 changes: 9 additions & 0 deletions manim/utils/testing/frames_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from pathlib import Path
from typing import Callable

import cairo
import pytest
from _pytest.fixtures import FixtureRequest

from manim import Scene
Expand All @@ -25,6 +27,7 @@
SCENE_PARAMETER_NAME = "scene"
_tests_root_dir_path = Path(__file__).absolute().parents[2]
PATH_CONTROL_DATA = _tests_root_dir_path / Path("control_data", "graphical_units_data")
MIN_CAIRO_VERSION = 11800


def frames_comparison(
Expand Down Expand Up @@ -81,6 +84,12 @@ def decorator_maker(tested_scene_construct):
@functools.wraps(tested_scene_construct)
# The "request" parameter is meant to be used as a fixture by pytest. See below.
def wrapper(*args, request: FixtureRequest, tmp_path, **kwargs):
# check for cairo version
if (
renderer_class is CairoRenderer
and cairo.cairo_version() < MIN_CAIRO_VERSION
):
pytest.skip("Cairo version is too old. Skipping cairo graphical tests.")
# Wraps the test_function to a construct method, to "freeze" the eventual additional arguments (parametrizations fixtures).
construct = functools.partial(tested_scene_construct, *args, **kwargs)

Expand Down
56 changes: 33 additions & 23 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/boolean_ops/union.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/brace/arcBrace.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/brace/braceTip.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/creation/FadeIn.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/creation/FadeOut.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/creation/create.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/creation/uncreate.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/Angle.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/Annulus.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/Arc.npz
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/Arrange.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/Circle.npz
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/Coordinates.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/CurvedArrow.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/Dot.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/DoubleArrow.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/Elbow.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/Ellipse.npz
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/LabeledLine.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/Line.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/Polygon.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/Polygram.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/Rectangle.npz
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/RightAngle.npz
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/Sector.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/Star.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/Vector.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/geometry/ZIndex.npz
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/img_and_svg/Arcs01.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/img_and_svg/Arcs02.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/img_and_svg/Heart.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/img_and_svg/Line.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/img_and_svg/Penrose.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/img_and_svg/Rhomboid.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/img_and_svg/UKFlag.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/img_and_svg/WeightSVG.npz
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/indication/ApplyWave.npz
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/indication/Flash.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/indication/FocusOn.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/indication/Indicate.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/indication/Wiggle.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/logo/banner.npz
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/mobjects/become.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/mobjects/match_style.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/movements/Homotopy.npz
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/movements/MoveTo.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/movements/PhaseFlow.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/movements/Rotate.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/movements/Shift.npz
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/opengl/Circle.npz
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/plot/axes.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/plot/get_area.npz
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/plot/get_axis_labels.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/plot/get_graph_label.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/plot/log_scaling_graph.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/plot/plot_line_graph.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/plot/polar_graph.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/plot/t_label.npz
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/polyhedra/Icosahedron.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/polyhedra/Octahedron.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/specialized/Broadcast.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/speed/SpeedModifier.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/tables/DecimalTable.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/tables/IntegerTable.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/tables/MathTable.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/tables/MobjectTable.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/tables/Table.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/threed/Arrow3D.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/threed/Axes.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/threed/CameraMove.npz
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/threed/Cone.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/threed/Cube.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/threed/Cylinder.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/threed/Dot3D.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/threed/Line3D.npz
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/threed/Sphere.npz
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/threed/Torus.npz
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/threed/Y_Direction.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/transform/ApplyMatrix.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/transform/FullRotation.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/transform/Restore.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/transform/Transform.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/test_graphical_units/control_data/updaters/Updater.npz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit b048695

Please sign in to comment.