# 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.12.0
pkgrel=4
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=('python-setuptools')
checkdepends=('python-pytest' 'python-sqlalchemy'
              'python-pycryptodome' 'python-coverage' 'python-webtest' 'python-redis'
              'python-pymongo' 'python-pylibmc' 'redis' 'python-cryptography'
              'python-memcached' 'memcached' 'pifpaf' 'python-mongomock')
source=(
  "$pkgname-$pkgver.tar.gz::https://github.com/bbangert/beaker/archive/$pkgver.tar.gz"
  $pkgname-1.12.0-remove_mock.patch::https://github.com/bbangert/beaker/commit/056d68f954b778e79cb5dc64db9a485fe1f64915.patch
  $pkgname-1.12.0-remove_mock2.patch::https://github.com/bbangert/beaker/commit/c089668e6ebb92c6d813ce731ea3b676e0024b7a.patch
)
sha512sums=('d7ef5070f036ca7ea100c348df58aa98edad0ee8e361c0a600485b6e1de301f3381ecc782dcbc4b1b2735a4b54e8aff7fd516a7c9d655d2c10517e358ffe031c'
            '8d61b8201b14f04b3b62b751341ad683344b2d6a09cef45168be76e9d9d2b249240448a0e49df13e1dbba3005477dfa46a85349f77524db91db3eaabb664633f'
            'e59f5e5c5e0ede5ba243e6ec764152c07655529a365d90c14dff3d3134422b899ba0a902b35c1e1dffdb12a935ae66f6ef112f62e1d053d5fdd6b9c83973ef79')

prepare() {
  # remove the use of python-mock
  patch -Np1 -d $_name-$pkgver -i ../$pkgname-1.12.0-remove_mock.patch
  patch -Np1 -d $_name-$pkgver -i ../$pkgname-1.12.0-remove_mock2.patch

  # 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 beaker-$pkgver/tests/test_managers/test_ext_mongodb.py

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

build() {
  cd beaker-$pkgver
  python3 setup.py build
}

check() {
  cd beaker-$pkgver
  # it_IT.UTF-8 is missing in test env
  pifpaf run memcached --port 11211 -- pifpaf run redis -- \
    pytest --deselect tests/test_cookie_expires.py::test_cookie_expires_different_locale
}

package() {
  cd beaker-$pkgver
  python setup.py install --root="$pkgdir" --optimize=1
  install -Dm644 LICENSE -t "$pkgdir"/usr/share/licenses/python-beaker/
}
