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

_pkg=arrow
_pkgname=pyarrow
pkgname=python-${_pkgname}
pkgver=17.0.0
pkgrel=3
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)
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 python-setuptools-scm)
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=('4e2a617b8deeb9f94ee085653a721904a75696f0827bcba82b535cc7f4f723066a09914c7fa83c593e51a8a4031e8bf99e563cac1ebb1d89604cb406975d4864'
            '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=0 \
  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
}
