##################################################
# The "properties" of the build environment (__*)
# (i.e., what we are using for compilation):
%__python3 /usr/bin/python3
%__python3_version %(LC_ALL=C %__python3 -c 'import sys; print("{0}.{1}".format(sys.version_info[0],sys.version_info[1]))' 2>/dev/null || echo unknown)
%__python3_abiflags %(%__python3-config --abiflags 2>/dev/null || echo unknown)

##################################################
# The "properties" of the target system (_*)
# (for which we are building).
# Normally, the target properties must be set according to
# the "properties" of the build environment:
# otherwise, there will be a mismatch between the real built package contents
# and the wished target system. An exception is building python3 package
# itself: then we should override the target "properties", because
# the present python3 in the build environment doesn't matter.

%_python3_abiflags %__python3_abiflags
%_python3_extension_suffix %(%__python3-config --extension-suffix 2>/dev/null || echo .so)
# According to our new ALT Sisyphus policy,
# there is only a single version of python3.
# So, %%_python3_version should not matter.
# TODO: redefine it to constant 3.
# Before that, we need to know where and how it is used in packages.
# It seems it is only used to export RPM_PYTHON3_VERSION
# (which only is used to determine the fallback value for RPM_PYTHON3_COMPILE_INCLUDE
# and might be used in some external packages...
# As for RPM_PYTHON3_COMPILE_INCLUDE, having the path with minor version there
# must matter only for the build of python3 itself,
# and can be modified just in its spec.
# No need to handle it in the general case here.)
%_python3_version %__python3_version
# A macro which will be used to guarantee the match between the ABIs
# (make sure to redefine it when building a new python3 package in its spec)
%_python3_abi_version %__python3_version

# deprecated pre-PEP517 or setup.py-based build and install macros
%python3_deprecated_macro() \
	%{warn:WARNING: %1 is deprecated and will be removed in future, please use %2 instead\
}
# traditional build/install macros
%python3_setup() \
	%global _buildrequires_build %_buildrequires_build %python3_setup_buildrequires \
	CFLAGS="${CFLAGS:-%optflags}" ; export CFLAGS ; \
	CXXFLAGS="${CXXFLAGS:-%optflags}" ; export CXXFLAGS ; \
	FFLAGS="${FFLAGS:-%optflags}" ; export FFLAGS ; \
	%__python3 setup.py

%python3_build \
	%{python3_deprecated_macro %%python3_build %%pyproject_build}%{python3_setup:} build
%python3_build_debug \
	%{python3_deprecated_macro %%python3_build_debug %%pyproject_build}%{python3_setup:} build --debug
%python3_install \
	%{python3_deprecated_macro %%python3_install %%pyproject_install}%{python3_setup:} install --skip-build --root=%buildroot --force
%python3_build_install \
	%{python3_deprecated_macro %%python3_build_install %%pyproject_build}%{python3_setup:} install --root=%buildroot --force
# Let's assume that in most cases setuptools are used or make no harm
# if those traditional Python3 build/install macros are used (overridable):
%python3_setup_buildrequires python3-module-setuptools


# Normalized names
# https://peps.python.org/pep-0503/#normalized-names
%pep503_name() %(%__python3 -c 'import re; print(re.sub(r"[-_.]+", "-", "%1").lower())' 2>/dev/null || echo unknown)

# https://packaging.python.org/en/latest/specifications/binary-distribution-format/#escaping-and-unicode
%pep427_name() %(%__python3 -c 'import re; print(re.sub(r"[-_.]+", "_", "%1").lower())' 2>/dev/null || echo unknown)


# PEP517/518 build and install macros
# actual builder and installer
%pyproject_buildrequires python3-module-pyproject-installer >= 0.4.0

%pyproject() \
	%global _buildrequires_build %_buildrequires_build %pyproject_buildrequires \
	%__python3 -m pyproject_installer

%pyproject_build \
	export CFLAGS="${CFLAGS:-%optflags}" ; \
	export CXXFLAGS="${CXXFLAGS:-%optflags}" ; \
	export FFLAGS="${FFLAGS:-%optflags}" ; \
	%{pyproject:} -v build

%pyproject_install %{pyproject:} -v install --destdir=%buildroot

%pyproject_distinfo() %{pep427_name: %1}-%version.dist-info/

%pyproject_run \
	export CFLAGS="${CFLAGS:-%optflags}" ; \
	export CXXFLAGS="${CXXFLAGS:-%optflags}" ; \
	export FFLAGS="${FFLAGS:-%optflags}" ; \
	export NO_INTERNET=YES ; \
	%{pyproject:} run

%pyproject_run_pytest \
	%global _buildrequires_build %_buildrequires_build %pytest_buildrequires \
	%{pyproject_run:} -- python3 -m pytest

%pyproject_run_unittest \
	%{pyproject_run:} -- python3 -m unittest

%pytest_buildrequires %{!?_disable_check:%{!?_without_check:%{!?_disable_test:%{!?_without_test:python3-module-pytest}}}}
# tox check macros
# tox and its required plugins for running in a network-isolated environment
%tox_buildrequires %{!?_disable_check:%{!?_without_check:%{!?_disable_test:%{!?_without_test:python3-module-tox python3-module-tox-no-deps python3-module-tox-console-scripts python3-module-tox-passenv}}}}

%_tox_bin %_bindir/tox.py3

# most commonly used tox config if upstream doesn't provide any or if we
# want to override it.
# Override note: today's tox searches its config in the following order:
# pyproject.toml, tox.ini, setup.cfg.
%tox_create_default_config() \
cat > tox.ini <<EOF\
[testenv]\
commands =\
    pytest {posargs:-vra}\
EOF

# default value for TOX_TESTENV_PASSENV
%_tox_default_tox_testenv_passenv NO_INTERNET

# - NO_INTERNET
#   Common used in ALTLinux variable a test runner can rely on to skip tests
#   requiring Internet.
#
# - PIP_NO_BUILD_ISOLATION
#   Disable isolation when building a modern source distribution. Build
#   dependencies specified by PEP 518 must be already installed if this option
#   is used.
#
# - PIP_NO_INDEX
#   Ignore package index (only looking at --find-links URLs instead).
#
# - TOXENV
#   Name or names (comma separated) of tox env used for running tests
#
# - VIRTUALENV_SYSTEM_SITE_PACKAGES
#   Create virtual environments that also have access to globally installed
#   packages. The default value is overwritten by the
#   VIRTUALENV_SYSTEM_SITE_PACKAGES environment variable.
#
# - TOX_TESTENV_PASSENV
#   Define additional space-separated variable names that are to be passed down
#   to the test command environment.
#
#   For example,
#   pass FOO and BAR additionally to ALT defaults
#   TOX_TESTENV_PASSENV="%{?_tox_default_tox_testenv_passenv:%_tox_default_tox_testenv_passenv }FOO BAR"
%tox() \
	%global _buildrequires_build %_buildrequires_build %tox_buildrequires \
	export NO_INTERNET=YES ; \
	export PIP_NO_BUILD_ISOLATION=NO ; \
	export PIP_NO_INDEX=YES ; \
	export TOXENV="${TOXENV-py3}" ; \
	export TOX_TESTENV_PASSENV="${TOX_TESTENV_PASSENV-%_tox_default_tox_testenv_passenv}" ; \
	export VIRTUALENV_SYSTEM_SITE_PACKAGES=YES ; \
	%_tox_bin

%tox_check \
	%{tox:} --sitepackages -vr -s false --no-deps --console-scripts

# relies on wheeltracker (created by builder as cwd/dist/.wheeltracker)
%tox_check_pyproject \
	_tox_wheel=$(cat ./dist/.wheeltracker) || \
		{ echo Make sure you built a pyproject ; exit 1 ; } ; \
	%{tox_check:} --installpkg="./dist/$_tox_wheel"


# When building the python3 distribution itself, set this to its specific minor-versioned location:
#_python3_path %%python3_libdir %%python3_libdir_noarch
# (or perhaps simply unset it).
# Otherwise, we care just about python3/site-packages/ (without the minor version;
# anything outside site-packages/ is disallowed anyway):
%_python3_path %python3_sitelibdir %python3_sitelibdir_noarch
%add_python3_path()	%global _python3_path %_python3_path %* \
			%global _python_compile_exclude %_python_compile_exclude %*

# Additional paths where our deps are allowed to be located:
%_python3_import_path %nil
%allow_python3_import_path()	%global _python3_import_path %_python3_import_path %*

%_python3_self_prov_path %nil
%add_python3_self_prov_path()        %global _python3_self_prov_path %{?_python3_self_prov_path:%_python3_self_prov_path:}%1

%_python3_req_hier yes
#
#	Включить поиск иерархических зависимостей
#
%python3_req_hier %global _python3_req_hier yes
#
#	Выключить поиск иерархических зависимостей
#
%python3_req_nohier %undefine _python3_req_hier

%_target_python3_libdir %_target_libdir
%_python3_set_noarch() %global _target_python3_libdir %_target_libdir_noarch
%_python3_set_arch() %global _target_python3_libdir %_target_libdir

%_python3_lib_path	""
%_python3_req_method	slight
%_python3_req_skip	""
%_python3_compile_exclude	%_defaultdocdir
%_python3_compile_include	%_python3_path
%_python3_compile_deep	20
%_python3_compile_skip_x	1
%_python3_compile_clean	1

%set_python3_req_method()	%global _python3_req_method %1

%add_python3_req_skip()	%global _python3_req_skip %_python3_req_skip %*
%add_python3_lib_path()	%{expand:%%add_python3_path %*} \
                        %global _python3_lib_path %_python3_lib_path %*
%add_python3_compile_exclude() %global _python3_compile_exclude %_python3_compile_exclude %*
%add_python3_compile_include() %global _python3_compile_include %_python3_compile_include %*
# TODO: consider declaring them for python (non 3) compile_exclude
# (as is done in %%add_python3_path), too:
#			       %global _python_compile_exclude %_python_compile_exclude %*
#
# The problem is: can people have a single path with mixed Python2/3 stuff?
# This is not crucial for python.req/prov because it seems to have a smart filter.

# Заголовочные файлы для компиляции расширений на C
%__python3_includedir %_includedir/python%__python3_version%__python3_abiflags

# Модули Python3, включенные в официальный релиз
%__python3_libdir %_target_libdir/python%__python3_version
%__python3_dynlibdir %python3_libdir/lib-dynload
%__python3_tooldir %python3_libdir/Tools
# The _noarch location is not used as of now in the python3-3.3.1-alt4 distribution
# (for targets when it is different from %%python3_libdir); we define it for adaptability
# to the future (and for internal use by brp-fix_python3_site-packages_location):
%__python3_libdir_noarch %_target_libdir_noarch/python%__python3_version

# Compatibility macros (which might have been used in other packages).
# TODO: check and switch other packages to %__* build system macros
# (for overall clarity of the system of macros).
%python3_libdir %__python3_libdir
%python3_dynlibdir %__python3_dynlibdir

# Модули Python3, установленные в дополнение к официальному релизу
%python3_sitelibdir %_target_python3_libdir/python3/site-packages

# архитектурно-независимые модули Python3, установленные в дополнение к официальному релизу
%python3_sitelibdir_noarch %_target_libdir_noarch/python3/site-packages

%__python3_deps_internal() \
for mod in %{*}; do \
    echo -n "python3%{?_python3_standalone}($mod)${rpm_python3_deps_constraint:+ $rpm_python3_deps_constraint} "; \
done; unset mod;\
%nil

%__python3_req_constraint %(%__python3 %_rpmlibdir/python3.req.constraint.py %buildroot %_python3_import_path)

# py3_requires <имя_модуля1> [<имя_модуля2> ...]
#
#	Добавляет зависимость на указанные модули Python3. Формируемые
# 	зависимости имеют вид: python3(имя_модуля1) [python3(имя_модуля2)...]
#
%py3_requires() \
Requires: %(rpm_python3_deps_constraint='%__python3_req_constraint'; %{expand: %%__python3_deps_internal %{*}}) \
%nil

# py3_provides <имя_модуля1> [<имя_модуля2> ...]
#
# 	Позволяет принудительно указать предоставляемые модули Python3. Формируемые
# 	"provides" имеют вид: python3(имя_модуля1) [python3(имя_модуля2)...]
#
%py3_provides() \
Provides: %(%{expand: %%__python3_deps_internal %{*}}) \
%nil

##################################################
# Handling of the ABI dep
#
# It's an ALT Sisyphus special name
# (looking like: python3.3-ABI(64bit)).

# What follows next is a computation of the suffix, which is
# new-arch-ABI-proof and which can be generally useful in all RPM specs
# (not python-specific).
#
# If a new weird arch-ABI arises, at least, we'll have
# a weird suffix here, not coinciding with another existing one:
#
# if "%_lib" == "lib", then empty string;
# otherwise, for example,
# if "%_lib" == "lib64", then "(64bit)" (without the quotation marks).
%ABI_suffix %{?_is_libsuff:(%{_libsuff}bit)}

# %%_python3_abi_version is the ABI version in the target system.
# It is set to the version from the build system in order
# to guarantee the match between the built package contents
# and the properties of the target system
# That's fine for building Python3 modules.
# If one is building python3 package itself,
# one should override %%_python3_abi_version.
%python3_ABI_dep python%_python3_abi_version-ABI%ABI_suffix

%__libpython3 %_libdir/libpython%_python3_abi_version%_python3_abiflags.so
%_files_to_ld_preload_python3 %files_requiring_python3_ABI%(for d in %_python3_compile_include; do echo -n " $d/*"; done)
%files_requiring_python3_ABI %nil

# libpython3*.so (which is to be ld_preloaded) resides in python3-devel
%requires_python3_ABI_for_files() \
BuildPreReq: python3-devel \
Requires: %python3_ABI_dep \
%global files_requiring_python3_ABI %files_requiring_python3_ABI %* \
%nil

# Replace any python shebang with python3 shebang
%python3_fix_shebang() (find %1 -type f -exec sed -i "1s|^#!.*python.*|#!%__python3|" {} +)
