# Maintainer:
# Contributor: @RubenKelevra <cyrond@gmail.com>

pkgname=python-watchfiles
_name=${pkgname#python-}
pkgver=0.24.0
pkgrel=2
pkgdesc='Simple, modern and high performance file watching and code reload in python'
arch=(x86_64 aarch64)
url='https://github.com/samuelcolvin/watchfiles'
license=(MIT)
depends=(
  gcc-libs
  glibc
  python
  python-anyio
)
makedepends=(
  python-build
  python-installer
  python-maturin
)
checkdepends=(
  python-dirty-equals
  python-pytest
  python-pytest-mock
  python-pytest-timeout
)
source=($url/archive/v$pkgver/$pkgname-$pkgver.tar.gz)
b2sums=('96d8707327d41e3a9410c291c557641e111d91ca90ec945fc05d4ec2ebab9290a65044024a324bbb1e92f99900e9bd7098933061ddc95369c33226c8aed7924b')

prepare() {
  # remove __init__.py otherwise this prevents tests detecting the watchfiles module anywhere else
  rm -v $_name-$pkgver/tests/__init__.py
}

build() {
  cd $_name-$pkgver

  # Set version following upstream CI workflow
  # https://github.com/samuelcolvin/watchfiles/blob/v0.23.0/.github/workflows/ci.yml#L202
  VERSION=$pkgver python .github/set_version.py

  python -m build --wheel --no-isolation
}

check() {
  local pytest_options=(
    -vv
    -o addopts=''
    --deselect tests/test_watch.py::test_awatch_interrupt_raise
  )
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")

  cd $_name-$pkgver
  # install to temporary location, as importlib is used
  python -m installer --destdir=test_dir dist/*.whl
  export PYTHONPATH="$PWD/test_dir/$site_packages:$PYTHONPATH"
  pytest "${pytest_options[@]}" .
}

package() {
  cd $_name-$pkgver
  python -m installer --destdir="$pkgdir" dist/*.whl
  install -Dm644 LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname
}
