# Contributor: Henrik Riomar <henrik.riomar@gmail.com>
# Maintainer: Henrik Riomar <henrik.riomar@gmail.com>
pkgname=py3-pkcs11
_pkgname=python-pkcs11
pkgver=0.7.0
pkgrel=8
pkgdesc="PKCS#11/Cryptoki support for Python"
url="https://github.com/danni/python-pkcs11"
arch="all"
license="MIT"
checkdepends="
	py3-oscrypto
	py3-pytest
	softhsm
	"
makedepends="
	cython
	py3-gpep517
	py3-setuptools_scm
	py3-wheel
	python3-dev
	"
depends="
	py3-asn1crypto
	py3-cached-property
	py3-cryptography
	python3
	"
subpackages="$pkgname-pyc"
source="
	https://github.com/danni/python-pkcs11/archive/v$pkgver/$_pkgname-$pkgver.tar.gz
	0001-test_x509-add-tzinfo-to-not_before-and-not_after-dat.patch
	0002-KeyType.EC_EDWARDS-in-test_sign_eddsa.patch
	0003-test_generate_params-use-size-1024.patch
	0004-tests-test_x509.py-disable-openssl-verify-test.patch
	0005-Fix-typo-in-_pkcs11.pyx.patch
	0006-fix-DeprecationWarning-in-test_ecdh.patch
	"
builddir="$srcdir/$_pkgname-$pkgver"

build() {
	export SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver
	gpep517 build-wheel \
		--wheel-dir .dist \
		--output-fd 3 3>&1 >&2
}

check() {
	# setup softhsm for tests
	tokendir=$(mktemp -d "$builddir/token.XXXXXX")
	conffile=$(mktemp "$builddir/conf.XXXXXX")
	cat > "$conffile" << EOF
directories.tokendir = $tokendir
objectstore.backend = file
EOF
	export SOFTHSM2_CONF=$conffile
	softhsm2-util --init-token --free --label pytest --pin 1234 --so-pin 987654321

	export PKCS11_TOKEN_LABEL=pytest
	export PKCS11_TOKEN_PIN=1234
	export PKCS11_TOKEN_SO_PIN=987654321

	export PKCS11_MODULE=/usr/lib/softhsm/libsofthsm2.so

	# do test installation
	export SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver
	python3 -m venv --clear --without-pip --system-site-packages .testenv
	.testenv/bin/python3 -m installer .dist/*.whl

	# run tests
	local _python_version
	_python_version="$(python3 -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')"
	PYTHONPATH="$builddir/.testenv/lib/python$_python_version/site-packages" \
	pytest-3 --import-mode=append
}

package() {
	export SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver
	python3 -m installer -d "$pkgdir" .dist/*.whl
}

sha512sums="
af8b300ab012226fbb0427f763f65151e9a1cb5605d62331c88d820c269bfabf7222af542212577e2e4e6c06b4f1c20c1852e53c4bcb25e30846fcda2788ac5f  python-pkcs11-0.7.0.tar.gz
ed0bbb955ed96da316158a2e1e75d0284c55151a8534773569ecb3b0c712f54f0eb4eff57c63e6523f749c8e484aa574b6d0614a29ed8e0c39ea9f9b7d075701  0001-test_x509-add-tzinfo-to-not_before-and-not_after-dat.patch
dd8c68ac64438f09d16d11b747ee0a12c57fde779cc4db20fa4422f35280e9b74745ad3a6b676457c91ef78e1a79574dbe7301d69b93fd0bcbca5beac910e950  0002-KeyType.EC_EDWARDS-in-test_sign_eddsa.patch
cb2e6ae12e054d4777a995c7fdd300af822b8969f20c2220a2a1879867c751de735d776e667b56fb01899d71fe147df9cc84ae56006c364c71fba089ea45a8e9  0003-test_generate_params-use-size-1024.patch
a62cf31bcfea83a9acf3522c6be96b2a82a35d188109de1fc1e2f5fe0a910cf82c040ebf330fdf085bf8da0fd33f00fc75c46cfc09c128a5d5171305860dfd27  0004-tests-test_x509.py-disable-openssl-verify-test.patch
84ee7b7889048170cccef09f0a36ed94adff654985a1ba03fcbba6c47ca7182f9ae59c91655ca3cb6e0d35288ec0e591b317bcbbdbcd21ca7e36a6c00e7d6194  0005-Fix-typo-in-_pkcs11.pyx.patch
60da53efc16e24107a77fe66e6de137d94f61c3c01c3bbbb145cdcf74f4085261aa58edd59a68204cc101146c318ed4591cd16a876db47f2f3f5f96cc4c66460  0006-fix-DeprecationWarning-in-test_ecdh.patch
"
