%{!?__python2: %global __python2 /usr/bin/python2}
%{!?python2_sitelib: %global python2_sitelib %(%{__python2} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")}

# rhmsg lib:
# fedora and rhel>7:  python 3
# fedora and rhel<=7: python 2
#
# rhmsg-cli:
# fedora and rhel>7:  python 3
# rhel<=7:            python 2

%if 0%{?fedora} || 0%{?rhel} > 7
%global with_python3 1
%else
%global with_python3 0
%endif

%if 0%{?fedora} > 29 || 0%{?rhel} > 7
%global with_python2 0
%else
%global with_python2 1
%endif

%if 0%{?with_python3}
%{!?__python3: %global __python3 /usr/bin/python3}
%{!?python3_sitelib: %global python3_sitelib %(%{__python3} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")}
%endif

%define src_name rhmsg

Summary: A Python module interacting with ActiveMQ
Name: python-%{src_name}
Version: 0.10.1
Release: 2%{?dist}
Source0: http://git.app.eng.bos.redhat.com/git/rhmsg.git/snapshot/%{src_name}-%{version}.tar.gz
License: GPLv3
Group: Development/Libraries
Url: https://code.engineering.redhat.com/gerrit/gitweb?p=rhmsg.git;a=summary
BuildArch: noarch

%description
A Python module interacting with ActiveMQ

%if 0%{?with_python2}
%package -n python2-%{src_name}
Summary: A Python module interacting with ActiveMQ

BuildRequires:  python-setuptools
%if 0%{?fedora}
BuildRequires:  python2-mock
BuildRequires:  python2-pytest
BuildRequires:  python2-six
BuildRequires:  python2-qpid-proton >= 0.13.0
BuildRequires:  python2-requests
%else
BuildRequires:  pytest
BuildRequires:  python-mock
BuildRequires:  python-requests
BuildRequires:  python-six
BuildRequires:  python-qpid-proton >= 0.13.0
BuildRequires:  python-unittest2
%endif

Requires:       python-qpid-proton >= 0.13.0
Requires:       python-setuptools

Provides:       python-%{src_name}
Obsoletes:      %{name} <= 0.5

%description -n python2-%{src_name}
A Python 2 module interacting with ActiveMQ

%endif  # end of with_python2

%if 0%{?with_python3}
%package -n python3-%{src_name}
Summary: A Python module interacting with ActiveMQ

BuildRequires:  python3-mock
BuildRequires:  python3-pytest
BuildRequires:  python3-qpid-proton >= 0.13.0
BuildRequires:  python3-requests
BuildRequires:  python3-setuptools
BuildRequires:  python3-six

Requires:       python3-qpid-proton >= 0.13.0
Requires:       python3-setuptools

%description -n python3-%{src_name}
A Python 3 module interacting with ActiveMQ

%endif # end of python3 subpackage

%package -n %{src_name}-cli
Summary: rhmsg command line utitlies to interact with Unified Message Bus

%if 0%{?with_python2}
Requires:       python-requests

%if 0%{?rhel} && 0%{?rhel} <= 7
Requires:       python2-%{src_name} = %{version}
%endif
%endif

%if 0%{?with_python3}
Requires:       python3-requests
Requires:       python3-%{src_name} = %{version}
%endif


%description -n %{src_name}-cli
rhmsg command line utitlies to interact with Unified Message Bus


%prep
%setup -q -n %{src_name}-%{version}


%build
%if 0%{?with_python2}
%{__python2} setup.py build
%endif

%if 0%{?with_python3}
%{__python3} setup.py build
%endif

%check
%if 0%{?with_python2}
PYTHONPATH=. py.test
%endif

%if 0%{?with_python3}
PYTHONPATH=. py.test-3
%endif


%install
%if 0%{?with_python2}
%{__python2} setup.py install -O1 --root=$RPM_BUILD_ROOT
%endif

# Order is important here. If python 3 is enabled, rhmsg-cli is default to
# python 3. So, following install command will overwrite python 2 binaries just
# installed in above step.

%if 0%{?with_python3}
%{__python3} setup.py install -O1 --root=$RPM_BUILD_ROOT

%if 0%{?with_python2}
rm -r $RPM_BUILD_ROOT/%{python2_sitelib}/%{src_name}/cli/
%endif
%endif


%clean


%if 0%{?with_python2}
%files -n python2-%{src_name}
%defattr(-,root,root)
%doc README.rst AUTHORS.rst CHANGELOG.rst doc/
%{python2_sitelib}/%{src_name}/
%{python2_sitelib}/%{src_name}-%{version}-*.egg-info
%endif


%if 0%{?with_python3}
%files -n python3-%{src_name}
%defattr(-,root,root)
%doc README.rst AUTHORS.rst CHANGELOG.rst doc/
%{python3_sitelib}/%{src_name}/
%{python3_sitelib}/%{src_name}-%{version}-*.egg-info
%endif


%files -n %{src_name}-cli
%{_bindir}/amq-consumer
%{_bindir}/amq-producer
%{_bindir}/rhmsg-replay

%changelog
* Thu Apr 02 2020 Chenxiong Qi <cqi@redhat.com> - 0.10.1-2
- Own rhmsg/ by lib

* Mon Nov 25 2019 Chenxiong Qi <cqi@redhat.com> - 0.10-1
- Install rhmsg with dependent packages together (Chenxiong Qi)
- Address review comments to command line options help (Chenxiong Qi)
- Let git ignore more files (Chenxiong Qi)
- Clean logger names (Chenxiong Qi)
- Resend all messages listed in a message file (Chenxiong Qi)
- Update command line options help (Chenxiong Qi)
- Migrate to argparse (Chenxiong Qi)
- Update docstring of ReceiverHandler (Chenxiong Qi)
- Use newer Fedora dist for CI (Chenxiong Qi)

* Wed Jan 02 2019 Chenxiong Qi <cqi@redhat.com> - 0.9-1
- SSLDomain moved to proton namespace (Chenxiong Qi)
- Ignore these files, produced by tox. (Ralph Bean)
- Work with Python 3.7 (Chenxiong Qi)

* Thu Dec 20 2018 Chenxiong Qi <cqi@redhat.com> - 0.8-5
- Backport: 0001-SSLDomain-moved-to-proton-namespace.patch

* Wed Nov 07 2018 Ondřej Nosek <onosek@redhat.com> - 0.8-5
- Rebuilt to provide Python 2 package for Fedora<=29

* Thu Aug 09 2018 Chenxiong Qi <cqi@redhat.com> - 0.8-4
- Require python-requests in rhel<=7

* Tue Aug 07 2018 Chenxiong Qi <cqi@redhat.com> - 0.8-3
- Rebuilt to provide Python 3 package for Fedora<=28

* Mon Jul 30 2018 Chenxiong Qi <cqi@redhat.com> - 0.8-2
- Refactor to build Python 3 package in Fedora and RHEL8

* Tue Apr 03 2018 Chenxiong Qi <cqi@redhat.com> - 0.8-1
- enable creation of durable subscriptions (Mike Bonnet)
- Allow equality sign in property value in amq-producer (Chenxiong Qi)
- run tests on Fedora 27 also (Mike Bonnet)
- run tests on all available interpreters in Fedora (Mike Bonnet)
- Make doc of argument body more concrete in AMQProducer.send_msg (Chenxiong Qi)
- remove deprecated usage of Exception.message (Mike Bonnet)
- also run tests on Fedora 26 (Mike Bonnet)
- fix some refactoring errors, py3 incompatibilities, and flake8 errors (Mike Bonnet)
- fix tests to account for differences between python 2 and 3 (Mike Bonnet)
- fix intermittent unit test error caused by undefined ordering of entries in a dict (Mike Bonnet)
- send_msg() and send_msgs(): allow passing keyword arguments, which will be set as attributes on the underlying Message (Mike Bonnet)
- simplify Jenkinsfile (Mike Bonnet)
- Ignore htmlcov directory (Chenxiong Qi)
- Add LICENSE file (Chenxiong Qi)
- Move broker envs into separate module (Chenxiong Qi)
- add address to the message dump (Mike Bonnet)
- enable Cobertura reporting (Mike Bonnet)
- README: describe cert files and example invocation (Ken Dreyer)
- A Jenkinsfile for use with the rcm-tools-jenkins Jenkins master. (Mike Bonnet)

* Fri Feb 02 2018 Mike Bonnet <mikeb@redhat.com> - 0.7-3
- Allow setting attributes on the underlying message in send_msg() and send_msgs()

* Thu Nov 09 2017 Chenxiong Qi <cqi@redhat.com> - 0.7-2
- Obsoletes 0.5 and older versions

* Wed Oct 18 2017 Chenxiong Qi <cqi@redhat.com> - 0.7-1
- Fix a pep8 error (cqi)
- Allow to accept message manually - COMPOSE-1782 (cqi)
- Show test coverage (cqi)
- Run tests in various Python versions (cqi)
- Update doc make-release.rst (cqi)

* Fri Aug 4 2017 Chenxiong Qi <cqi@redhat.com> - 0.6-1
- Package: split cli to separate package
- Package: rename main package name to python2-rhmsg and python-rhmsg is still provided
- Able to read messages from a file (Chenxiong Qi)
- pretty-print received messages (Chenxiong Qi)
- Add requirements files (Chenxiong Qi)
- Add new CLI rhmsg-replay (Chenxiong Qi)
- Delete unuseful example scripts (Chenxiong Qi)
- fix tests to actually check the exception message (Mike Bonnet)
- improve error reporting in AMQProducer (Mike Bonnet)

* Fri May 19 2017 Mike Bonnet <mikeb@redhat.com> - 0.5-2
- include error messages in exception text when messages could not be sent

* Tue Mar 21 2017 Mike Bonnet <mikeb@redhat.com> - 0.5-1
- enable peer authentication in the consumer
- replace inconsistent --host/--url option with --env in the cli tools
- handle unicode message bodies
- rename the --it-cert option to --ca-certs in the cli tools
- fix problems with the default on_transport_error handler

* Fri Dec 02 2016 Mike Bonnet <mikeb@redhat.com> - 0.4-3
- fix problems with the default on_transport_error handler

* Tue Nov 15 2016 Mike Bonnet <mikeb@redhat.com> - 0.4-2
- call set_peer_authentication() after set_trusted_ca_db()

* Fri Nov 04 2016 Chenxiong Qi <cqi@redhat.com> - 0.4-1
- add failover support to AMQProducer (Mike Bonnet)

* Fri Sep 30 2016 Chenxiong Qi <cqi@redhat.com> - 0.3-1
- pass properties to the proton.Message class properly (Mike Bonnet)
- Ignore .cache directory (Chenxiong Qi)

* Tue Sep 20 2016 Chenxiong Qi <cqi@redhat.com> - 0.2-1
- allow specifying a timeout in the AMQProducer constructor (Mike Bonnet)
- send_msg(): send a single message (Mike Bonnet)
- allow specifying queue or topic in the AMQProducer constructor (Mike Bonnet)
- minor spelling fix (Mike Bonnet)
- initialize all attributes of AMQProducer in __init__() (Mike Bonnet)
- test cases for AMQProducer (Mike Bonnet)
- Fix incorrect module name in example (Chenxiong Qi)


* Thu Aug 25 2016 Chenxiong Qi <cqi@redhat.com> - 0.1-1
- Initial package