%global python3_pkgversion 3.11
%global pkgname pygit2

Name:           python-%{pkgname}
Version:        1.14.0
Release:        2.ac16.py3.11%{?dist}
Summary:        Python bindings for libgit2

License:        GPL-2.0-only WITH GCC-exception-2.0
URL:            https://www.pygit2.org/
Source0:        https://github.com/libgit2/pygit2/archive/v%{version}.tar.gz#/%{pkgname}-%{version}.tar.gz
Patch0: python-pygit2-transfer-size.patch
# This patch lowers the Python-level dependency on cffi. Fedora package python-cffi-1.15.1-5 and
# higher contain the patches needed for Python >= 3.12.
# Remove when cffi >= 1.16.0 is available on all releases with Python 3.12.
Patch:          python-pygit2-cffiver.patch

BuildRequires:  make
BuildRequires:  gcc
BuildRequires:  (libgit2-devel >= 1.7.0 with libgit2-devel < 1.8.0)

%description
pygit2 is a set of Python bindings to the libgit2 library, which implements
the core of Git.


%package -n     python%{python3_pkgversion}-%{pkgname}
Summary:        Python 3 bindings for libgit2
%{?python_provide:%python_provide python3-%{pkgname}}
BuildRequires:  python%{python3_pkgversion}-pytest

%description -n python%{python3_pkgversion}-%{pkgname}
pygit2 is a set of Python bindings to the libgit2 library, which implements
the core of Git.

The python3-%{pkgname} package contains the Python 3 bindings.


%package        doc
Summary:        Documentation for %{name}
BuildArch:      noarch
BuildRequires:  /usr/bin/sphinx-build-%{python3_pkgversion}
BuildRequires:  python%{python3_pkgversion}-sphinx_rtd_theme

%description    doc
Documentation for %{name}.


%prep
%autosetup -n %{pkgname}-%{version} -p1


%generate_buildrequires
%pyproject_buildrequires


%build
sed -i -re "/SPHINXBUILD/ s|sphinx-build|sphinx-build-%{python3_pkgversion}|" docs/Makefile
%pyproject_wheel

make -C docs html


%install
%pyproject_install
find %{_builddir} -name '.buildinfo' -print -delete


%check
# This is horrible, but otherwise pytest does not use pygit2 from site-packages
rm -f pygit2/__init__.py
# https://github.com/libgit2/pygit2/issues/812
%ifarch ppc64 s390x
  PYTHONPATH=%{buildroot}%{python3_sitearch} py.test-%{python3_version} -v || :
%else
  PYTHONPATH=%{buildroot}%{python3_sitearch} py.test-%{python3_version} -v
%endif


for b in $(find %{buildroot}%{_bindir}/ -type f) ; do mv "$b" $(sed -re "s|(.*)$|\1-%{python3_pkgversion}|"<<<"$b"); done
for m in $(find %{buildroot}%{_mandir}/ -type f) ; do mv "$m" $(sed -re "s|(.*).([1-8])(.*)$|\1-%{python3_pkgversion}.\2\3|"<<<"$m"); done
for m in $(find %{buildroot}%{_datadir}/locale/* -type f) ; do mv "$m" $(sed -re "s|(.*).mo$|\1-%{python3_pkgversion}.mo|"<<<"$m"); done
for l in $(find %{buildroot}/usr/lib64/lib* -type f -o -type l) ; do mv "$l" $(sed -re "s|(.*).so(.*)$|\1-py%{python3_pkgversion}.so\2|"<<<"$l"); done
for p in $(find %{buildroot}/usr/lib64/pkgconfig/* -type f) ; do mv "$p" $(sed -re "s|(.*).pc$|\1-py%{python3_pkgversion}.pc|"<<<"$p"); done
%files -n python%{python3_pkgversion}-%{pkgname}
%license COPYING
%doc README.md
%{python3_sitearch}/%{pkgname}-*.dist-info/
%{python3_sitearch}/%{pkgname}/

%files doc
%license COPYING
%doc docs/_build/html/*


%changelog
* Sun May 12 2024 Ding-Yi Chen <dingyichen@gmail.com> -  1.14.0-2.ac16.py3.11
 - Cut corner packaging