# 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=15.0.2
pkgrel=2
pkgdesc="Columnar in-memory analytics layer for big data — Python module."
arch=(x86_64 aarch64)
url="https://arrow.apache.org"
license=(Apache)
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 cython0 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=('6c83e3be1e5840c30387f088315b74aca8e7c2d060793af70a156effb496a71e3e6af0693188c0f46f8a4a061a263a47095912ef04a5dc8141abd59075b14c78'
            '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
  ARROW_HOME=/usr \
  PARQUET_HOME=/usr \
  PYARROW_BUNDLE_ARROW_CPP_HEADERS=0 \
  PYARROW_BUNDLE_PLASMA_EXECUTABLE=0 \
  PYARROW_WITH_HDFS=1 \
  PYARROW_WITH_FLIGHT=1 \
  PYARROW_WITH_DATASET=1 \
  PYARROW_WITH_PARQUET=1 \
  PYARROW_WITH_PLASMA=1 \
  PYARROW_WITH_TENSORFLOW=1 \
  PYARROW_WITH_ORC=1 \
  python -m build --wheel --no-isolation
}

check() {
  cd apache-${_pkg}-${pkgver}/python
  local python_version=$(python -c 'import sys; print("".join(map(str, sys.version_info[:2])))')
  # rename source to avoid name clash
  mv pyarrow _nopyarrow
  PYTHONPATH="${PWD}/build/lib.linux-${CARCH}-cpython-${python_version/./}" \
  ARROW_TEST_DATA="${srcdir}"/arrow-testing/data \
  ARROW_HOME=/usr \
  PARQUET_HOME=/usr \
  pytest -vv --color=yes -k 'not test_cython_api and not test_visit_strings and not test_env_var and not test_get_include and not test_pyarrow_include'
  mv _nopyarrow pyarrow
}

package(){
  cd apache-${_pkg}-${pkgver}/python
  ARROW_HOME=/usr \
  PARQUET_HOME=/usr \
  PYARROW_BUNDLE_ARROW_CPP_HEADERS=0 \
  PYARROW_BUNDLE_PLASMA_EXECUTABLE=0 \
  PYARROW_WITH_HDFS=1 \
  PYARROW_WITH_FLIGHT=1 \
  PYARROW_WITH_DATASET=1 \
  PYARROW_WITH_PARQUET=1 \
  PYARROW_WITH_PLASMA=1 \
  PYARROW_WITH_TENSORFLOW=1 \
  PYARROW_WITH_ORC=1 \
  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}
}
