# Packager = Tuxcore <dev@tuxcore.org> 
_name=urllib3
pkgname=python-urllib3
pkgver=1.26.18
pkgrel=3
pkgdesc="HTTP library with thread-safe connection pooling and file post support"
arch=("any")
url="https://github.com/urllib3/urllib3"
license=("MIT")
depends=('python')
makedepends=(
  'python-build'
  'python-installer'
  'python-setuptools'
  'python-wheel'
)
checkdepends=(
  'python-brotli'
  'python-certifi'
  'python-cryptography'
  'python-dateutil'
  'python-flaky'
  'python-gcp-devrel-py-tools'
  'python-idna'
  'python-pyopenssl'
  'python-pysocks'
  'python-pytest7'
  'python-pytest-freezegun'
  'python-pytest-timeout'
  'python-tornado'
  'python-trustme'
)
optdepends=(
  'python-brotli: Brotli support'
  'python-certifi: security support'
  'python-cryptography: security support'
  'python-idna: security support'
  'python-pyopenssl: security support'
  'python-pysocks: SOCKS support'
)
source=("https://github.com/urllib3/urllib3/archive/$pkgver/$pkgname-$pkgver.tar.gz")
sha512sums=('62c0af4b11e797a85420ef3f0888f2e608334329eddd88b9fe563b5437189cbea8dbbcd53f999557d9828fcf4bf03b8ca9f6e3d401533bc4ae8ff96e3ece1557')

prepare() {
  # remove python-mock requirement
  find $_name-$pkgver -type f -iname "*.py" -exec sed 's/import mock/from unittest import mock/; s/from mock/from unittest.mock/' -i {} +
}

build() {
  cd $_name-$pkgver
  python -m build --wheel --no-isolation
}

check() {
  local pytest_options=(
    -vv
    # TODO: report upstream
    --deselect test/test_ssltransport.py::SingleTLSLayerTestCase::test_ssl_object_attributes
    --deselect test/contrib/test_pyopenssl.py::TestSSL::test_ssl_read_timeout
    --deselect test/with_dummyserver/test_socketlevel.py::TestSSL::test_ssl_read_timeout
    # Tests hang and need an adjusted backported patch for pytest8
    # https://github.com/urllib3/urllib3/commit/8c2088622059860e5411c8e37b26e402a5dda0bb
    --deselect test/with_dummyserver/test_socketlevel.py::TestSSL::*
    --deselect test/with_dummyserver/test_socketlevel.py::TestClients::*
    --deselect test/contrib/test_pyopenssl.py::TestClientCerts::test_client_certs_two_files
    --deselect test/contrib/test_pyopenssl.py::TestClientCerts::test_client_certs_one_file
    --deselect test/contrib/test_pyopenssl.py::TestClientCerts::test_missing_client_certs_raises_error
    --deselect test/contrib/test_pyopenssl.py::TestClientCerts::test_client_cert_with_string_password
    --deselect test/contrib/test_pyopenssl.py::TestClientCerts::test_client_cert_with_bytes_password
    --deselect test/contrib/test_pyopenssl.py::TestSSL::test_ssl_failure_midway_through_conn
    --deselect test/contrib/test_pyopenssl.py::TestSSL::test_retry_ssl_error
    --deselect test/contrib/test_pyopenssl.py::TestSSL::test_requesting_large_resources_via_ssl
    --deselect test/with_dummyserver/test_proxy_poolmanager.py::TestHTTPProxyManager::test_scheme_host_case_insensitive
    --deselect test/with_dummyserver/test_socketlevel.py::TestClientCerts::test_client_certs_two_files
    --deselect test/with_dummyserver/test_socketlevel.py::TestClientCerts::test_client_certs_one_file
    --deselect test/with_dummyserver/test_socketlevel.py::TestClientCerts::test_missing_client_certs_raises_error
    --deselect test/with_dummyserver/test_socketlevel.py::TestClientCerts::test_client_cert_with_string_password
    --deselect test/with_dummyserver/test_socketlevel.py::TestClientCerts::test_client_cert_with_bytes_password
    --deselect test/with_dummyserver/test_socketlevel.py::TestProxyManager::test_connect_reconn
    --deselect test/with_dummyserver/test_socketlevel.py::TestProxyManager::test_connect_ipv6_addr
    --deselect test/with_dummyserver/test_proxy_poolmanager.py::TestIPv6HTTPProxyManager::test_basic_ipv6_proxy
    --deselect test/with_dummyserver/test_socketlevel.py::TestSSL::test_ssl_failure_midway_through_conn
    --deselect test/with_dummyserver/test_socketlevel.py::TestProxyManager::test_https_proxymanager_connected_to_http_proxy
    --deselect test/with_dummyserver/test_socketlevel.py::TestSSL::test_retry_ssl_error
    --deselect test/with_dummyserver/test_socketlevel.py::TestSSL::test_ssl_failed_fingerprint_verification
    --deselect test/with_dummyserver/test_socketlevel.py::TestSSL::test_ssl_custom_validation_failure_terminates
    --deselect test/with_dummyserver/test_socketlevel.py::TestSSL::test_requesting_large_resources_via_ssl
    --deselect test/test_connection.py::TestConnection::test_recent_date
    --deselect test/test_no_ssl.py::TestImportWithoutSSL::test_cannot_import_ssl
    --deselect test/contrib/test_pyopenssl.py::TestSSL::test_ssl_failed_fingerprint_verification
    --deselect test/contrib/test_pyopenssl.py::TestSSL::test_ssl_custom_validation_failure_terminates
  )
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")

  cd $_name-$pkgver
  # install to temporary location, as importlib is used
  python -m installer --destdir=test_dir dist/*.whl
  export PYTHONPATH="$PWD/test_dir/$site_packages:$PYTHONPATH"
  pytest "${pytest_options[@]}"
}

package() {
  cd $_name-$pkgver
  python -m installer --destdir="$pkgdir" dist/*.whl
  install -Dm644 LICENSE.txt -t "$pkgdir"/usr/share/licenses/$pkgname/
}
