# Copyright 1999-2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

DISTUTILS_EXT=1
DISTUTILS_USE_PEP517=setuptools
PYPI_VERIFY_REPO=https://github.com/pyca/bcrypt
# TODO: freethreading
PYTHON_COMPAT=( python3_{12..15} )

RUST_MIN_VER="1.85.0"
CRATES="
	base64@0.23.1
	bcrypt-pbkdf@0.11.0
	bcrypt@0.19.3
	block-buffer@0.12.1
	blowfish@0.10.0
	byteorder@1.5.0
	cfg-if@1.0.5
	cipher@0.5.2
	cmov@0.5.4
	cpufeatures@0.3.1
	crypto-common@0.2.2
	ctutils@0.4.2
	digest@0.11.3
	getrandom@0.4.3
	heck@0.5.0
	hybrid-array@0.4.15
	inout@0.2.5
	libc@0.2.191
	once_cell@1.21.4
	pbkdf2@0.13.0
	portable-atomic@1.15.0
	proc-macro2@1.0.107
	pyo3-build-config@0.30.1
	pyo3-ffi@0.30.1
	pyo3-macros-backend@0.30.1
	pyo3-macros@0.30.1
	pyo3@0.30.1
	quote@1.0.47
	r-efi@6.0.0
	sha2@0.11.0
	subtle@2.6.1
	syn@3.0.9
	target-lexicon@0.13.5
	typenum@1.20.1
	unicode-ident@1.0.27
	zeroize@1.9.1
"

inherit cargo distutils-r1 pypi

DESCRIPTION="Modern password hashing for software and servers"
HOMEPAGE="
	https://github.com/pyca/bcrypt/
	https://pypi.org/project/bcrypt/
"
SRC_URI+="
	${CARGO_CRATE_URIS}
"

LICENSE="Apache-2.0"
# Dependent crate licenses
LICENSE+=" Apache-2.0-with-LLVM-exceptions BSD MIT Unicode-3.0"
SLOT="0"
KEYWORDS="~amd64 ~arm ~arm64 ~loong ~mips ~ppc ~ppc64 ~riscv ~s390 ~sparc ~x86"

BDEPEND="
	>=dev-python/setuptools-rust-1.7.0[${PYTHON_USEDEP}]
"

# Rust
QA_FLAGS_IGNORED="usr/lib.*/py.*/site-packages/bcrypt/_bcrypt.*.so"

EPYTEST_PLUGINS=()
distutils_enable_tests pytest

src_unpack() {
	pypi_src_unpack
	cargo_crate_unpack
	cargo_gen_config
}

src_prepare() {
	distutils-r1_src_prepare

	pushd "${ECARGO_VENDOR}"/pyo3-0*/ >/dev/null || die
	eapply "${FILESDIR}/bcrypt-5.1.0-patch-pyo3-subinterp.patch"
	popd >/dev/null || die
}

python_configure_all() {
	# Workaround for sys-cluster/ceph (bug #920906)
	# https://github.com/pyca/bcrypt/issues/694
	# https://github.com/PyO3/pyo3/issues/3451
	export RUSTFLAGS="${RUSTFLAGS} --cfg pyo3_unsafe_allow_subinterpreters"
}

python_test() {
	epytest tests
}
