## START: Set by rpmautospec
## (rpmautospec version 0.3.5)
## RPMAUTOSPEC: autorelease, autochangelog
%define autorelease(e:s:pb:n) %{?-p:0.}%{lua:
    release_number = 7;
    base_release_number = tonumber(rpm.expand("%{?-b*}%{!?-b:1}"));
    print(release_number + base_release_number - 1);
}%{?-e:.%{-e*}}%{?-s:.%{-s*}}%{!?-n:%{?dist}}
## END: Set by rpmautospec

# Sphinx-generated HTML documentation is not suitable for packaging; see
# https://bugzilla.redhat.com/show_bug.cgi?id=2006555 for discussion.
#
# We could generate PDF documentation as a substitute, except that
# python-sphinxcontrib-asyncio is incompatible with Sphinx 6.1.3 and later
# (https://bugzilla.redhat.com/show_bug.cgi?id=2180497).
%bcond_with doc_pdf

Name:           python-asyncpg
Summary:        A fast PostgreSQL Database Client Library for Python/asyncio
Version:        0.27.0
Release:        %autorelease

# The entire source is Apache-2.0, except:
# - asyncpg/protocol/record/recordobj.c is PSF-2.0
License:        Apache-2.0 AND PSF-2.0
URL:            https://github.com/MagicStack/asyncpg
Source:         %{pypi_source asyncpg}

BuildRequires:  gcc
BuildRequires:  python3-devel

%if %{with doc_pdf}
BuildRequires:  make
BuildRequires:  python3-sphinx-latex
BuildRequires:  latexmk
%endif

# For tests:
#
# For pg_config binary
BuildRequires:  libpq-devel
# For pg_ctl binary
BuildRequires:  postgresql-server
# For citext extension
BuildRequires:  postgresql-contrib

# Note that asyncpg/pgproto comes from a git submodule referencing a separate
# project, https://github.com/MagicStack/py-pgproto. However, we do not treat
# it as a bundled dependency because it contains only sources; it has no build
# system and is not designed for separate installation; and it is managed as a
# part of the asyncpg package, as evidenced by the comment “This module is part
# of asyncpg” in the file headers.

%global common_description %{expand:
asyncpg is a database interface library designed specifically for PostgreSQL
and Python/asyncio. asyncpg is an efficient, clean implementation of PostgreSQL
server binary protocol for use with Python’s asyncio framework. You can read
more about asyncpg in an introductory blog post
http://magic.io/blog/asyncpg-1m-rows-from-postgres-to-python/.}

%description %{common_description}


%package -n     python3-asyncpg
Summary:        %{summary}

%if %{without doc_pdf}
Obsoletes:      %{name}-doc < 0.27.0-5
%endif

%description -n python3-asyncpg %{common_description}


%package doc
Summary:        Documentation for %{name}

BuildArch:      noarch

%description doc %{common_description}


%prep
%autosetup -n asyncpg-%{version}

# Remove pre-generated C sources from Cython to ensure they are re-generated
# and not used in the build. Note that recordobj.c is not a generated source,
# and must not be removed!
find asyncpg -type f -name '*.c' ! -name 'recordobj.c' -print -delete

# Do not put the source directory at the front of the path, as this keeps us
# from using our own PYTHONPATH setting to allow importing the compiled
# extension modules.
sed -r -i 's|(sys\.path\.)insert\(0,[[:blank:]]*|\1append\(|' \
    docs/conf.py
# Drop intersphinx mappings, since we can’t download remote inventories and
# can’t easily produce working hyperlinks from inventories in local
# documentation packages.
echo 'intersphinx_mapping.clear()' >> docs/conf.py

# Loosen SemVer pins to allow newer versions of Sphinx-related dependencies.
sed -r -i 's/([Ss]phinx.*)~=/\1>=/g' setup.py

# We will not run style linting tests since they are brittle, so we might as
# well drop the corresponding dependencies.
sed -r -i '/(pycodestyle|flake8)/d' setup.py


%generate_buildrequires
# Note dev extra includes doc and test extras
%pyproject_buildrequires -x dev


%build
%pyproject_wheel

%if %{with doc_pdf}
BLIB="${PWD}/build/lib.%{python3_platform}-cpython-%{python3_version_nodots}"
PYTHONPATH="${BLIB}" %make_build -C docs latex \
        SPHINXBUILD='sphinx-build' SPHINXOPTS='-j%{?_smp_build_ncpus}'
%make_build -C docs/_build/latex LATEXMKOPTS='-quiet'
%endif


%install
%pyproject_install
%pyproject_save_files asyncpg


%check
# It is not clear why the tests always import asyncpg as ../asyncpg/__init__.py
# even if we set PYTHONPATH to the installed sitearch directory. This
# workaround is ugly, but there is nothing actually wrong with it, as the
# install is already done by the time the check section runs:
rm -rf asyncpg
ln -s %{buildroot}%{python3_sitearch}/asyncpg/

# Do not run flake8 code style tests, which may fail; besides, we have patched
# flake8 and pycodestyle out of the test dependencies
k="${k-}${k+ and }not TestFlake8"

%pytest -k "${k}"


%files -n python3-asyncpg -f %{pyproject_files}


%if %{with doc_pdf}
%files doc
%license LICENSE
%doc README.rst
%doc docs/_build/latex/asyncpg.pdf
%endif


%changelog
* Sat Jun 03 2023 Benjamin A. Beasley <code@musicinmybrain.net> - 0.27.0-7
- Remove explicit %%set_build_flags, not needed since F36

* Tue Mar 21 2023 Benjamin A. Beasley <code@musicinmybrain.net> - 0.27.0-5
- Drop the -doc subpackage in F39
- Works around RHBZ#2180497.

* Fri Mar 17 2023 Benjamin A. Beasley <code@musicinmybrain.net> - 0.27.0-4
- Don’t assume %%_smp_mflags is -j%%_smp_build_ncpus

* Wed Mar 08 2023 Benjamin A. Beasley <code@musicinmybrain.net> - 0.27.0-3
- Drop %%%%pyproject_build_lib (F37+)

* Fri Jan 20 2023 Fedora Release Engineering <releng@fedoraproject.org> - 0.27.0-2
- Rebuilt for https://fedoraproject.org/wiki/Fedora_38_Mass_Rebuild

* Sat Oct 29 2022 Benjamin A. Beasley <code@musicinmybrain.net> - 0.27.0-1
- Update to 0.27.0 (close RHBZ#2138024)

* Fri Oct 21 2022 Benjamin A. Beasley <code@musicinmybrain.net> - 0.26.0-4
- Drop test skip for upstream issue #877
- The problem was in a GCC 12 pre-release, and was resolved by GCC 12 final

* Fri Oct 21 2022 Benjamin A. Beasley <code@musicinmybrain.net> - 0.26.0-3
- Update License to SPDX

* Fri Jul 22 2022 Fedora Release Engineering <releng@fedoraproject.org> - 0.26.0-2
- Rebuilt for https://fedoraproject.org/wiki/Fedora_37_Mass_Rebuild

* Thu Jul 07 2022 Benjamin A. Beasley <code@musicinmybrain.net> - 0.26.0-1
- Update to 0.26.0 (close RHBZ#2105049)

* Mon Jun 13 2022 Python Maint <python-maint@redhat.com> - 0.25.0-10
- Rebuilt for Python 3.11

* Fri Jan 21 2022 Fedora Release Engineering <releng@fedoraproject.org> - 0.25.0-9
- Rebuilt for https://fedoraproject.org/wiki/Fedora_36_Mass_Rebuild

* Sun Jan 16 2022 Benjamin A. Beasley <code@musicinmybrain.net> - 0.25.0-8
- Skip a failing test on s390x with GCC 12

* Fri Jan 14 2022 Benjamin A. Beasley <code@musicinmybrain.net> - 0.25.0-7
- Use provisional %%%%pyproject_build_lib

* Fri Jan 07 2022 Benjamin A. Beasley <code@musicinmybrain.net> - 0.25.0-6
- Drop intersphinx mappings

* Fri Dec 10 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.25.0-5
- Do package Cython sources (.pyx,.pxd,.pxi)

* Tue Dec 07 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.25.0-4
- Simplify documentation build

* Tue Dec 07 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.25.0-3
- Trivial adjustment to test exclusion

* Sat Nov 27 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.25.0-2
- Reduce LaTeX PDF build verbosity

* Wed Nov 17 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.25.0-1
- Update to 0.25.0 (close RHBZ#2023992)

* Mon Oct 25 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.24.0-9
- Use %%%%python3 macro instead of %%%%__python3

* Wed Oct 13 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.24.0-8
- Do not BR flake8/pycodestyle (close RHBZ#2013762)

* Fri Oct 01 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.24.0-7
- Generate PDF instead of HTML Sphinx documentation.

* Thu Sep 23 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.24.0-6
- Allow newer Sphinx and sphinx_rtd_theme

* Mon Sep 13 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.24.0-5
- Let pyproject-rpm-macros handle the license file

* Sun Sep 12 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.24.0-4
- Drop BR on pyproject-rpm-macros, now implied by python3-devel

* Wed Sep 08 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.24.0-3
- Drop patch that used sphinxcontrib-trio for docs

* Wed Sep 08 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.24.0-2
- Reduce macro indirection in the spec file

* Thu Aug 26 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.24.0-1
- Update to 0.24.0 (close RHBZ#1991810)

* Tue Jul 27 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.23.0-12
- Move %%generate_buildrequires after %%prep to make the spec file easier
  to follow

* Fri Jul 23 2021 Fedora Release Engineering <releng@fedoraproject.org> - 0.23.0-11
- Rebuilt for https://fedoraproject.org/wiki/Fedora_35_Mass_Rebuild

* Fri Jul 16 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.23.0-6
- Dependency on python3-docs should have been in -doc subpackage

* Fri Jul 02 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.23.0-5
- Allow later versions of test dependencies pycodestyle and uvloop

* Thu Jun 17 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.23.0-4
- Use a patch instead of sed expressions to replace sphinxcontrib-asyncio with
  sphinxcontrib-trio, so it is easier to notice if the upstream
  sphinxcontrib-asyncio version requirement changes and we do not silently
  switch back to sphinxcontrib-asyncio from sphinxcontrib-trio
- Stop removing bundled egg-info, since this should not be required with
  pyproject-rpm-macros
- Loosen pinned versions selectively rather than across the board

* Thu Jun 17 2021 Karolina Surma <ksurma@redhat.com> - 0.23.0-3
- Generate correct BuildRequires by defining them in source setup.py

* Fri Jun 04 2021 Python Maint <python-maint@redhat.com> - 0.23.0-2
- Rebuilt for Python 3.10

* Mon May 17 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.23.0-1
- Update to 0.23.0
- Drop asyncpg-0.22.0-python3.10-test_invalid_input.patch since it was merged
  upstream
- Drop workaround (via extra sources) for missing documentation sources

* Tue Apr 27 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.22.0-6
- Updated patch for Python 3.10

* Mon Apr 26 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.22.0-5
- Patch for Python 3.10 compatibility (RHBZ#1953538, upstream issue #750)

* Tue Mar 30 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.22.0-4
- Use pyproject-rpm-macros for build and install, too
- More complete documentation build

* Tue Mar 16 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.22.0-3
- Remove setuptools BR, redundant with %%pyproject_buildrequires

* Wed Mar 03 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.22.0-2
- Fix intersphinx inventory path

* Tue Mar 02 2021 Benjamin A. Beasley <code@musicinmybrain.net> - 0.22.0-1
- Initial package