# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Maintainer: Daniel M. Capella <polyzen@archlinux.org>
# Contributor: Sergej Pupykin <pupykin.s+arch@gmail.com>
# Contributor: Angel 'angvp' Velasquez <angvp[at]archlinux.com.ve>

pkgname=python-pycurl
pkgver=7.45.7
pkgrel=1
pkgdesc="A Python 3.x interface to libcurl"
arch=('x86_64' 'aarch64')
url="https://github.com/pycurl/pycurl"
license=('LGPL-2.1-only OR MIT')
depends=('curl' 'glibc' 'openssl' 'python')
makedepends=('git' 'python-build' 'python-installer' 'python-setuptools' 'python-wheel')
checkdepends=('python-flask' 'python-pytest' 'vsftpd' 'python-flaky' 'python-pyflakes')
source=("git+https://github.com/pycurl/pycurl.git#tag=REL_${pkgver//./_}")
sha512sums=('765a74b426015729c1deb87156d9d2f13b0766f2db925705263e5f5634123ad32a20bf062053df5375286bc95a0ff301dd0b1d2cbf3cd62fb6c4ceeef4f811c9')

build() {
  cd pycurl
  # the unified source release builds (see PYCURL_RELEASE=1 in Makefile) do not work with tests
  python -m build --wheel --no-isolation
  # needed for tests
  make -C tests/fake-curl/libcurl
}

check() {
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")
  local pytest_options=(
    -vv
    --ignore tests/ftp_test.py  # downloads the internet
    # hanging tests (probably also due to downloading the internet)
    --ignore tests/multi_socket_test.py
    --ignore tests/multi_test.py
    --ignore tests/multi_timer_test.py
    --deselect tests/open_socket_cb_test.py::OpenSocketCbTest::test_socket_open
    --ignore tests/perform_test.py
    --ignore tests/post_test.py
    --ignore tests/read_cb_test.py
    --ignore tests/readdata_test.py
    --deselect tests/reset_test.py::ResetTest::test_reset
    --deselect tests/resolve_test.py::ResolveTest::test_resolve
    --deselect tests/seek_cb_test.py::SeekCbTest::test_seek_function
    --deselect tests/setopt_lifecycle_test.py::SetoptLifecycleTest::test_postfields_lifecycle
    --deselect tests/setopt_string_test.py::SetoptTest::test_setopt_string
    --deselect tests/relative_url_test.py::RelativeUrlTest::test_get_relative
    --deselect tests/pause_test.py::PauseTest::test_pause_via_call
    --deselect tests/pause_test.py::PauseTest::test_pause_via_return
    --deselect tests/setopt_test.py::SetoptTest::test_set_httpheader_none
    --deselect tests/setopt_test.py::SetoptTest::test_unset_httpheader
    --deselect tests/setopt_unicode_test.py::SetoptUnicodeTest::test_ascii_string
    --deselect tests/setopt_unicode_test.py::SetoptUnicodeTest::test_unicode_encoded
    --deselect tests/share_test.py::ShareTest::test_share
    --deselect tests/sockopt_cb_test.py::SockoptCbTest::test_sockoptfunction_ok
    --deselect tests/user_agent_string_test.py::UserAgentStringTest::test_pycurl_user_agent_string
    --ignore tests/write_test.py
    --ignore tests/write_to_stringio_test.py
    --deselect tests/xferinfo_cb_test.py::XferinfoCbTest::test_xferinfo_cb
    # unclear failures
    --deselect tests/multi_callback_test.py::MultiCallbackTest::test_multi_socket_action
    --deselect tests/multi_socket_select_test.py::MultiSocketSelectTest::test_multi_socket_select
  )

  cd pycurl
  python -m installer --destdir=test_dir dist/*.whl
  export PYTHONPATH="$PWD/test_dir/$site_packages:$PYTHONPATH"
  pytest "${pytest_options[@]}"
}

package() {
  cd pycurl
  python -m installer --destdir="$pkgdir" dist/*.whl
  install -Dm644 COPYING-MIT -t "$pkgdir"/usr/share/licenses/$pkgname/
}
