# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Maintainer: Bruno Pagani <archange@archlinux.org>
# Contributor: Stéphane Gaudreault <stephane@archlinux.org>
# Contributor: Stefan Husmann <stefan-husmann@t-online.de>
# Contributor: Angel 'angvp' Velasquez <angvp[at]archlinux.com.ve>
# Contributor: Douglas Soares de Andrade <dsa@aur.archlinux.org>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - replace make/optdepends on pytho-cairocffi with python-cairo (cffi has haskell dep)
#  - disable lto via setup.cfg

pkgname=python-matplotlib
pkgver=3.10.0
pkgrel=1
_mpl_images_ver=3.10.0
pkgdesc="A python plotting library, making publication quality plots"
arch=(x86_64)
url="https://matplotlib.org"
license=(custom)
depends=(freetype2 python-contourpy python-cycler python-dateutil python-fonttools python-kiwisolver python-numpy
         python-packaging python-pillow python-pyparsing qhull)
optdepends=('tk: Tk{Agg,Cairo} backends'
            'pyside6: alternative for Qt6{Agg,Cairo} backends'
            'python-pyqt5: Qt5{Agg,Cairo} backends'
            'python-pyqt6: Qt6{Agg,Cairo} backends'
            'python-gobject: for GTK{3,4}{Agg,Cairo} backend'
            'python-wxpython: WX{Agg,Cairo} backend'
            'python-cairo: {GTK{3,4},Qt{5,6},Tk,WX}Cairo backends'
            'python-tornado: WebAgg backend'
            'ffmpeg: for saving movies'
            'imagemagick: for saving animated gifs'
            'ghostscript: usetex dependencies'
            'texlive-binextra: usetex dependencies'
            'texlive-fontsrecommended: usetex dependencies'
            'texlive-latexrecommended: usetex usage with pdflatex'
            'python-certifi: https support')
makedepends=(git python-build python-installer python-certifi python-setuptools-scm pybind11 python-wheel meson-python)
checkdepends=(python-pytest python-pytest-xdist python-pytest python-pytest-rerunfailures xorg-server-xvfb
              texlive-bin texlive-latexextra texlive-pictures texlive-fontsrecommended texlive-luatex
              ghostscript inkscape ffmpeg imagemagick gtk4
              python-cairo python-gobject python-pyqt5
              python-pyqt6 pyside6 python-tornado python-wxpython tk
              jupyter-nbconvert jupyter-nbformat python-ipykernel
              python-pandas python-pikepdf python-pytz python-xarray
              noto-fonts-cjk wqy-zenhei python-xcffib ttf-dejavu)
_ftver=2.13.1
source=(git+https://github.com/matplotlib/matplotlib.git#tag=v${pkgver}?signed
        https://github.com/QuLogic/mpl-images/archive/v${_mpl_images_ver}-with-freetype-${_ftver}/mpl-images-${_mpl_images_ver}-ft${_ftver}.tar.gz
        freetype.patch)
b2sums=('5a6c87aec6b1ea5111e5b03c5e002e650a006153e1728910ebfde8637e50ac87964c291d61df5ce191b298673c943de21fd91f16a5ded8b8e6ca40a6377fd9c3'
        '63f89667204996c6a78c297bda22526ceb25112c0ccbe14a07b92772e8f9b4bad213e8fd27a4ce25bd2fa983bbc6450b66b5315d92f444f8105f6ece5aca0601'
        'bb8546a237dec0cf49c5a8315ad1771051d405b90c7dda5b572d2f4f2a24bb0a1116337070092bd6c568f9d16ced9494afd4c6c484e5ce42044d947239164ae4')
validpgpkeys=(23CAB59E3332F94D26BEF0378D86E7FAE5EB0C10  # Elliott Sales de Andrade <quantum.analyst@gmail.com>
              EB8322187FD451192E430A7279B3FEC456F12599) # Kyle Sunden <contact@ksunden.space>

prepare() {
  cd matplotlib
  # Fix SCM detected version
  rm -r .git
  # Don't require oldest-supported-numpy
  sed -e 's|oldest-supported-numpy|numpy|' -e 's|,<0.17.0||' -i pyproject.toml
  patch -p1 < ../freetype.patch # From Fedora/upstream
  sed -e 's|2_000_000|2_500_000|' -i lib/matplotlib/tests/test_backends_interactive.py
  # Use appropriate baseline images for tests
  for _module in matplotlib mpl_toolkits
  do
    cp -r ../mpl-images-${_mpl_images_ver}-with-freetype-${_ftver}/lib/${_module}/* lib/${_module}
  done
}

build() {
  cd matplotlib
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
  python -m build --wheel --no-isolation \
    -C setup-args=-Dsystem-freetype=true \
    -C setup-args=-Dsystem-qhull=true
}

check() {
  cd matplotlib
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
  python -m venv --system-site-packages test-env
  test-env/bin/python -m installer dist/*.whl
  # Use appropriate baseline images for tests
  local python_version=`python -c 'import sys; print(".".join(map(str, sys.version_info[:2])))'`
  for _module in matplotlib mpl_toolkits
  do
    cp -r lib/${_module}/* test-env/lib/python${python_version}/site-packages/${_module}
    cp -r ../mpl-images-${_mpl_images_ver}-with-freetype-${_ftver}/lib/${_module}/* test-env/lib/python${python_version}/site-packages/${_module}
  done
  # test_ipynb: https://github.com/matplotlib/matplotlib/issues/21654 fixed but `ModuleNotFoundError: No module named 'matplotlib'`
  # test_compressed1: https://github.com/QuLogic/mpl-images/issues/4
  XDG_RUNTIME_DIR=/tmp/runtime-build \
  PYTHONPATH=test-env/lib/python${python_version}/site-packages \
  xvfb-run -a -s "-screen 0 640x480x24" \
    test-env/bin/python -m pytest -ra -n auto -v --color=yes --pyargs matplotlib mpl_toolkits.{axes_grid1,axisartist,mplot3d} \
      -k 'not test_ipynb and not test_compressed1 and not test_multi_font_type3 and not test_multi_font_type42 and not test_figure_legend_outside'
}

package() {
  cd matplotlib
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
  python -m installer --destdir="${pkgdir}" dist/*.whl
  install -Dm644 doc/devel/license.rst -t "${pkgdir}"/usr/share/licenses/${pkgname}/
  # Remove tests
  rm -r "${pkgdir}"$(python -c "import site; print(site.getsitepackages()[0])")/{matplotlib,mpl_toolkits/*}/tests/
}