# Maintainer: Filipe Laíns (FFY00) <lains@archlinux.org>
# Contributor: Andrea Scarpino <andrea@archlinux.org>
# Contributor: examon <examon.mail[at]gmail[dot]com>
# Contributor: Sebastian Wiesner <lunaryorn googlemail com>
# Contributor: Dwight Schauer <dschauer@ti.com>

_pkgname=pyudev
pkgname=python-$_pkgname
pkgver=0.24.0
pkgrel=1
arch=('any')
url='https://github.com/pyudev/pyudev'
license=('LGPL')
pkgdesc='Python bindings to libudev'
depends=('python-six' 'systemd')
makedepends=('python-build' 'python-installer' 'python-setuptools' 'python-wheel' 'python-sphinx' 'python-mock')
checkdepends=('python-pytest' 'python-docutils' 'python-mock' 'python-hypothesis' 'python-pip')
optdepends=('python-pyqt5: PyQt integration'
            'pyside2: PySide integration'
            'python-wxpython: WX integration')
source=("$_pkgname-$pkgver.tar.gz::$url/archive/v$pkgver.tar.gz")
sha512sums=('fe6fec9bbc402bbcf6458b6c3ea2db43ac3ac03bba3c74193dde46253b532d1c9d55ad78e4bc8fa0ff660bf0e371f07ef60c6c4a6251b0dd64acbb129f63507e')

prepare() {
  cd $_pkgname-$pkgver

  # Remove failing tests (we can't test udev inside makepkg)
  rm tests/test_{util,discover,device,monitor,enumerate,observer}.py

  # Fix documentation build
  sed -i "s|os.path.join(doc_directory, os.pardir)|os.path.join(doc_directory, os.pardir, 'src')|
          s|b'autodoc-process-docstring'|'autodoc-process-docstring'|" doc/conf.py
}

build() {
  cd $_pkgname-$pkgver

  python -m build -nw

  # Generate documentation
  sphinx-apidoc -f -e -o doc src/pyudev
  sphinx-build -a -b html doc doc/html
}

check() {
  cd $_pkgname-$pkgver

  PYTHONPATH=src python -m pytest
}

package() {
  cd $_pkgname-$pkgver

  python -m installer -d "$pkgdir" dist/*.whl

  # Install documentation
  install -dm 755 "$pkgdir"/usr/share/doc/$pkgname
  cp -r -a --no-preserve=ownership doc/html "$pkgdir"/usr/share/doc/$pkgname
  rm -rf "$pkgdir"/usr/share/doc/$pkgname/html/.doctrees
}
