# Maintainer: Daniel M. Capella <polyzen@archlinux.org>

pkgname=python-pycares
_name=${pkgname#python-}
pkgver=4.4.0
pkgrel=2
pkgdesc='Python interface for c-ares'
arch=('x86_64' 'aarch64')
url=https://github.com/saghul/pycares
license=('MIT')
depends=('c-ares' 'python-cffi')
makedepends=(
  'git'
  'python-build'
  'python-installer'
  'python-setuptools'
  'python-wheel'
)
checkdepends=('python-idna' 'python-pytest')
optdepends=('python-idna')
source=("git+$url.git#tag=v$pkgver")
b2sums=('41f96f2297e367f6054c6d2748817afc2d84e4aae00da0f09d68ca906a9e625b0ff4f791cf09e729fba40c3af679c08234289a256800216a5dc86b14b0f6ba19')

build() {
  cd "$_name"
  PYCARES_USE_SYSTEM_LIB=1 python -m build --wheel --skip-dependency-check --no-isolation
}

check() {
  local pytest_options=(
    -vv
    --deselect tests/test_all.py::DNSTest::test_idna_encoding_query_a
    --deselect tests/test_all.py::DNSTest::test_query_txt_chunked
    --deselect tests/test_all.py::DNSTest::test_gethostbyaddr
    --deselect tests/test_all.py::DNSTest::test_gethostbyaddr6
    --deselect tests/test_all.py::DNSTest::test_getnameinfo
  )

  cd "$_name"
  python -m venv --system-site-packages test-env
  test-env/bin/python -m installer dist/*.whl
  test-env/bin/python -m pytest "${pytest_options[@]}"
}

package() {
  cd "$_name"
  python -m installer --destdir="$pkgdir" dist/*.whl

  # Symlink license file
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")
  install -d "$pkgdir"/usr/share/licenses/$pkgname
  ln -s "$site_packages"/"$_name"-$pkgver.dist-info/LICENSE \
    "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
}
