## START: Set by rpmautospec
## (rpmautospec version 0.3.0)
%define autorelease(e:s:pb:n) %{?-p:0.}%{lua:
    release_number = 8;
    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

# Tests require interacting with a temporary MySQL database. We are able to do
# this, but leave a build conditional to fall back to a “smoke test” in case it
# breaks.
%bcond_without tests

Name:           python-asyncmy
Summary:        A fast asyncio MySQL/MariaDB driver
Version:        0.2.5
Release:        %autorelease

License:        Apache-2.0
URL:            https://github.com/long2ice/asyncmy
# The GitHub source includes tests and examples; the PyPI source lacks them.
Source0:        %{url}/archive/v%{version}/asyncmy-%{version}.tar.gz

# Doc/license files installed directly in site-packages
# https://github.com/long2ice/asyncmy/issues/33
Patch:          0001-Do-not-install-text-files-in-site-packages.patch

# Test failures and errors on 32-bit platforms
# https://github.com/long2ice/asyncmy/issues/34
# https://bugzilla.redhat.com/show_bug.cgi?id=2060899
ExcludeArch:    %{ix86} %{arm32}

BuildRequires:  gcc
BuildRequires:  python3-devel

%if %{with tests}
BuildRequires:  mariadb-server

# pyproject.toml: [tool.poetry.dev-dependencies]
# aiomysql = "*" ← benchmarks only
# bandit = "*" ← linter/analyzer
# black = "*" ← linter/formatter
# cryptography = "*"
BuildRequires:  python3dist(cryptography)
# cython = "*" ← already in [build-system] requires
# flake8 = "*" ← linter
# isort = "*" ← linter/formatter
# mypy = "*" ← linter/typechecker
# mysqlclient = "*" ← benchmarks only
# pymysql = "0.8.1" ← benchmarks only
# pytest = "*"
BuildRequires:  python3dist(pytest)
# pytest-asyncio = "*"
BuildRequires:  python3dist(pytest-asyncio)
# pytest-mock = "*"
BuildRequires:  python3dist(pytest-mock)
# pytest-xdist = "*"
BuildRequires:  python3dist(pytest-xdist)
# uvloop = { version = "*", markers = "…" } ← benchmarks only
%endif

%global common_description %{expand:
asyncmy is a fast asyncio MySQL/MariaDB driver, which reuses most of pymysql
and aiomysql but rewrites the core protocol with Cython to speed it up.}

%description %{common_description}


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

%description -n python3-asyncmy %{common_description}


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


%generate_buildrequires
%pyproject_buildrequires


%build
%pyproject_wheel


%install
%pyproject_install
%pyproject_save_files asyncmy

# Do not distribute Cython-generated C source files; these are not useful
find '%{buildroot}%{python3_sitearch}/asyncmy' \
    -type f -name '*.c' -print -delete
sed -r -i '/\.c$/d' '%{pyproject_files}'


%check
%if %{with tests}
# Based on rubygem-mysql2 packaging:

# Use a randomized port in case the standard mysqld port 3306 is occupied, and
# to account for multiple simultaneous builds on the same host.
# https://src.fedoraproject.org/rpms/rubygem-pg/pull-request/3
MYSQL_PORT="$((13306 + ${RANDOM} % 1000))"
MYSQL_USER="$(whoami)"
MYSQL_DATA_DIR="${PWD}/data"
MYSQL_SOCKET="${PWD}/mysql.sock"
MYSQL_LOG="${PWD}/mysql.log"
MYSQL_PID_FILE="${PWD}/mysql.pid"

mkdir "${MYSQL_DATA_DIR}"
mysql_install_db --datadir="${MYSQL_DATA_DIR}" --log-error="${MYSQL_LOG}"

%{_libexecdir}/mysqld --port="${MYSQL_PORT}" --skip-ssl \
    --datadir="${MYSQL_DATA_DIR}" --log-error="${MYSQL_LOG}" \
    --socket="${MYSQL_SOCKET}" --pid-file="${MYSQL_PID_FILE}" & :

echo "Waiting for server… ${i}" 1>&2
TIMEOUT=30
while ! grep -q 'ready for connections.' "${MYSQL_LOG}"
do
  sleep 1
  TIMEOUT=$((TIMEOUT - 1))
  if [[ "${TIMEOUT}" = '0' ]]
  then
    echo 'Timed out' 1>&2
    exit 1
  fi
done

echo 'Ready' 1>&2
trap "kill $(cat "${MYSQL_PID_FILE}")" INT TERM EXIT

# See https://github.com/brianmario/mysql2/blob/master/.travis_setup.sh
mysql -u "${MYSQL_USER}" -S "${MYSQL_SOCKET}" -P "${MYSQL_PORT}" \
  -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';"

sed -r -i "s/\b3306\b/${MYSQL_PORT}/" conftest.py
# Make sure we do not import the “un-built” package
rm -rf asyncmy

%pytest

%else
%pyproject_check_import
%endif


%files -n python3-asyncmy -f %{pyproject_files}
%license LICENSE
%doc CHANGELOG.md
%doc README.md


%changelog
* Fri Jan 20 2023 Fedora Release Engineering <releng@fedoraproject.org> 0.2.5-8
- Rebuilt for https://fedoraproject.org/wiki/Fedora_38_Mass_Rebuild

* Wed Nov 30 2022 Benjamin A. Beasley <code@musicinmybrain.net> 0.2.5-7
- Drop default argument -r from pyproject_buildrequires

* Wed Nov 30 2022 Benjamin A. Beasley <code@musicinmybrain.net> 0.2.5-6
- Fix MySQL/mariadb test server startup

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

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

* Tue Jun 14 2022 Python Maint <python-maint@redhat.com> 0.2.5-3
- Rebuilt for Python 3.11

* Wed Apr 20 2022 Benjamin A. Beasley <code@musicinmybrain.net> 0.2.5-2
- Stop numbering patches

* Wed Mar 30 2022 Benjamin A. Beasley <code@musicinmybrain.net> 0.2.5-1
- Update to 0.2.5 (close RHBZ#2069905)

* Mon Mar 28 2022 Benjamin A. Beasley <code@musicinmybrain.net> 0.2.4-1
- Update to 0.2.4 (close RHBZ#2068920)

* Fri Mar 04 2022 Benjamin A. Beasley <code@musicinmybrain.net> 0.2.3-2
- Add RHBZ link for ExcludeArch

* Wed Mar 02 2022 Benjamin A. Beasley <code@musicinmybrain.net> 0.2.3-1
- Initial package (close RHBZ#2041221)