# Maintainer: Bruno Pagani <archange@archlinux.org>
# Maintainer: Konstantin Gizdov <arch at kge dot pw>
# Contributor: Guillaume Horel <guillaume.horel@gmail.com>

_pkg=arrow
_pkgname=pyarrow
pkgname=python-${_pkgname}
pkgver=16.1.0
pkgrel=4
pkgdesc="Columnar in-memory analytics layer for big data — Python module."
arch=(x86_64 aarch64)
url="https://arrow.apache.org"
license=(Apache-2.0)
depends=(arrow gcc-libs glibc python python-numpy python-setuptools-scm)
optdepends=('python-cffi: interact with C code'
            'python-pandas: Pandas integration'
            'python-fsspec: Filesystem Spec support')
makedepends=(git cmake cython python-build python-installer python-wheel python-cffi python-pandas)
checkdepends=(python-brotli python-hypothesis python-pandas python-pytest python-pytz)
source=(
  https://archive.apache.org/dist/${_pkg}/${_pkg}-${pkgver}/apache-${_pkg}-${pkgver}.tar.gz{,.asc}
  git+https://github.com/apache/arrow-testing.git
)
sha512sums=('28975f59e1fdde2dba4afaf4a5ba934b63db3a7f27656e2aa0af0f0d2a046c9dbfa9a6082de94629c36d03809b296566a37ea65ec5a2fc17fedac7d21e272d31'
            'SKIP'
            'SKIP')
validpgpkeys=(265F80AB84FE03127E14F01125BCCA5220D84079  # Krisztian Szucs (apache) <szucs.krisztian@gmail.com>
              08D3564B7C6A9CAFBFF6A66791D18FCF079F8007  # Kouhei Sutou <kou@cozmixng.org>
              AF6AADA4C9835B75973FF5DA275C532289DD0F4A) # Raúl Cumplido Domínguez (CODE SIGNING KEY) <raulcd@apache.org>

prepare() {
  cd apache-${_pkg}-${pkgver}/python
  for _file in pyproject.toml requirements-build.txt requirements-wheel-build.txt setup.py; do
    sed -e '/oldest-supported-numpy/d' -e 's|< 8.0.0||g' -i "${_file}"
  done
}

build() {
  cd apache-${_pkg}-${pkgver}/python
  # https://github.com/apache/arrow/blob/main/docs/source/developers/python.rst#relevant-components-and-environment-variables
  PYARROW_CMAKE_OPTIONS="-DARROW_SIMD_LEVEL=NONE -DARROW_RUNTIME_SIMD_LEVEL=MAX" \
  PYARROW_WITH_DATASET=1 \
  PYARROW_WITH_FLIGHT=1 \
  PYARROW_WITH_HDFS=1 \
  PYARROW_WITH_ORC=1 \
  PYARROW_WITH_PARQUET=1 \
  PYARROW_WITH_PARQUET_ENCRYPTION=1 \
  PYARROW_WITH_SUBSTRAIT=1 \
  PYARROW_WITH_TENSORFLOW=1 \
  python -m build --wheel --no-isolation
}

check() {
  python -m venv --system-site-packages local-env
  local-env/bin/python -m installer apache-${_pkg}-${pkgver}/python/dist/*.whl
  ARROW_TEST_DATA="${srcdir}"/arrow-testing/data \
  local-env/bin/python -m pytest -vv --color=yes --pyargs pyarrow
}

package(){
  cd apache-${_pkg}-${pkgver}/python
  python -m installer --destdir="${pkgdir}" dist/*.whl

  # drop tests from install
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")
  rm -rf "${pkgdir}${site_packages}"/${_pkgname}/{conftest.py,tests}

  # move python include files
  install -d "${pkgdir}"/usr/include/arrow/
  mv "${pkgdir}${site_packages}"/${_pkgname}/include/arrow/python/ "${pkgdir}"/usr/include/arrow/
  rm -rf "${pkgdir}${site_packages}"/${_pkgname}/include/
  ln -sT /usr/include/arrow/ "${pkgdir}${site_packages}"/${_pkgname}/include
}
