# The gevent tests are automatically skipped when gevent is not installed.
# When bootstrapping new Python versions, gevent is often not available until
# very late. This bcond allows to ship zmq without gevent when needed.
%bcond gevent 1

Name:           python-zmq
Version:        22.3.0
Release:        %autorelease
Summary:        Python bindings for zeromq

# As noted in https://github.com/zeromq/pyzmq/blob/v26.2.0/RELICENSE/README.md:
#   pyzmq starting with 26.0.0 is fully licensed under the 3-clause Modified
#   BSD License. A small part of the core (Cython backend only) was previously
#   licensed under LGPLv3 for historical reasons. Permission has been granted
#   by the contributors of the vast majority of those components to relicense
#   under MPLv2 or BSD. This backend has been completely replaced in pyzmq 26,
#   and the new implementation is fully licensed under BSD-3-Clause, so pyzmq
#   is now under a single license.
# Since this package is currently at 22.3.0, the entire source is BSD-3-Clause
# (LICENSE.BSD), except:
#   - Some core files (the low-level Cython bindings) are LGPL-3.0-or-later
#     (LICENSE.LESSER); based on comments in file headers, this is:
#     * zmq/backend/cython/_device.pyx.
#     * zmq/include/zmq.h
#     * zmq/include/zmq_utils.h
#   - zmq/ssh/forward.py, which is derived from a Paramiko demo, is
#     LGPL-2.1-or-later
#   - The following are Apache-2.0:
#     * zmq/eventloop/minitornado/ioloop.py
#     * zmq/eventloop/minitornado/platform/auto.py
#     * zmq/eventloop/minitornado/platform/interface.py
#     * zmq/eventloop/minitornado/platform/posix.py
#     * zmq/eventloop/minitornado/stack_context.py
#     * zmq/eventloop/zmqstream.py
# Additionally, the following do not affect the license of the binary RPMs:
#   - tools/run_with_env.cmd is CC0-1.0; for distribution in the source RPM, it
#     is covered by “Existing uses of CC0-1.0 on code files in Fedora packages
#     prior to 2022-08-01, and subsequent upstream versions of those files in
#     those packages, continue to be allowed. We encourage Fedora package
#     maintainers to ask upstreams to relicense such files.”
#     https://gitlab.com/fedora/legal/fedora-license-data/-/issues/91#note_1151947383
#   - examples/device/device.py and examples/win32-interrupt/display.py are
#     LicenseRef-Fedora-Public-Domain; submitted for review in “Review of
#     python-zmq examples dedicated to the public domain,”
#     https://gitlab.com/fedora/legal/fedora-license-data/-/issues/616
#   - The following are BSD-2-Clause, but are removed in %%prep:
#     * bundled/zeromq/external/wepoll/
#   - The following are LGPL-3.0-or-later, but are removed in %%prep:
#     * bundled/zeromq/, except bundled/zeromq/external/wepoll/
License:        %{shrink:
                BSD-3-Clause AND
                LGPL-3.0-or-later AND
                LGPL-2.1-or-later AND
                Apache-2.0
                }
URL:            https://zeromq.org/languages/python/
Source0:        %{pypi_source pyzmq}
# BUG: A file is licensed Apache-2.0, but the license text is not distributed
# https://github.com/zeromq/pyzmq/issues/2048
Source1:        https://www.apache.org/licenses/LICENSE-2.0.txt#/LICENSE.Apache-2.0

BuildRequires:  gcc

BuildRequires:  pkgconfig(libzmq)

BuildRequires:  python%{python3_pkgversion}-devel
BuildRequires:  python%{python3_pkgversion}-pytest
BuildRequires:  python%{python3_pkgversion}-tornado
BuildRequires:  python%{python3_pkgversion}-numpy

%if %{with gevent}
BuildRequires:  python%{python3_pkgversion}-gevent
%endif

%global common_description %{expand:
This package contains Python bindings for ZeroMQ. ØMQ is a lightweight and fast
messaging implementation.}

%description %{common_description}


%package -n python%{python3_pkgversion}-zmq
Summary:        %{summary}
License:        LGPLv3+
%py_provides    python%{python3_pkgversion}-pyzmq

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


%package -n python%{python3_pkgversion}-zmq-tests
Summary:        %{summary}, testsuite
License:        LGPLv3+
Requires:       python%{python3_pkgversion}-zmq = %{version}-%{release}
%py_provides    python%{python3_pkgversion}-pyzmq-tests

%description -n python%{python3_pkgversion}-zmq-tests %{common_description}

This package contains the test suite for the Python bindings.


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

# remove bundled libraries
rm -rv bundled/

# Remove any Cython-generated .c files in order to regenerate them:
find zmq -name '*.c' -print -delete

# Remove shebangs from non-script sources. The find-then-modify pattern
# preserves mtimes on sources that did not need to be modified.
find 'src' -type f -name '*.py' \
    -exec gawk '/^#!/ { print FILENAME }; { nextfile }' '{}' '+' |
  xargs -r sed -r -i '1{/^#!/d}'

# BUG: A file is licensed Apache-2.0, but the license text is not distributed
# https://github.com/zeromq/pyzmq/issues/2048
cp -p '%{SOURCE1}' .


%generate_buildrequires
%pyproject_buildrequires


%build
%pyproject_wheel


%install
%pyproject_install
%pyproject_save_files -l zmq


%check
# to avoid partially initialized zmq module from cwd
mkdir -p _empty
cd _empty
ln -s %{buildroot}%{python3_sitearch}/zmq/ ../pytest.ini ./

# test_draft seems to get incorrectly run:
# https://github.com/zeromq/pyzmq/issues/1853
k="${k-}${k+ and }not test_draft"

# Whatever it takes to build the Cython extension tests/cython_ext.pyx that is
# *in the tests*, we aren’t doing it. This goes away in later releases, so we
# don’t put any effort into figuring out how to make it work.
k="${k-}${k+ and }not test_cython"

%pytest -k "${k-}" -v -rs zmq/tests


%files -n python%{python3_pkgversion}-zmq -f %{pyproject_files}
%doc README.md
%exclude %{python3_sitearch}/zmq/tests/


%files -n python%{python3_pkgversion}-zmq-tests
%{python3_sitearch}/zmq/tests/


%changelog
%autochangelog