# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Contributor: Ionut Biru <ibiru@archlinux.org>
# Contributor: Massimiliano Torromeo < massimiliano DOT torromeo AT gmail DOT com >

_name=beaker
pkgname=python-beaker
pkgver=1.13.0
pkgrel=6
arch=('any')
license=('BSD-3-Clause')
pkgdesc="Caching and sessions WSGI middleware for use with web applications and stand-alone Python scripts and applications"
url="https://beaker.readthedocs.io/en/latest/"
depends=('python')
makedepends=('git' 'python-build' 'python-installer' 'python-setuptools' 'python-wheel')
checkdepends=('python-pytest' 'python-sqlalchemy'
              'python-pycryptodome' 'python-coverage' 'python-webtest' 'python-redis'
              'python-pymongo' 'python-pylibmc' 'valkey' 'python-cryptography'
              'python-memcached' 'memcached' 'pifpaf' 'python-mongomock')
source=("git+https://github.com/bbangert/beaker.git#tag=$pkgver")
sha512sums=('146454c163ad10c23e7594a5258e2d9b216f14d38871ed55bd2021bd6ad63be8d5e185a6a9d58286d0591cd92e9046a1d93ccfabfcba74fca28aa845f93def3c')

prepare() {
  cd beaker

  # remove pkg_resources use (cherry-pick from 96283fe, excluding CHANGELOG)
  git show 96283fe89240040bf979e8555ba73571876e837c -- beaker/cache.py | git apply

  # Use a fake MongoDB for tests
  sed -e '/class TestMongoDB/i import mongomock' \
      -e "s|'mongodb://localhost:27017/beaker_testdb'|mongomock.MongoClient('mongodb://localhost:27017/beaker_testdb')|" \
      -i tests/test_managers/test_ext_mongodb.py

  # Drop tests which require a redis cluster
  rm tests/test_managers/test_ext_rediscluster.py

  sed -i "s#/usr/bin/python#/usr/bin/python3#" beaker/crypto/pbkdf2.py
}

build() {
  cd beaker
  python -m build --wheel --no-isolation
}

check() {
  cd beaker
  # it_IT.UTF-8 is missing in test env
  # test_dbm_container fails with SQLite objects created in a thread can only be used in that same thread.
  pifpaf run memcached --port 11211 -- pifpaf run redis -- \
    pytest --deselect tests/test_cookie_expires.py::test_cookie_expires_different_locale \
	   --deselect tests/test_container.py::test_dbm_container \
	   --deselect tests/test_container.py::test_dbm_container_2 \
           --deselect tests/test_container.py::test_dbm_container_3
}

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