#!/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

# 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=%)

%:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	dh $@ --with mkdocs,python3 --buildsystem=pybuild
else
	dh $@ --with python3 --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:
	PYBUILD_SYSTEM=custom PYBUILD_TEST_ARGS="cd {build_dir} && py.test-3 -oaddopts=-v $(IGNORETESTS)" 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
