%global python3_pkgversion 3.11
%global srcname testtools
%global common_description %{expand:
testtools is a set of extensions to the Python standard library's unit testing
framework.}

# To build this package in a new environment (i.e. a new EPEL branch), you'll
# need to build in a particular order.  Duplicate numbered steps can happen at
# the same time.
#
# 1. bootstrap python-extras
# 1. bootstrap python-fixtures
# 2. bootstrap python-testtools
# 3. python-extras
# 3. python-fixtures
# 3. python-testscenarios
# 4. python-testresources
# 5. python-testtools
%bcond_with bootstrap

Name:           python-%{srcname}
Version:        2.7.1
Release:        2.ac14.py3.11%{?dist}
Summary:        Extensions to the Python standard library unit testing framework
License:        MIT
URL:            https://github.com/testing-cabal/testtools

Source:         %pypi_source
# When rebasing patches, be aware that setup.cfg uses spaces in the git source,
# but tabs in the PyPI tarball.

BuildArch:      noarch

%description %{common_description}

%package -n python%{python3_pkgversion}-%{srcname}
Summary:        %{summary}
BuildRequires:  python%{python3_pkgversion}-devel

%description -n python%{python3_pkgversion}-%{srcname} %{common_description}

%if %{without bootstrap}
%package        doc
BuildRequires:  make
BuildRequires:  python%{python3_pkgversion}-sphinx
Summary:        Documentation for %{name}

# https://fedoraproject.org/wiki/Packaging:No_Bundled_Libraries#Packages_granted_temporary_exceptions
Provides:       bundled(jquery)

%description doc
This package contains HTML documentation for %{name}.
%endif


%prep
%autosetup -p 1 -n %{srcname}-%{version}


%generate_buildrequires
%pyproject_buildrequires %{!?with_bootstrap:-x test -x twisted}


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

%if %{without bootstrap}
make -C doc html
%endif


%install
%pyproject_install
%pyproject_save_files %{srcname}


%check
%if %{without bootstrap}
PYTHONPATH=%{buildroot}%{python3_sitelib} %{python3} -m testtools.run testtools.tests.test_suite
# Typically we would want an %%else condition to run an import check, but it
# will fail during the bootstrap phase, so leave it out.
%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}-%{srcname} -f %{pyproject_files}
%doc NEWS README.rst

%if %{without bootstrap}
%files doc
%doc doc/_build/html/*
%endif


%changelog
* Tue May 14 2024 Ding-Yi Chen <dingyichen@gmail.com> -  2.7.1-2.ac14.py3.11
 - Cut corner packaging