# Contributor: Iskren Chernev <me@iskren.info>
# Maintainer: Iskren Chernev <me@iskren.info>
pkgname=py3-dt-schema
pkgver=2025.02
pkgrel=0
pkgdesc="Devicetree schema tools"
url="https://github.com/devicetree-org/dt-schema"
arch="noarch"
license="BSD-2-Clause"
depends="python3 py3-ruamel.yaml py3-jsonschema417 py3-rfc3987 py3-libfdt"
makedepends="py3-setuptools py3-setuptools_scm py3-wheel py3-gpep517 py3-installer"
checkdepends="dtc"
subpackages="$pkgname-pyc"
source="dt-schema-$pkgver.tar.gz::https://github.com/devicetree-org/dt-schema/archive/v$pkgver.tar.gz"
builddir="$srcdir/dt-schema-$pkgver"
options="net"

prepare() {
	default_prepare
	{
		find dtschema -name '*.py'
		find test -name '*.py'
		find tools -type f
	} | while read -r file; do
		sed -i \
			-e 's/import jsonschema$/import jsonschema417 as jsonschema/' \
			-e 's/from jsonschema/from jsonschema417/' \
			"$file"
	done
	sed -i 's/jsonschema/jsonschema417/' pyproject.toml
	# The project uses setuptools_scm, which handles adding the schema
	# files to the package, but there is no SCM here..
	{
		echo '[tools.setuptools.package.find]'
		echo 'where=["."]'
	} >> pyproject.toml
	echo "recursive-include dtschema/*schemas *.yaml" > MANIFEST.in
}

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

check() {
	python3 -m venv --clear --without-pip --system-site-packages .testenv
	.testenv/bin/python3 -m installer .dist/*.whl
	.testenv/bin/python3 test/test-dt-validate.py
}

package() {
	SETUPTOOLS_SCM_PRETEND_VERSION="$pkgver" \
		python3 -m installer -d "$pkgdir" \
		.dist/dtschema-*-py3-none-any.whl
}

sha512sums="
e149d38fc003887c3e76fad2834461f5e1c58d7074228306834bf042c56a3f97fc3260babdf58c649de507b77e5024b6b42325c56d5d17142a59c251cf55d17a  dt-schema-2025.02.tar.gz
"
