2024-01-25 16:08:50 +00:00
# This is a backward-compatible suffix used in all pyproject-rpm-macros directories
# For the main Python it's empty, for all others it's "-3.X"
%_pyproject_files_pkgversion %{expr:"%{python3_pkgversion}" != "3" ? "-%{python3_pkgversion}" : ""}
2022-04-29 09:21:18 +00:00
# This is a directory where wheels are stored and installed from, absolute
2024-01-25 16:08:50 +00:00
%_pyproject_wheeldir %{_builddir}%{?buildsubdir:/%{buildsubdir}}/pyproject-wheeldir%{_pyproject_files_pkgversion}
2020-04-16 13:42:50 +00:00
# This is a directory used as TMPDIR, where pip copies sources to and builds from, relative to PWD
# For proper debugsource packages, we create TMPDIR within PWD
# See https://github.com/pypa/pip/issues/7555#issuecomment-595180864
#
# This will be used in debugsource package paths (applies to extension modules only)
# NB: pytest collects tests from here if not hidden
# https://docs.pytest.org/en/latest/reference.html#confval-norecursedirs
2024-01-25 16:08:50 +00:00
%_pyproject_builddir %{_builddir}%{?buildsubdir:/%{buildsubdir}}/.pyproject-builddir%{_pyproject_files_pkgversion}
2019-11-13 11:39:36 +00:00
2022-03-22 15:48:20 +00:00
# We prefix all created files with this value to make them unique
# Ideally, we would put them into %%{buildsubdir}, but that value changes during the spec
# The used value is similar to the one used to define the default %%buildroot
2024-01-25 16:08:50 +00:00
%_pyproject_files_prefix %{name}-%{version}-%{release}.%{_arch}%{_pyproject_files_pkgversion}
2022-03-22 15:48:20 +00:00
%pyproject_files %{_builddir}/%{_pyproject_files_prefix}-pyproject-files
%_pyproject_modules %{_builddir}/%{_pyproject_files_prefix}-pyproject-modules
%_pyproject_ghost_distinfo %{_builddir}/%{_pyproject_files_prefix}-pyproject-ghost-distinfo
%_pyproject_record %{_builddir}/%{_pyproject_files_prefix}-pyproject-record
2023-03-31 16:57:54 +00:00
%_pyproject_buildrequires %{_builddir}/%{_pyproject_files_prefix}-pyproject-buildrequires
2020-04-15 14:44:04 +00:00
2024-09-23 13:53:23 +00:00
# Internal macro, takes %%set_build_flags and strips all the exports
# TODO: Make such a list an actual source of %%set_build_flags (in redhat-rpm-config)
# Cannot use %%gsub directly to preserve EL 9 compatibility
%_pyproject_build_flags %{lua:local exports = rpm.expand('%{set_build_flags} ;'); print((exports:gsub('%s*;+%s+export%s+[%u_]+%s*;+%s*', ' ')))}
2021-06-26 14:31:09 +00:00
# Avoid leaking %%{_pyproject_builddir} to pytest collection
# https://bugzilla.redhat.com/show_bug.cgi?id=1935212
# The value is read and used by the %%pytest and %%tox macros:
%_set_pytest_addopts %global __pytest_addopts --ignore=%{_pyproject_builddir}
2023-05-20 00:37:27 +00:00
%pyproject_wheel(C:) %{expand:\\\
2021-06-26 14:31:09 +00:00
%_set_pytest_addopts
2021-05-27 13:16:16 +00:00
mkdir -p "%{_pyproject_builddir}"
2024-09-23 13:53:23 +00:00
%{_pyproject_build_flags} \\\
TMPDIR="%{_pyproject_builddir}" \\\
2023-05-20 00:37:27 +00:00
%{__python3} -Bs %{_rpmconfigdir}/redhat/pyproject_wheel.py %{?**} %{_pyproject_wheeldir}
2019-07-02 10:41:04 +00:00
}
2023-04-18 09:55:52 +00:00
%pyproject_build_lib %{!?__pyproject_build_lib_warned:%{warn:The %%{pyproject_build_lib} macro is deprecated.
It only works with setuptools and is not build-backend-agnostic.
The macro is not scheduled for removal, but there is a possibility of incompatibilities with future versions of setuptools.
As a replacement for the macro for the setuptools backend on Fedora 37+, you can use $PWD/build/lib for pure Python packages,
or $PWD/build/lib.%%{python3_platform}-cpython-%%{python3_version_nodots} for packages with extension modules.
Other build backends and older distributions may need different paths.
See https://lists.fedoraproject.org/archives/list/python-devel@lists.fedoraproject.org/thread/HMLOPAU3RZLXD4BOJHTIPKI3I4U6U7OE/ for details.
2023-05-30 09:24:39 +00:00
}%global __pyproject_build_lib_warned 1}%{expand:\\\
2021-12-08 10:07:48 +00:00
$(
pyproject_build_lib=()
2022-06-15 15:43:26 +00:00
if [ -d build/lib.%{python3_platform}-cpython-%{python3_version_nodots} ]; then
pyproject_build_lib+=( "${PWD}/build/lib.%{python3_platform}-cpython-%{python3_version_nodots}" )
fi
2021-12-08 10:07:48 +00:00
if [ -d build/lib.%{python3_platform}-%{python3_version} ]; then
pyproject_build_lib+=( "${PWD}/build/lib.%{python3_platform}-%{python3_version}" )
fi
if [ -d build/lib ]; then
pyproject_build_lib+=( "${PWD}/build/lib" )
fi
for directory in $(find "%{_pyproject_builddir}" -type d -wholename "%{_pyproject_builddir}/pip-req-build-*/build/lib.%{python3_platform}-%{python3_version}" 2>/dev/null); do
pyproject_build_lib+=( "${directory}" )
done
for directory in $(find "%{_pyproject_builddir}" -type d -wholename "%{_pyproject_builddir}/pip-req-build-*/build/lib" 2>/dev/null); do
pyproject_build_lib+=( "${directory}" )
done
echo $(IFS=:; echo "${pyproject_build_lib[*]}")
)}
2019-07-02 10:41:04 +00:00
%pyproject_install() %{expand:\\\
2020-05-06 10:55:13 +00:00
specifier=$(ls %{_pyproject_wheeldir}/*.whl | xargs basename --multiple | sed -E 's/([^-]+)-([^-]+)-.+\\\.whl/\\\1==\\\2/')
2023-12-11 13:16:03 +00:00
if [ -z $specifier ]; then
echo 'ERROR: %%%%pyproject_install found no wheel in %%%%{_pyproject_wheeldir} %{_pyproject_wheeldir}' >&2
exit 1
fi
2022-05-27 11:52:41 +00:00
TMPDIR="%{_pyproject_builddir}" %{__python3} -m pip install --root %{buildroot} --prefix %{_prefix} --no-deps --disable-pip-version-check --progress-bar off --verbose --ignore-installed --no-warn-script-location --no-index --no-cache-dir --find-links %{_pyproject_wheeldir} $specifier
2019-07-02 14:53:05 +00:00
if [ -d %{buildroot}%{_bindir} ]; then
2020-09-04 10:31:37 +00:00
%py3_shebang_fix %{buildroot}%{_bindir}/*
2019-11-13 11:41:31 +00:00
rm -rfv %{buildroot}%{_bindir}/__pycache__
2019-07-02 10:41:04 +00:00
fi
2021-07-20 13:35:18 +00:00
rm -f %{_pyproject_ghost_distinfo}
2020-09-22 11:38:34 +00:00
site_dirs=()
# Process %%{python3_sitelib} if exists
2019-07-02 14:53:05 +00:00
if [ -d %{buildroot}%{python3_sitelib} ]; then
2020-09-22 11:38:34 +00:00
site_dirs+=( "%{python3_sitelib}" )
2019-07-02 11:57:24 +00:00
fi
2020-09-22 11:38:34 +00:00
# Process %%{python3_sitearch} if exists and does not equal to %%{python3_sitelib}
2020-07-29 10:49:15 +00:00
if [ %{buildroot}%{python3_sitearch} != %{buildroot}%{python3_sitelib} ] && [ -d %{buildroot}%{python3_sitearch} ]; then
2020-09-22 11:38:34 +00:00
site_dirs+=( "%{python3_sitearch}" )
fi
# Process all *.dist-info dirs in sitelib/sitearch
for site_dir in ${site_dirs[@]}; do
for distinfo in %{buildroot}$site_dir/*.dist-info; do
2021-07-20 13:35:18 +00:00
echo "%ghost ${distinfo#%{buildroot}}" >> %{_pyproject_ghost_distinfo}
2020-07-29 10:49:15 +00:00
sed -i 's/pip/rpm/' ${distinfo}/INSTALLER
2020-09-22 11:38:34 +00:00
PYTHONPATH=%{_rpmconfigdir}/redhat \\
%{__python3} -B %{_rpmconfigdir}/redhat/pyproject_preprocess_record.py \\
2021-07-20 13:35:18 +00:00
--buildroot %{buildroot} --record ${distinfo}/RECORD --output %{_pyproject_record}
2020-09-29 11:04:54 +00:00
rm -fv ${distinfo}/RECORD
rm -fv ${distinfo}/REQUESTED
2020-07-29 10:49:15 +00:00
done
2020-09-22 11:38:34 +00:00
done
2021-07-20 13:35:18 +00:00
lines=$(wc -l %{_pyproject_ghost_distinfo} | cut -f1 -d" ")
2020-07-29 10:49:15 +00:00
if [ $lines -ne 1 ]; then
2021-05-27 18:42:37 +00:00
echo -e "\\n\\nWARNING: %%%%pyproject_extras_subpkg won't work without explicit -i or -F, found $lines dist-info directories.\\n\\n" >&2
2021-07-20 13:35:18 +00:00
rm %{_pyproject_ghost_distinfo} # any attempt to use this will fail
2019-07-02 14:53:05 +00:00
fi
}
2020-04-15 14:44:04 +00:00
2020-08-04 13:08:18 +00:00
# Note: the three times nested questionmarked -i -f -F pattern means: If none of those options was used -- in that case, we inject our own -f
2024-06-25 10:27:56 +00:00
%pyproject_extras_subpkg(n:i:f:FaA) %{expand:%{?python_extras_subpkg:%{python_extras_subpkg%{?!-i:%{?!-f:%{?!-F: -f %{_pyproject_ghost_distinfo}}}} %**}}}
2020-07-29 10:49:15 +00:00
2024-05-22 20:56:28 +00:00
# Escaping shell-globs, percentage signs and spaces was reworked in RPM 4.19+
# https://github.com/rpm-software-management/rpm/issues/1749#issuecomment-1020420616
# Since we support both ways, we pass either 4.19 or 4.18 to the script, so it knows which one to use
# Rather than passing the actual version, we let RPM compare the versions, as it is easier done here than in Python
2023-09-27 12:37:15 +00:00
%pyproject_save_files(lL) %{expand:\\\
2024-05-22 20:56:28 +00:00
%{expr:v"0%{?rpmversion}" >= v"4.18.90" ? "RPM_FILES_ESCAPE=4.19" : "RPM_FILES_ESCAPE=4.18" } \\
2020-04-15 14:44:04 +00:00
%{__python3} %{_rpmconfigdir}/redhat/pyproject_save_files.py \\
2021-10-27 09:28:45 +00:00
--output-files "%{pyproject_files}" \\
--output-modules "%{_pyproject_modules}" \\
2020-04-15 14:44:04 +00:00
--buildroot "%{buildroot}" \\
--sitelib "%{python3_sitelib}" \\
--sitearch "%{python3_sitearch}" \\
--python-version "%{python3_version}" \\
2021-07-20 13:35:18 +00:00
--pyproject-record "%{_pyproject_record}" \\
2022-01-17 16:20:54 +00:00
--prefix "%{_prefix}" \\
2023-09-27 12:37:15 +00:00
%{**}
2020-04-15 14:44:04 +00:00
}
2021-10-27 09:28:45 +00:00
# -t - Process only top-level modules
# -e - Exclude the module names matching given glob, may be used repeatedly
%pyproject_check_import(e:t) %{expand:\\\
if [ ! -f "%{_pyproject_modules}" ]; then
echo 'ERROR: %%%%pyproject_check_import only works when %%%%pyproject_save_files is used' >&2
exit 1
fi
%py3_check_import -f "%{_pyproject_modules}" %{?**}
}
2020-04-15 14:44:04 +00:00
2023-09-13 10:50:59 +00:00
%_pyproject_check_import_allow_no_modules(e:t) \
if [ -z "$(cat %{_pyproject_modules})" ]; then\
echo "No modules to check found, exiting check"\
else\
%pyproject_check_import %{?**}\
fi
2019-07-26 15:50:36 +00:00
%default_toxenv py%{python3_version_nodots}
%toxenv %{default_toxenv}
2019-07-26 14:15:59 +00:00
2024-11-04 12:32:52 +00:00
%_pyproject_tomlidep %["%{python3_pkgversion}" == "3"\
? "echo '(python%{python3_pkgversion}dist(tomli) if python%{python3_pkgversion}-devel < 3.11)'"\
: "%[v"%{python3_pkgversion}" < v"3.11"\
? "echo 'python%{python3_pkgversion}dist(tomli)'"\
: "true # will use tomllib, echo nothing"\
]"\
]
2020-04-15 14:44:04 +00:00
2023-01-06 12:41:07 +00:00
# Note: Keep the options in sync with this macro from macros.aaa-pyproject-srpm
2024-11-04 12:32:52 +00:00
%pyproject_buildrequires(rRxtNwpe:g:C:) %{expand:\\\
2022-04-22 14:48:09 +00:00
%_set_pytest_addopts
2022-06-16 10:44:27 +00:00
# The default flags expect the package note file to exist
# see https://bugzilla.redhat.com/show_bug.cgi?id=2097535
%{?_package_note_flags:%_generate_package_note_file}
2022-04-22 14:48:09 +00:00
%{-R:
%{-r:%{error:The -R and -r options are mutually exclusive}}
2023-12-11 12:48:52 +00:00
%{-x:%{error:The -R and -x options are mutually exclusive}}
%{-e:%{error:The -R and -e options are mutually exclusive}}
%{-t:%{error:The -R and -t options are mutually exclusive}}
2022-04-22 14:48:09 +00:00
%{-w:%{error:The -R and -w options are mutually exclusive}}
2024-09-17 12:44:15 +00:00
%{-p:%{error:The -R and -p options are mutually exclusive}}
2022-04-22 14:48:09 +00:00
}
2021-06-28 11:23:19 +00:00
%{-N:
%{-r:%{error:The -N and -r options are mutually exclusive}}
%{-x:%{error:The -N and -x options are mutually exclusive}}
%{-e:%{error:The -N and -e options are mutually exclusive}}
%{-t:%{error:The -N and -t options are mutually exclusive}}
2022-04-22 14:48:09 +00:00
%{-w:%{error:The -N and -w options are mutually exclusive}}
2024-09-17 12:44:15 +00:00
%{-p:%{error:The -N and -p options are mutually exclusive}}
2023-05-26 05:32:05 +00:00
%{-C:%{error:The -N and -C options are mutually exclusive}}
2024-11-04 12:32:52 +00:00
%{-g:if [ -f pyproject.toml ]; then
%_pyproject_tomlidep
fi}
2021-06-28 11:23:19 +00:00
}
2024-09-17 12:44:15 +00:00
%{-w:
%{-p:%{error:The -w and -p options are mutually exclusive}}
}
2020-11-03 10:29:50 +00:00
%{-e:%{expand:%global toxenv %(%{__python3} -s %{_rpmconfigdir}/redhat/pyproject_construct_toxenv.py %{?**})}}
2023-01-06 12:41:07 +00:00
echo 'pyproject-rpm-macros' # first stdout line matches the implementation in macros.aaa-pyproject-srpm
2020-07-22 16:13:50 +00:00
echo 'python%{python3_pkgversion}-devel'
echo 'python%{python3_pkgversion}dist(packaging)'
2024-09-23 12:19:54 +00:00
%{!-N:echo 'python%{python3_pkgversion}dist(pip) >= 19'
if [ -f pyproject.toml ]; then
2024-11-04 12:32:52 +00:00
%_pyproject_tomlidep
2021-07-14 15:13:34 +00:00
elif [ -f setup.py ]; then
2021-02-02 22:31:44 +00:00
# Note: If the default requirements change, also change them in the script!
echo 'python%{python3_pkgversion}dist(setuptools) >= 40.8'
2021-07-14 15:13:34 +00:00
else
echo 'ERROR: Neither pyproject.toml nor setup.py found, consider using %%%%pyproject_buildrequires -N <requirements-file> if this is not a Python package.' >&2
exit 1
2021-06-28 11:23:19 +00:00
fi}
2019-07-17 16:16:16 +00:00
# setuptools assumes no pre-existing dist-info
2019-10-08 11:01:08 +00:00
rm -rfv *.dist-info/ >&2
2019-07-02 14:53:05 +00:00
if [ -f %{__python3} ]; then
2022-04-22 14:48:09 +00:00
mkdir -p "%{_pyproject_builddir}"
2023-03-31 16:57:54 +00:00
echo -n > %{_pyproject_buildrequires}
2024-09-23 13:53:23 +00:00
%{_pyproject_build_flags} \\\
TMPDIR="%{_pyproject_builddir}" \\\
2023-03-31 16:57:54 +00:00
RPM_TOXENV="%{toxenv}" HOSTNAME="rpmbuild" %{__python3} -Bs %{_rpmconfigdir}/redhat/pyproject_buildrequires.py %{?!_python_no_extras_requires:--generate-extras} --python3_pkgversion %{python3_pkgversion} --wheeldir %{_pyproject_wheeldir} --output %{_pyproject_buildrequires} %{?**} >&2
cat %{_pyproject_buildrequires}
2019-07-02 10:41:04 +00:00
fi
2023-02-13 15:16:58 +00:00
# Incomplete .dist-info dir might confuse importlib.metadata
rm -rfv *.dist-info/ >&2
2019-07-02 10:41:04 +00:00
}
2019-07-26 15:50:36 +00:00
2020-04-15 14:44:04 +00:00
2019-07-26 15:50:36 +00:00
%tox(e:) %{expand:\\\
TOX_TESTENV_PASSENV="${TOX_TESTENV_PASSENV:-*}" \\
2022-11-28 15:29:34 +00:00
%{?py3_test_envvars}%{?!py3_test_envvars:PYTHONDONTWRITEBYTECODE=1 \\
2019-07-26 15:50:36 +00:00
PATH="%{buildroot}%{_bindir}:$PATH" \\
PYTHONPATH="${PYTHONPATH:-%{buildroot}%{python3_sitearch}:%{buildroot}%{python3_sitelib}}" \\
2022-11-28 15:29:34 +00:00
%{?__pytest_addopts:PYTEST_ADDOPTS="${PYTEST_ADDOPTS:-} %{__pytest_addopts}"}} \\
2020-07-15 10:15:19 +00:00
HOSTNAME="rpmbuild" \\
2020-07-21 13:20:12 +00:00
%{__python3} -m tox --current-env -q --recreate -e "%{-e:%{-e*}}%{!-e:%{toxenv}}" %{?*}
2019-07-26 15:50:36 +00:00
}
2024-07-02 21:52:05 +00:00
# Declarative buildsystem, requires RPM 4.20+ to work
# https://rpm-software-management.github.io/rpm/manual/buildsystem.html
%buildsystem_pyproject_conf() %nil
%buildsystem_pyproject_generate_buildrequires() %pyproject_buildrequires %*
%buildsystem_pyproject_build() %pyproject_wheel %*
%buildsystem_pyproject_install() %["%{shrink:%*}" == "" ? "%{error:BuildOption(install) is mandatory with pyproject BuildSystem.}" : "%pyproject_install \
%pyproject_save_files %*"]
%buildsystem_pyproject_check() %pyproject_check_import %*