5aca0a6
# This is a backward-compatible suffix used in all pyproject-rpm-macros directories
5aca0a6
# For the main Python it's empty, for all others it's "-3.X"
5aca0a6
%_pyproject_files_pkgversion %{expr:"%{python3_pkgversion}" != "3" ? "-%{python3_pkgversion}" : ""}
5aca0a6
1642d25
# This is a directory where wheels are stored and installed from, absolute
5aca0a6
%_pyproject_wheeldir %{_builddir}%{?buildsubdir:/%{buildsubdir}}/pyproject-wheeldir%{_pyproject_files_pkgversion}
95ba837
95ba837
# This is a directory used as TMPDIR, where pip copies sources to and builds from, relative to PWD
95ba837
# For proper debugsource packages, we create TMPDIR within PWD
95ba837
# See https://github.com/pypa/pip/issues/7555#issuecomment-595180864
95ba837
#
95ba837
# This will be used in debugsource package paths (applies to extension modules only)
95ba837
# NB: pytest collects tests from here if not hidden
95ba837
#     https://docs.pytest.org/en/latest/reference.html#confval-norecursedirs
5aca0a6
%_pyproject_builddir %{_builddir}%{?buildsubdir:/%{buildsubdir}}/.pyproject-builddir%{_pyproject_files_pkgversion}
49a323e
ae639fc
# We prefix all created files with this value to make them unique
ae639fc
# Ideally, we would put them into %%{buildsubdir}, but that value changes during the spec
ae639fc
# The used value is similar to the one used to define the default %%buildroot
5aca0a6
%_pyproject_files_prefix %{name}-%{version}-%{release}.%{_arch}%{_pyproject_files_pkgversion}
ae639fc
ae639fc
%pyproject_files %{_builddir}/%{_pyproject_files_prefix}-pyproject-files
ae639fc
%_pyproject_modules %{_builddir}/%{_pyproject_files_prefix}-pyproject-modules
ae639fc
%_pyproject_ghost_distinfo %{_builddir}/%{_pyproject_files_prefix}-pyproject-ghost-distinfo
ae639fc
%_pyproject_record %{_builddir}/%{_pyproject_files_prefix}-pyproject-record
4569036
%_pyproject_buildrequires %{_builddir}/%{_pyproject_files_prefix}-pyproject-buildrequires
2800b49
5470f56
# Avoid leaking %%{_pyproject_builddir} to pytest collection
5470f56
# https://bugzilla.redhat.com/show_bug.cgi?id=1935212
5470f56
# The value is read and used by the %%pytest and %%tox macros:
5470f56
%_set_pytest_addopts %global __pytest_addopts --ignore=%{_pyproject_builddir}
5470f56
156e2fc
%pyproject_wheel(C:) %{expand:\\\
5470f56
%_set_pytest_addopts
29157c1
mkdir -p "%{_pyproject_builddir}"
29157c1
CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}" TMPDIR="%{_pyproject_builddir}" \\\
156e2fc
%{__python3} -Bs %{_rpmconfigdir}/redhat/pyproject_wheel.py %{?**} %{_pyproject_wheeldir}
162b0ca
}
162b0ca
162b0ca
a5e7a3c
%pyproject_build_lib %{!?__pyproject_build_lib_warned:%{warn:The %%{pyproject_build_lib} macro is deprecated.
a5e7a3c
It only works with setuptools and is not build-backend-agnostic.
a5e7a3c
The macro is not scheduled for removal, but there is a possibility of incompatibilities with future versions of setuptools.
a5e7a3c
As a replacement for the macro for the setuptools backend on Fedora 37+, you can use $PWD/build/lib for pure Python packages,
a5e7a3c
or $PWD/build/lib.%%{python3_platform}-cpython-%%{python3_version_nodots} for packages with extension modules.
a5e7a3c
Other build backends and older distributions may need different paths.
a5e7a3c
See https://lists.fedoraproject.org/archives/list/python-devel@lists.fedoraproject.org/thread/HMLOPAU3RZLXD4BOJHTIPKI3I4U6U7OE/ for details.
efe3187
}%global __pyproject_build_lib_warned 1}%{expand:\\\
55905e4
$(
55905e4
pyproject_build_lib=()
1996e90
if [ -d build/lib.%{python3_platform}-cpython-%{python3_version_nodots} ]; then
1996e90
  pyproject_build_lib+=( "${PWD}/build/lib.%{python3_platform}-cpython-%{python3_version_nodots}" )
1996e90
fi
55905e4
if [ -d build/lib.%{python3_platform}-%{python3_version} ]; then
55905e4
  pyproject_build_lib+=( "${PWD}/build/lib.%{python3_platform}-%{python3_version}" )
55905e4
fi
55905e4
if [ -d build/lib ]; then
55905e4
  pyproject_build_lib+=( "${PWD}/build/lib" )
55905e4
fi
55905e4
for directory in $(find "%{_pyproject_builddir}" -type d -wholename "%{_pyproject_builddir}/pip-req-build-*/build/lib.%{python3_platform}-%{python3_version}" 2>/dev/null); do
55905e4
  pyproject_build_lib+=( "${directory}" )
55905e4
done
55905e4
for directory in $(find "%{_pyproject_builddir}" -type d -wholename "%{_pyproject_builddir}/pip-req-build-*/build/lib" 2>/dev/null); do
55905e4
  pyproject_build_lib+=( "${directory}" )
55905e4
done
55905e4
echo $(IFS=:; echo "${pyproject_build_lib[*]}")
55905e4
)}
55905e4
55905e4
162b0ca
%pyproject_install() %{expand:\\\
19f84b1
specifier=$(ls %{_pyproject_wheeldir}/*.whl | xargs basename --multiple | sed -E 's/([^-]+)-([^-]+)-.+\\\.whl/\\\1==\\\2/')
89c9b40
if [ -z $specifier ]; then
89c9b40
  echo 'ERROR: %%%%pyproject_install found no wheel in %%%%{_pyproject_wheeldir} %{_pyproject_wheeldir}' >&2
89c9b40
  exit 1
89c9b40
fi
Owen W. Taylor 946c872
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
fdf5116
if [ -d %{buildroot}%{_bindir} ]; then
a506123
  %py3_shebang_fix %{buildroot}%{_bindir}/*
8cce1fa
  rm -rfv %{buildroot}%{_bindir}/__pycache__
162b0ca
fi
8588098
rm -f %{_pyproject_ghost_distinfo}
8d5b3ac
site_dirs=()
8d5b3ac
# Process %%{python3_sitelib} if exists
fdf5116
if [ -d %{buildroot}%{python3_sitelib} ]; then
8d5b3ac
  site_dirs+=( "%{python3_sitelib}" )
253976c
fi
8d5b3ac
# Process %%{python3_sitearch} if exists and does not equal to %%{python3_sitelib}
cb4e43c
if [ %{buildroot}%{python3_sitearch} != %{buildroot}%{python3_sitelib} ] && [ -d %{buildroot}%{python3_sitearch} ]; then
8d5b3ac
  site_dirs+=( "%{python3_sitearch}" )
8d5b3ac
fi
8d5b3ac
# Process all *.dist-info dirs in sitelib/sitearch
8d5b3ac
for site_dir in ${site_dirs[@]}; do
8d5b3ac
  for distinfo in %{buildroot}$site_dir/*.dist-info; do
8588098
    echo "%ghost ${distinfo#%{buildroot}}" >> %{_pyproject_ghost_distinfo}
cb4e43c
    sed -i 's/pip/rpm/' ${distinfo}/INSTALLER
8d5b3ac
    PYTHONPATH=%{_rpmconfigdir}/redhat \\
8d5b3ac
      %{__python3} -B %{_rpmconfigdir}/redhat/pyproject_preprocess_record.py \\
8588098
      --buildroot %{buildroot} --record ${distinfo}/RECORD --output %{_pyproject_record}
c5d2f62
    rm -fv ${distinfo}/RECORD
c5d2f62
    rm -fv ${distinfo}/REQUESTED
cb4e43c
  done
8d5b3ac
done
8588098
lines=$(wc -l %{_pyproject_ghost_distinfo} | cut -f1 -d" ")
cb4e43c
if [ $lines -ne 1 ]; then
3309d2d
  echo -e "\\n\\nWARNING: %%%%pyproject_extras_subpkg won't work without explicit -i or -F, found $lines dist-info directories.\\n\\n" >&2
8588098
  rm %{_pyproject_ghost_distinfo}  # any attempt to use this will fail
fdf5116
fi
fdf5116
}
fdf5116
2800b49
7e5adc9
# 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
8588098
%pyproject_extras_subpkg(n:i:f:F) %{expand:%{?python_extras_subpkg:%{python_extras_subpkg%{?!-i:%{?!-f:%{?!-F: -f %{_pyproject_ghost_distinfo}}}} %**}}}
cb4e43c
cb4e43c
638ba27
# Escaping an actual percentage sign in path by 8 signs has been verified in RPM 4.16 and 4.17.
638ba27
# See this thread http://lists.rpm.org/pipermail/rpm-list/2021-June/002048.html
4b32bbb
# Since RPM 4.19, 2 signs are needed instead. 4.18.90+ is a pre-release of RPM 4.19.
638ba27
# On the CI, we build tests/escape_percentages.spec to verify the assumptions.
f392619
%pyproject_save_files(lL) %{expand:\\\
4b32bbb
%{expr:v"0%{?rpmversion}" >= v"4.18.90" ? "RPM_PERCENTAGES_COUNT=2" : "RPM_PERCENTAGES_COUNT=8" } \\
2800b49
%{__python3} %{_rpmconfigdir}/redhat/pyproject_save_files.py \\
c1baa53
  --output-files "%{pyproject_files}" \\
c1baa53
  --output-modules "%{_pyproject_modules}" \\
2800b49
  --buildroot "%{buildroot}" \\
2800b49
  --sitelib "%{python3_sitelib}" \\
2800b49
  --sitearch "%{python3_sitearch}" \\
2800b49
  --python-version "%{python3_version}" \\
8588098
  --pyproject-record "%{_pyproject_record}" \\
c3a20e9
  --prefix "%{_prefix}" \\
f392619
  %{**}
2800b49
}
2800b49
c1baa53
# -t - Process only top-level modules
c1baa53
# -e - Exclude the module names matching given glob, may be used repeatedly
c1baa53
%pyproject_check_import(e:t) %{expand:\\\
c1baa53
if [ ! -f "%{_pyproject_modules}" ]; then
c1baa53
  echo 'ERROR: %%%%pyproject_check_import only works when %%%%pyproject_save_files is used' >&2
c1baa53
  exit 1
c1baa53
fi
c1baa53
%py3_check_import -f "%{_pyproject_modules}" %{?**}
c1baa53
}
c1baa53
2800b49
97a7574
%_pyproject_check_import_allow_no_modules(e:t) \
97a7574
if [ -z "$(cat %{_pyproject_modules})" ]; then\
97a7574
  echo "No modules to check found, exiting check"\
97a7574
else\
97a7574
  %pyproject_check_import %{?**}\
97a7574
fi
97a7574
97a7574
ec07317
%default_toxenv py%{python3_version_nodots}
ec07317
%toxenv %{default_toxenv}
0124d2a
2800b49
a4d05ba
# Note: Keep the options in sync with this macro from macros.aaa-pyproject-srpm
156e2fc
%pyproject_buildrequires(rRxtNwe:C:) %{expand:\\\
40f6765
%_set_pytest_addopts
40f6765
# The _auto_set_build_flags feature does not do this in %%generate_buildrequires section,
40f6765
# but we want to get an environment consistent with %%build:
40f6765
%{?_auto_set_build_flags:%set_build_flags}
5b7df3c
# The default flags expect the package note file to exist
5b7df3c
# see https://bugzilla.redhat.com/show_bug.cgi?id=2097535
5b7df3c
%{?_package_note_flags:%_generate_package_note_file}
40f6765
%{-R:
40f6765
%{-r:%{error:The -R and -r options are mutually exclusive}}
89f2408
%{-x:%{error:The -R and -x options are mutually exclusive}}
89f2408
%{-e:%{error:The -R and -e options are mutually exclusive}}
89f2408
%{-t:%{error:The -R and -t options are mutually exclusive}}
40f6765
%{-w:%{error:The -R and -w options are mutually exclusive}}
40f6765
}
d6ad9a7
%{-N:
d6ad9a7
%{-r:%{error:The -N and -r options are mutually exclusive}}
d6ad9a7
%{-x:%{error:The -N and -x options are mutually exclusive}}
d6ad9a7
%{-e:%{error:The -N and -e options are mutually exclusive}}
d6ad9a7
%{-t:%{error:The -N and -t options are mutually exclusive}}
40f6765
%{-w:%{error:The -N and -w options are mutually exclusive}}
1dd4353
%{-C:%{error:The -N and -C options are mutually exclusive}}
d6ad9a7
}
38ef5fb
%{-e:%{expand:%global toxenv %(%{__python3} -s %{_rpmconfigdir}/redhat/pyproject_construct_toxenv.py %{?**})}}
a4d05ba
echo 'pyproject-rpm-macros'  # first stdout line matches the implementation in macros.aaa-pyproject-srpm
5561755
echo 'python%{python3_pkgversion}-devel'
5561755
echo 'python%{python3_pkgversion}dist(pip) >= 19'
5561755
echo 'python%{python3_pkgversion}dist(packaging)'
d6ad9a7
%{!-N:if [ -f pyproject.toml ]; then
07577de
  %["%{python3_pkgversion}" == "3"
5ab7319
    ? "echo '(python%{python3_pkgversion}dist(tomli) if python%{python3_pkgversion}-devel < 3.11)'"
07577de
    : "%[v"%{python3_pkgversion}" < v"3.11"
5ab7319
       ? "echo 'python%{python3_pkgversion}dist(tomli)'"
07577de
       : "true # will use tomllib, echo nothing"
07577de
    ]"
07577de
  ]
5b1caad
elif [ -f setup.py ]; then
ff39661
  # Note: If the default requirements change, also change them in the script!
ff39661
  echo 'python%{python3_pkgversion}dist(setuptools) >= 40.8'
ff39661
  echo 'python%{python3_pkgversion}dist(wheel)'
5b1caad
else
5b1caad
  echo 'ERROR: Neither pyproject.toml nor setup.py found, consider using %%%%pyproject_buildrequires -N <requirements-file> if this is not a Python package.' >&2
5b1caad
  exit 1
d6ad9a7
fi}
bc156c4
# setuptools assumes no pre-existing dist-info
1026263
rm -rfv *.dist-info/ >&2
fdf5116
if [ -f %{__python3} ]; then
40f6765
  mkdir -p "%{_pyproject_builddir}"
4569036
  echo -n > %{_pyproject_buildrequires}
40f6765
  CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}" TMPDIR="%{_pyproject_builddir}" \\\
4569036
  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
4569036
  cat %{_pyproject_buildrequires}
162b0ca
fi
1e37a4d
# Incomplete .dist-info dir might confuse importlib.metadata
1e37a4d
rm -rfv *.dist-info/ >&2
162b0ca
}
ec07317
2800b49
ec07317
%tox(e:) %{expand:\\\
ec07317
TOX_TESTENV_PASSENV="${TOX_TESTENV_PASSENV:-*}" \\
f21e684
%{?py3_test_envvars}%{?!py3_test_envvars:PYTHONDONTWRITEBYTECODE=1 \\
ec07317
PATH="%{buildroot}%{_bindir}:$PATH" \\
ec07317
PYTHONPATH="${PYTHONPATH:-%{buildroot}%{python3_sitearch}:%{buildroot}%{python3_sitelib}}" \\
f21e684
%{?__pytest_addopts:PYTEST_ADDOPTS="${PYTEST_ADDOPTS:-} %{__pytest_addopts}"}} \\
37216e7
HOSTNAME="rpmbuild" \\
cb8e334
%{__python3} -m tox --current-env -q --recreate -e "%{-e:%{-e*}}%{!-e:%{toxenv}}" %{?*}
ec07317
}