#!/usr/bin/make -f
include /usr/share/dpkg/pkg-info.mk

DEBIAN_DIR := $(CURDIR)/debian
export PYBUILD_NAME = distributed
export PYBUILD_TEST_CUSTOM = 1
export PYBUILD_TEST_ARGS = \
  cd {build_dir}; \
  $(DEBIAN_DIR)/run-tests \
    $(DEBIAN_DIR)/get-test-exclusions rules {interpreter} \
    -m pytest --pyargs distributed \
    --verbose \
    --color=no \
    --timeout-method=signal \
    --timeout=300 \
    -m "not avoid_ci"

# FIXME: several tests are tripping on the missing dask_expr raising
# FutureWarnings on dask.dataframe module import.  Setting configuration
# option "dataframe.query-planning" to False will not raise such
# warning, but the way forward would probably be to package dask_expr.
#
# Because the directory layout is different For autopkgtest-pkg-pybuild,
# we need to check the {build_dir} directory exists before trying to copy
# into it.
export PYBUILD_BEFORE_TEST = \
  mkdir -pv {home_dir}/.config \
  && printf '\173 "dataframe": \173 "query-planning": False \175 \175\n' \
     > {home_dir}/.config/dask

export NEEDED_JS=distributed/http/static/js/anime.min.js \
                 distributed/http/static/js/reconnecting-websocket.min.js

%:
	dh $@ --buildsystem=pybuild

UPSTREAM_VERSION = $(subst +ds,,$(DEB_VERSION_UPSTREAM))
execute_after_dh_testdir:
	# Guard against version mismatch in debian/patches/fix-version.patch.
	# This patch needs to be updated to newer upstream version every time.
	grep -q $(UPSTREAM_VERSION) distributed/_version.py

# This cannot be moved to debian/python3-distributed.pyinstall,
# as that happens after the tests have been run.
export PYBUILD_AFTER_BUILD = \
  cp -v distributed/tests/testegg-*-py*.egg distributed/tests/mytest.pyz \
    {build_dir}/distributed/tests

override_dh_auto_build: export http_proxy=127.0.0.1:9
override_dh_auto_build: export https_proxy=127.0.0.1:9
override_dh_auto_build:
	rm -f distributed/comm/tests/__init__.py
	set -e; \
	for p in ${NEEDED_JS}; do \
	    uglifyjs -o $$p debian/missing-sources/$${p%.min.js}.js ; \
	done
	cp /usr/share/javascript/sortable-tablesort/sortable.min.js distributed/http/static/js
	cp /usr/share/javascript/sortable-tablesort/sortable.min.css* distributed/http/static/css
	chmod a-x distributed/tests/test_utils_test.py
	dh_auto_build
	chmod a+x distributed/tests/test_utils_test.py
#	ln -s /usr/share/javascript/mathjax docs/source/_static
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	PYTHONPATH=. python3 -m sphinx -N -bhtml docs/source build/html # HTML generator
	rm -rf build/html/_static/__pycache__
endif

# This is needed for newer versions of OpenSSL
dh_auto_test: export CRYPTOGRAPHY_OPENSSL_NO_LEGACY=1

override_dh_compress:
	dh_compress -Xchangelog.html

override_dh_install:
	dh_install
	dh_installchangelogs docs/source/changelog.rst

override_dh_auto_install:
	dh_auto_install
	dh_link
	find debian/ -name *.ico -exec chmod a-x {} \;
	rm -rf debian/python3-distributed/usr/lib/python3*/dist-packages/dask-worker-space debian/python3-distributed/usr/lib/python3*/dist-packages/core debian/python3-distributed/usr/lib/python3*/dist-packages/test_cluster_dump

override_dh_installdocs:
	dh_installdocs -ppython-distributed-doc --doc-main-package=python3-distributed
	dh_installdocs --remaining-packages
	rm -vf debian/python-distributed-doc/usr/share/doc/python3-distributed/html/_static/vendor/fontawesome/*/LICENSE.txt

override_dh_auto_clean:
	touch distributed/comm/tests/__init__.py
	dh_auto_clean
	-rm $(NEEDED_JS)
