# Maintainer: Antonio Rojas <arojas@archlinux.org>
# Contributor: Aloxaf <aloxafx@gmail.com>
# Contributor: Alex Hirzel <alex@hirzel.us>

_name=playwright-python
pkgname=python-playwright
pkgver=1.57.0
pkgrel=2
pkgdesc='A Python library to automate Chromium, Firefox and WebKit browsers with a single API'
arch=(any)
url='https://github.com/microsoft/playwright-python'
license=(Apache-2.0)
depends=(nodejs
         python
         python-greenlet
         python-pyee
         sh)
makedepends=(git
             python-auditwheel
             python-build
             python-installer
             python-setuptools-scm
             python-wheel
             zip)
checkdepends=(python-autobahn
              python-flaky
              python-objgraph
              python-pixelmatch
              python-pyopenssl
              python-pytest chromium
              python-pytest-playwright
              python-requests)
source=(git+https://github.com/microsoft/$_name#tag=v$pkgver
        https://registry.npmjs.org/playwright-core/-/playwright-core-$pkgver.tgz
        auditwheel-6.3.patch)
noextract=(playwright-$pkgver-linux.zip)
sha256sums=('ffe6e919f5d14428dd6bf79fe93964cc9f0971d83770088fcfa28dbd70632898'
            '8d2942f575e1b772bae8bd2c4d71f9055bf6581bc79d7001b2b277335b89ef2a'
            '8af7d2f2a41582212fc20665ec5a38e569b79195d85ec35e25f4ec421cb5db32')

prepare() {
  zip -qr playwright-$pkgver-linux.zip package
  if [ "$CARCH" = "aarch64" ]; then
    zip -qr playwright-$pkgver-linux-arm64.zip package
  fi

  cd $_name
  sed -e 's|==.*\"|"|' -i pyproject.toml # Drop dependency version constraints
  sed -e "s|driver_version = \".*\"|driver_version = \"$pkgver\"|" -i setup.py
  install -Dm644 "$srcdir"/playwright-$pkgver-linux.zip -t driver
  if [ "$CARCH" = "aarch64" ]; then
    install -Dm644 "$srcdir"/playwright-$pkgver-linux-arm64.zip -t driver
  fi
  patch -p1 -i ../auditwheel-6.3.patch
}

build() {
  cd $_name
  python -m build --wheel --no-isolation
}

# Testing is utterly broken
#check() {
#  cd $_name
#  PYTHONPATH=$PWD \
#  pytest -v
#}

package() {
  cd $_name
  python -m installer --destdir="$pkgdir" dist/*.whl

# Replace bundled node with system one
  ln -s /usr/bin/node -t "$pkgdir"/usr/lib/python*/site-packages/playwright/driver/
}
