#!/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 LC_ALL=C.UTF-8 LANGUAGE=C.UTF-8 date -u -d "@$(SOURCE_DATE_EPOCH)" "+%a, %d %b %Y" || echo "xxx, xx xxx xxxx")
SOURCE_TIME:=$(shell LC_ALL=C.UTF-8 LANGUAGE=C.UTF-8 date -u -d "@$(SOURCE_DATE_EPOCH)" "+%T" || echo "xx:xx:xx")

UVER := $(shell echo $(DEB_VERSION_UPSTREAM) | sed -e 's,+dfsg[0-9]*,,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
ifeq (,$(findstring mips,$(DEB_HOST_ARCH))$(findstring hppa,$(DEB_HOST_ARCH))$(findstring _ia64_,_$(DEB_HOST_ARCH)_))
	PYTEST_WARN_IGNORE :=
else
	# mips/hppa may have unusual NaN behaviour
	# https://en.wikipedia.org/wiki/NaN#Encoding
	# ia64 warns in test_groupby_quantile_all_na_group_masked (see 2.0.3+dfsg-6 build log)
	PYTEST_WARN_IGNORE := -W "ignore:invalid value encountered:RuntimeWarning"
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, test_wrong_url needs internet, test_statsmodels is a circular dependency when adding a new Python version
# The echo-random-characters is to allow searching for fails that don't show the usual test failure message i.e. crashes/errors
# -v makes the log too long for salsa-ci
# the grep -q is because trying to test __pycache__ is an error in pytest 8+, #1063959
export PYBUILD_TEST_ARGS=TEST_SUCCESS=true; cd {build_dir} ; PANDAS_RUN_CRASHING_TESTS=1 PANDAS_CI=1 LC_ALL=C.UTF-8 PYTEST_QT_API=pyqt5 xvfb-run -a -s "-screen 0 1280x1024x24 -noreset" {interpreter} -m pytest -s -c {dir}/pyproject.toml --deb-data-root-dir={dir}/pandas/tests --forked {build_dir}/pandas/tests/io/pytables || true ; PANDAS_CI=1 LC_ALL=C.UTF-8 PYTEST_QT_API=pyqt5 xvfb-run -a -s "-screen 0 1280x1024x24 -noreset" {interpreter} -m pytest -s --runxfail -c {dir}/pyproject.toml --deb-data-root-dir={dir}/pandas/tests {build_dir}/pandas/tests/io/test_stata.py {build_dir}/pandas/tests/window/test_numba.py {build_dir}/pandas/tests/api/test_api.py {build_dir}/pandas/tests/test_downstream.py {build_dir}/pandas/tests/groupby/test_groupby.py {build_dir}/pandas/tests/io/test_pickle.py || true ; echo "start of non-ignored tests" ; for TEST_SUBSET in {build_dir}/pandas/tests/* ; do echo $$TEST_SUBSET | grep -q -e __pycache__ || PANDAS_CI=1 LC_ALL=C.UTF-8 PYTEST_QT_API=pyqt5 xvfb-run -a -s "-screen 0 1280x1024x24 -noreset" {interpreter} -m pytest -s -m "$(PYTEST_MARKER)" $(PYTEST_WARN_IGNORE) -k "not test_register_entrypoint and not test_wrong_url and not test_statsmodels" -c {dir}/pyproject.toml --deb-data-root-dir={dir}/pandas/tests $$TEST_SUBSET || test $$? = 5 || TEST_SUCCESS=false && echo "rdjoqkol test state = $$TEST_SUCCESS"; done ; rm -rf test-data.xml test_stata.dta .pytest_cache pandas/.pytest_cache .hypothesis {dir}/.pytest_cache {dir}/pandas/.pytest_cache {dir}/.hypothesis ; $$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

execute_before_dh_auto_clean:
	find pandas/ -regex '.*\.c\(\|pp\)' | xargs grep -l -e 'Generated by Cython'  | xargs -r rm -f
	# the || true is because this will fail without docutils (-arch or nodoc builds)
	cd doc && LC_ALL=C.UTF-8 python3 make.py clean || true
	rm -rf build buildtmp *-stamp doc/source/savefig doc/source/user_guide/styled.xlsx doc/source/index.rst _version_meson.py

execute_before_dh_auto_build:
	mkdir -p buildtmp
	echo -e "__version__ = '$(UVER_PY)'\n__git_version__ = 'not-from-upstream-git'" > _version_meson.py

execute_after_dh_auto_build-indep:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	: # Build Documentation
	cd doc && PYTHONPATH=$(CURDIR)/.pybuild/cpython3_$(PY3VER)/build LC_ALL=C.UTF-8 LANGUAGE=C.UTF-8 python3 make.py html
	# strip build paths, object IDs 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# at 0x[0-9a-f]\{8,16\}\(&gt;\|>\)# at 0xadde5de1e8ed\1#g' \
	    -e 's#tmp/ipykernel_[0-9]\+#tmp/ipykernel_nnnnnnn#g' \
	    -e 's#^\s\+.\(iopub.execute_input\|iopub.status.busy\|iopub.status.idle\|shell.execute_reply\).:.*# #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

# to reduce package size, don't ship the test data
# the autopkgtest uses --deb-data-root-dir (find_test_data.patch) to get it from the source package
execute_after_dh_auto_install:
	rm -rf debian/python3-pandas/usr/lib/python*/dist-packages/pandas/tests/io/data debian/python3-pandas/usr/lib/python*/dist-packages/pandas/tests/io/*/data

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
