#!/usr/bin/make -f
export DH_VERBOSE=1
export PYBUILD_NAME=keras

export KERAS_BACKEND=theano
export THEANO_FLAGS=base_compiledir='.pybuild',device=cpu
export PYTHONDONTWRITEBYTECODE=1

export LC_ALL=C.UTF-8
export LANG=C.UTF-8

export PYBUILD_TEST_PYTEST=1
export PYBUILD_BEFORE_TEST=cp -r {dir}/tests {build_dir}
export PYBUILD_AFTER_TEST=rm -rf {build_dir}/tests
export PYBUILD_TEST_ARGS_python2=-v $(IGNORETESTS)
export PYBUILD_TEST_ARGS_python3=-v $(IGNORETESTS)

# TensorFlow before 1.13 does not support Python3.7 so tests below will fail
PY3_TF_PACKAGE := $(shell dpkg-query -W -f='$${Package}\n' python3-tensorflow-* | head -n 1)
PY3_TF_VERSION := $(shell dpkg-query -W -f='$${Version}' $(PY3_TF_PACKAGE))
ifeq ($(shell dpkg --compare-versions $(PY3_TF_VERSION) lt 1.13~ && echo yes),yes)
  TESTSTOIGNORE_PY37=\
    tests/keras/utils/multi_gpu_test.py \
    tests/test_documentation.py \
    tests/keras/test_callbacks.py
  IGNORETESTS_PY37=$(TESTSTOIGNORE_PY37:%=--ignore=%)
  # Line below does not override test arguments for py3.7
  # py3 args are applied, not sure why...
  #export PYBUILD_TEST_ARGS_python3.7=-v $(IGNORETESTS) $(IGNORETESTS_PY37)
  export PYBUILD_TEST_ARGS_python3=-v $(IGNORETESTS) $(IGNORETESTS_PY37)
endif

# require tensorflow but not SKIPPED
#TESTSTOIGNORE=\
#  tests/keras/utils/multi_gpu_test.py \
#  tests/test_documentation.py \
#  tests/keras/test_callbacks.py

# internal failure, bad function calls
TESTSTOIGNORE+=\
  tests/integration_tests/test_image_data_tasks.py \
  tests/integration_tests/test_temporal_data_tasks.py \
  tests/keras/backend/backend_test.py

# need to access the internet
#TESTSTOIGNORE+=\
#  tests/integration_tests/imagenet_utils_test.py \
#  tests/integration_tests/test_datasets.py

# related to docs gen not found in build dir
TESTSTOIGNORE+=\
  tests/test_doc_auto_generation.py

IGNORETESTS=$(TESTSTOIGNORE:%=--ignore=%)

# According to a message here, this test is flaky
# https://github.com/keras-team/keras/pull/11658
# It is failing for me with Python 3.7
# Second one fails with different warning class instance
# Warning != UserWarning, only on Stretch newer works
IGNORETESTS += -k "not test_finite_generator_enqueuer_threads and not test_generator_enqueuer_fail_threads and not test_warnings"

# Get version from changelog as well as lsb_release
# to compute unique build version (useful for repo mirroring systems doing file pooling)
PKG_VERSION  := $(shell dpkg-parsechangelog | egrep '^Version:' | cut -f 2 -d ' ')
DIST_NAME    := $(shell lsb_release -si)
DIST_RELEASE := $(shell lsb_release -sc)
DIST_VERSION := $(shell lsb_release -sr)
PKG_VERSION_TAG     := $(PKG_VERSION)+$(DIST_NAME).$(DIST_RELEASE).$(DIST_VERSION)

%:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	dh $@ --with python3,python2 --buildsystem=pybuild
else
	dh $@ --with python3,python2 --buildsystem=pybuild
endif

override_dh_auto_build-indep:
	dh_auto_build
# In the PyPI distribution the file CONTRIBUTING.md is missing, which
# causes an error during doc build, so make it an empty file.
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	cd docs && sh -c "env PYTHONDONTWRITEBYTECODE=1 PYTHONPATH=.. python3 autogen.py && mkdocs build --site-dir=html"
else
	mkdir -p docs/html
	echo '<html><head><title>Keras</title></head></html>' > docs/html/index.html
endif

# Despite PYTHONDONTWRITEBYTECODE, a __pycache__ is nonetheless generated by mkdocs!
	find . -type d -name __pycache__ -exec rm -fv {} \;
# Distributed docs should not contain any programs
	find docs/html -name \*.py -exec rm -fv {} \;

override_dh_auto_test:
# Run tests using Theano backend and CPU device.
# Not every test is expected to pass, so we run a subset here:
	http_proxy="" https_proxy="" dh_auto_test

override_dh_installdocs:
	markdown README.md > .pybuild/README.html
	LANG=C.UTF-8 links -dump .pybuild/README.html > .pybuild/README
	dh_installdocs -A .pybuild/README

override_dh_compress:
	dh_compress -X.py

override_dh_gencontrol:
	dh_gencontrol -- -v"$(PKG_VERSION_TAG)"
