#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/pkg-info.mk

PY3VERS := $(shell py3versions -vr)
PY3VER := $(shell py3versions -vd)
SOURCE_DATE:=$(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "+%a, %d %b %Y" || echo "xxx, xx xxx xxxx")
SOURCE_TIME:=$(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "+%T" || echo "xx:xx:xx")

UVER := $(shell echo $(DEB_VERSION_UPSTREAM) | sed -e 's,+dfsg,,g')
# Python doesn't use ~ for rc
UVER_PY := $(shell echo $(UVER) | sed -e 's,[~],,g')
UVER_PYSHORT := $(shell echo $(UVER_PY) | sed -e 's,+git.*,,g')

# Filter out tests with "marker expressions" and "keyword expressions". Ref: pytest(1)
ifeq ($(DEB_HOST_ARCH),$(filter $(DEB_HOST_ARCH), amd64 i386 kfreebsd-amd64 kfreebsd-i386 x32))
	PYTEST_MARKER := not network
else
	PYTEST_MARKER := not network and not slow
endif
# for matplotlib etc
export HOME=$(CURDIR)/buildtmp

# Split up the test suite to avoid running out of memory, use xvfb for plot tests, run but ignore potentially crashing tests; test_register_entrypoint expects an installed package
export PYBUILD_TEST_ARGS=TEST_SUCCESS=true; cd {build_dir} ; for TEST_SUBSET in {build_dir}/pandas/tests/* ; do LOCALE_OVERRIDE=C xvfb-run -a -s "-screen 0 1280x1024x24 -noreset" {interpreter} -m pytest -s -v -m "$(PYTEST_MARKER)" -k "not test_register_entrypoint" --confcutdir={build_dir}/pandas --deb-data-root-dir={dir}/pandas/tests --strict-data-files $$TEST_SUBSET || test $$? = 5 || TEST_SUCCESS=false ; done ; LOCALE_OVERRIDE=C {interpreter} -m pytest -s -v -m "$(PYTEST_MARKER)" --forked --runxfail --confcutdir={build_dir}/pandas --deb-data-root-dir={dir}/pandas/tests --strict-data-files {build_dir}/pandas/tests/groupby/transform/test_numba.py {build_dir}/pandas/tests/window/test_numba.py {build_dir}/pandas/tests/window/test_apply.py || true ; $$TEST_SUCCESS

export PYBUILD_EXT_DESTDIR=debian/python3-pandas-lib
export PYBUILD_DESTDIR=debian/python3-pandas

# try to prevent unsanctioned downloads
export  http_proxy=http://127.0.0.1:9/
export  https_proxy=http://127.0.0.1:9/

export SHELL=/bin/bash

# Mega rule
%:
	: # Explicit build system to avoid use of all-in-1 Makefile
	dh $@ --buildsystem=pybuild --with python3,numpy3,sphinxdoc

# The *cython* rules are provided to allow using pre-built Cython files in distributions without a new enough Cython; they are not (and must not be) used in official Debian builds (including -backports).  Use instructions:
# on a system with new enough Cython, run debian/rules cythonize - this will add a directory under debian/
# uncomment the _uncythonize rule dependency below
# remove the cython3 Build-Depends from d/control
# copy to the older system and build as normal
# To upgrade to a new upstream version, this process must be repeated
# Warning - has not been tested for some time
_cythonize%: override_dh_clean # force removal of previous copies
	python$(*:2=) setup.py cython
	D=debian/cythonized-files$(*:2=) && \
		git rm -rf $$D || rm -rf $$D; \
		find pandas/ -regex '.*\.c\(\|pp\)' | while read f; do \
			grep -q 'Generated by Cython' "$$f" || continue; \
			mkdir -p "$$D/$$(dirname $$f)"; \
			cp "$$f" "$$D/$$(dirname $$f)"; \
			git add -f "$$D/$$f" || true; \
		done; \
		echo "$(UVER)" >| $$D/VERSION; git add $$D/VERSION || true

_uncythonize%:
	: # Make sure that cythonized sources are up-to-date
	[ "$(UVER)" = "`cat debian/cythonized-files3/VERSION`" ]
	echo "$*" | grep -q '^3' && PY=3 || PY= ; \
		echo "I: Using pre-Cython-ed files for Python $*"; \
		cd debian/cythonized-files$$PY/ ; \
		find . -regex '.*\.c\(\|pp\)' | while read f; do cp $$f ../../$$f; done

cythonize: _cythonize3

override_dh_clean:
	find pandas/ -regex '.*\.c\(\|pp\)' | xargs grep -l -e 'Generated by Cython'  | xargs -r rm -f
	rm -rf build buildtmp doc/build *-stamp # pandas.egg-info pandas/datasets/__config__.py
	dh_clean

override_dh_auto_build-arch: # ${PY3VERS:%=_uncythonize%}
	mkdir -p buildtmp
	[ -e pandas/__version.py ] || \
	echo -e "version = '$(UVER_PY)'\nshort_version = '$(UVER_PYSHORT)'" > pandas/__version.py
	dh_auto_build

override_dh_auto_build-indep: override_dh_auto_build-arch
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	: # Build Documentation
	cd doc && PYTHONPATH=$(CURDIR)/.pybuild/cpython3_$(PY3VER)/build LC_ALL=C python3 make.py html
	# strip build paths and statsmodels timestamps for reproducibility, and online Javascript for privacy
	for html in `find doc/build/html -name _modules -prune -o -name "*.html" -o -name "*.ipynb"` ; do \
	    sed -i -e 's#$(CURDIR)/.pybuild/[^/]*/build/pandas/#/usr/lib/python3/dist-packages/pandas/#g' \
	    -e 's#$(CURDIR)/debian/python3-pandas/usr/lib/python3/dist-packages/pandas/#/usr/lib/python3/dist-packages/pandas/#g' \
	    -e 's#<script crossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" type="text/javascript" src="https://cdnjs\.cloudflare\.com/ajax/libs/require\.js/2\.3\.4/require\.min\.js">#<script type="text/javascript" src="file:///usr/share/javascript/requirejs/require.min.js">#g' \
	    -e 's#\(Date:.*\)[A-Z][a-z]\+, \+[0-9]\+,\? \+[A-Z][a-z]\+,\? \+[0-9]\+#\1$(SOURCE_DATE)#g' \
	    -e 's#\(Time:.*\)[0-9][0-9]:[0-9][0-9]:[0-9][0-9]#\1$(SOURCE_TIME)#g' $${html} ; \
	done
endif

override_dh_installdocs:
	dh_installdocs -A *.md
	# for NEWS.html.gz - put it here and use symlinks to avoid breaking internal links
	gzip -c -n -9 debian/python-pandas-doc/usr/share/doc/python-pandas-doc/html/whatsnew/index.html > debian/python-pandas-doc/usr/share/doc/python-pandas-doc/html/whatsnew/index.html.gz || true
	cp -av debian/contributors_list.txt debian/python-pandas-doc/usr/share/doc/python-pandas-doc || true
	# deduplicate files - the ||true is because we only build-depend on jdupes if we're building documentation
	jdupes -r -l debian/python-pandas-doc/usr/share/doc || true


## immediately useable documentation and exemplar scripts/data
override_dh_compress:
	dh_compress -X.py -X.html -X.pdf -X.css -X.jpg -X.txt -X.js -X.json -X.rtc -Xobjects.inv

# see PYBUILD_TEST_ARGS above
override_dh_auto_test:
	PYBUILD_SYSTEM=custom dh_auto_test