# Maintainer: Some One <some.one@some.email.com>

_realname=oslo-db
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=12.3.3
pkgrel=1
pkgdesc="OpenStack Database Pattern Library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://docs.openstack.org/oslo.db/latest/'
msys2_repository_url='https://opendev.org/openstack/oslo.db'
msys2_changelog_url='https://docs.openstack.org/releasenotes/oslo.db/'
msys2_documentation_url='https://docs.openstack.org/oslo.db/latest/'
msys2_issue_tracker_url='https://bugs.launchpad.net/oslo.db'
msys2_references=(
  'archlinux: python-oslo-db'
  'purl: pkg:pypi/oslo-db'
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-python-pbr"
         "${MINGW_PACKAGE_PREFIX}-python-alembic"
         "${MINGW_PACKAGE_PREFIX}-python-debtcollector"
         "${MINGW_PACKAGE_PREFIX}-python-oslo-i18n"
         "${MINGW_PACKAGE_PREFIX}-python-oslo-config"
         "${MINGW_PACKAGE_PREFIX}-python-oslo-utils"
         "${MINGW_PACKAGE_PREFIX}-python-sqlalchemy"
         "${MINGW_PACKAGE_PREFIX}-python-stevedore"
         "${MINGW_PACKAGE_PREFIX}-python-testresources"
         "${MINGW_PACKAGE_PREFIX}-python-testscenarios")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-mock"
              "${MINGW_PACKAGE_PREFIX}-python-testrepository"
              "${MINGW_PACKAGE_PREFIX}-python-testresources"
              "${MINGW_PACKAGE_PREFIX}-python-oslotest"
              "${MINGW_PACKAGE_PREFIX}-python-oslo-context")
source=("https://github.com/openstack/oslo.db/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "https://github.com/openstack/oslo.db/commit/94d6e24ca19b0116eed00d5ccdb8a538918c6dcf.patch")
sha256sums=('bd481e9cb6ec7835ec703d337f35b7f8db80111f6997c07e17b6ae5110b9d59e'
            '4e2f53a3905f90b1912d0c1daad8e3b2076c0a5056a35f36b976c409c0459ff1')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}/oslo.db-${pkgver}"
  sed -i 's/import unittest2/import unittest as unittest2/' oslo_db/tests/sqlalchemy/test_async_eventlet.py

  apply_patch_with_msg \
    94d6e24ca19b0116eed00d5ccdb8a538918c6dcf.patch

  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver
  # set version for PBR
  export PBR_VERSION=$pkgver
}

build() {
  cp -r "${_realname//-/.}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

check() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  PYTHON=${MINGW_PREFIX}/bin/python ${MINGW_PREFIX}/bin/stestr run || warning "Tests failed"
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
