SUMMARY="A Python library for measuring code coverage"
DESCRIPTION="coverage is tool for measuring code coverage of Python programs."
HOMEPAGE="https://github.com/nedbat/coveragepy
	https://coverage.readthedocs.io/
	https://pypi.org/project/coverage/"
COPYRIGHT="2001 Gareth Rees
	2004-2019 Ned Batchelder"
LICENSE="Apache v2"
REVISION="1"
SOURCE_URI="https://github.com/nedbat/coveragepy/archive/coverage-$portVersion.tar.gz"
CHECKSUM_SHA256="2234feba7b3cbfe71ed54c6b752b50fd365f4c6c49301b1fb1ad490aaaf4faad"
SOURCE_DIR="coveragepy-coverage-$portVersion"

ARCHITECTURES="all"
SECONDARY_ARCHITECTURES="x86"

PROVIDES="
	coverage$secondaryArchSuffix = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	"
BUILD_PREREQUIRES="
	cmd:gcc$secondaryArchSuffix
	"

PYTHON_PACKAGES=()
PYTHON_VERSIONS=()
PYTHON_LIBSUFFIXES=()
# We don't have python2 for secondaryArch
if [ -z "$secondaryArchSuffix" ]; then
	PYTHON_PACKAGES+=(python)
	PYTHON_VERSIONS+=(2.7)
	PYTHON_LIBSUFFIXES+=("")
	BUILD_REQUIRES+="
		setuptools_python
		"
	BUILD_PREREQUIRES+="
		cmd:python2
		"
fi
# gcc2 does not support the flags passed by python3
if [ "$effectiveTargetArchitecture" != x86_gcc2 ]; then
	PYTHON_PACKAGES+=(python3)
	PYTHON_VERSIONS+=(3.7)
	PYTHON_LIBSUFFIXES+=(m)
	BUILD_REQUIRES+="
		setuptools_python3
		"
	BUILD_PREREQUIRES+="
		cmd:python3
		"
fi
if [ "$targetArchitecture" = x86_gcc2 -a -n "$secondaryArchSuffix" ]; then
	PROVIDES_python3="
		coverage_python3 = $portVersion
		"
fi
for i in "${!PYTHON_PACKAGES[@]}"; do
	pythonPackage=${PYTHON_PACKAGES[i]}
	pythonVersion=${PYTHON_VERSIONS[$i]}
	pythonLibSuffix=${PYTHON_LIBSUFFIXES[$i]}

	eval "PROVIDES_$pythonPackage+=\"
		${portName}_$pythonPackage = $portVersion
		cmd:coverage${pythonVersion%.*}
		cmd:coverage_$pythonVersion
		\""
	if [ $pythonPackage = python3 ]; then
		PROVIDES_python3+="
			cmd:coverage
			"
	fi
	eval "REQUIRES_$pythonPackage=\"
		haiku$secondaryArchSuffix
		lib:libpython$pythonVersion$pythonLibSuffix$secondaryArchSuffix
		\""
done

BUILD()
{
	for i in "${!PYTHON_PACKAGES[@]}"; do
		pythonPackage=${PYTHON_PACKAGES[i]}
		pythonVersion=${PYTHON_VERSIONS[$i]}

		rm -rf "$sourceDir"-$pythonPackage
		cp -a "$sourceDir" "$sourceDir"-$pythonPackage
		cd "$sourceDir"-$pythonPackage

		python=python$pythonVersion
		$python setup.py build
	done
}

INSTALL()
{
	for i in "${!PYTHON_PACKAGES[@]}"; do
		pythonPackage=${PYTHON_PACKAGES[i]}
		pythonVersion=${PYTHON_VERSIONS[$i]}

		cd "$sourceDir"-$pythonPackage

		python=python$pythonVersion
		installLocation=$prefix/lib/$python/vendor-packages/
		export PYTHONPATH=$installLocation
		mkdir -p "$installLocation"
		$python setup.py install \
			--optimize=1 \
			--root=/ --prefix="$prefix"

		if [ $pythonPackage = python ]; then
			rm "$prefix"/bin/coverage
		fi

		packageEntries $pythonPackage \
			"$prefix"/bin \
			"$prefix"/lib/$python
	done

	if [ -z "$secondaryArchSuffix" ]; then
		install -m 755 -d "$docDir"
		install -m 644 -t "$docDir" README.rst
	fi
}
