Skip to content

Commit 60cd1ad

Browse files
committed
Initial pyproject macros based on the ones from Fedora
The implementation is somewhat different, but the interfaces should be about the same.
1 parent 6b09771 commit 60cd1ad

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed

debbuild-macros.spec

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ Provides: debbuild-macros-python3
3838
Provides: python-deb-macros
3939
Provides: python2-deb-macros
4040
Provides: python3-deb-macros
41+
# Provides pyproject macros
42+
Provides: debbuild-macros-pyproject
43+
Provides: pyproject-deb-macros
4144
# Provides perl macros
4245
Provides: debbuild-macros-perl
4346
Provides: perl-deb-macros
@@ -91,6 +94,7 @@ with Debian Policy.
9194
mkdir -p %{buildroot}%{_debconfigdir}
9295
cp -av gpgverify %{buildroot}%{_debconfigdir}
9396
cp -av cmake/cmake-* %{buildroot}%{_debconfigdir}
97+
cp -av pyproject/pyproject-* %{buildroot}%{_debconfigdir}
9498
mkdir -p %{buildroot}%{_debmacrodir}
9599
cp -av macros.* %{buildroot}%{_debmacrodir}
96100

@@ -103,6 +107,7 @@ rm -fv %{buildroot}%{_debmacrodir}/macros.systemd
103107
%license LICENSE*
104108
%{_debconfigdir}/gpgverify
105109
%{_debconfigdir}/cmake-*
110+
%{_debconfigdir}/pyproject-*
106111
%{_debmacrodir}/macros.*
107112

108113

macros.pyproject

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# SPDX-License-Identifier: MIT
2+
3+
# This is a directory where wheels are stored and installed from, relative to PWD
4+
%_pyproject_wheeldir pyproject-wheeldir
5+
6+
# This is a directory used as TMPDIR, where pip copies sources to and builds from, relative to PWD
7+
# For proper debugsource packages, we create TMPDIR within PWD
8+
# See https://github.com/pypa/pip/issues/7555#issuecomment-595180864
9+
#
10+
# This will be used in debugsource package paths (applies to extension modules only)
11+
# NB: pytest collects tests from here if not hidden
12+
# https://docs.pytest.org/en/latest/reference.html#confval-norecursedirs
13+
%_pyproject_builddir .pyproject-builddir
14+
15+
%pyproject_files %{_builddir}/pyproject-files
16+
%pyproject_ghost_distinfo %{_builddir}/pyproject-ghost-distinfo
17+
%pyproject_record %{_builddir}/pyproject-record
18+
19+
%pyproject_wheel() %{expand:\\\
20+
export TMPDIR="${PWD}/%{_pyproject_builddir}"
21+
mkdir -p "${TMPDIR}"
22+
CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}" \\\
23+
%{__python3} -m pip wheel --wheel-dir %{_pyproject_wheeldir} --no-deps --use-pep517 --no-build-isolation --disable-pip-version-check --no-clean --progress-bar off --verbose .
24+
}
25+
26+
27+
%pyproject_install() %{expand:\\\
28+
specifier=$(ls %{_pyproject_wheeldir}/*.whl | xargs basename --multiple | sed -E 's/([^-]+)-([^-]+)-.+\\\.whl/\\\1==\\\2/')
29+
export TMPDIR="${PWD}/%{_pyproject_builddir}"
30+
%{__python3} -m pip install --root %{buildroot} --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
31+
if [ -d %{buildroot}%{_bindir} ]; then
32+
%py3_shebang_fix %{buildroot}%{_bindir}/*
33+
rm -rfv %{buildroot}%{_bindir}/__pycache__
34+
fi
35+
rm -f %{pyproject_ghost_distinfo}
36+
site_dirs=()
37+
# Process %%{python3_sitelib} if exists
38+
if [ -d %{buildroot}%{python3_sitelib} ]; then
39+
site_dirs+=( "%{python3_sitelib}" )
40+
fi
41+
# Process %%{python3_sitearch} if exists and does not equal to %%{python3_sitelib}
42+
if [ %{buildroot}%{python3_sitearch} != %{buildroot}%{python3_sitelib} ] && [ -d %{buildroot}%{python3_sitearch} ]; then
43+
site_dirs+=( "%{python3_sitearch}" )
44+
fi
45+
# Process all *.dist-info dirs in sitelib/sitearch
46+
for site_dir in ${site_dirs[@]}; do
47+
for distinfo in %{buildroot}$site_dir/*.dist-info; do
48+
echo "%ghost ${distinfo#%{buildroot}}" >> %{pyproject_ghost_distinfo}
49+
sed -i 's/pip/rpm/' ${distinfo}/INSTALLER
50+
PYTHONPATH=%{_rpmconfigdir}/redhat \\
51+
%{__python3} -B %{_rpmconfigdir}/redhat/pyproject_preprocess_record.py \\
52+
--buildroot %{buildroot} --record ${distinfo}/RECORD --output %{pyproject_record}
53+
rm -fv ${distinfo}/RECORD
54+
rm -fv ${distinfo}/REQUESTED
55+
done
56+
done
57+
lines=$(wc -l %{pyproject_ghost_distinfo} | cut -f1 -d" ")
58+
if [ $lines -ne 1 ]; then
59+
echo -e "\\n\\nWARNING: %%%%pyproject_extras_subpkg won't work without explicit -i or -F, found $lines dist-info directories.\\n\\n" >/dev/stderr
60+
rm %{pyproject_ghost_distinfo} # any attempt to use this will fail
61+
fi
62+
}
63+
64+
65+
# 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
66+
%pyproject_extras_subpkg(n:i:f:F) %{expand:%{?python_extras_subpkg:%{python_extras_subpkg%{?!-i:%{?!-f:%{?!-F: -f %{pyproject_ghost_distinfo}}}} %**}}}
67+
68+
69+
%pyproject_save_files() %{expand:\\\
70+
%{__python3} %{_rpmconfigdir}/redhat/pyproject_save_files.py \\
71+
--output "%{pyproject_files}" \\
72+
--buildroot "%{buildroot}" \\
73+
--sitelib "%{python3_sitelib}" \\
74+
--sitearch "%{python3_sitearch}" \\
75+
--python-version "%{python3_version}" \\
76+
--pyproject-record "%{pyproject_record}" \\
77+
%{*}
78+
}
79+
80+
81+
%default_toxenv py%{python3_version_nodots}
82+
%toxenv %{default_toxenv}
83+
84+
85+
%pyproject_buildrequires(rxte:) %{expand:\\\
86+
%{-e:%{expand:%global toxenv %(%{__python3} -s %{_rpmconfigdir}/redhat/pyproject_construct_toxenv.py %{?**})}}
87+
echo 'python%{python3_pkgversion}-devel'
88+
echo 'python%{python3_pkgversion}dist(pip) >= 19'
89+
echo 'python%{python3_pkgversion}dist(packaging)'
90+
if [ -f pyproject.toml ]; then
91+
echo 'python%{python3_pkgversion}dist(toml)'
92+
else
93+
# Note: If the default requirements change, also change them in the script!
94+
echo 'python%{python3_pkgversion}dist(setuptools) >= 40.8'
95+
echo 'python%{python3_pkgversion}dist(wheel)'
96+
fi
97+
# Check if we can generate dependencies on Python extras
98+
if [ "%{py_dist_name []}" == "[]" ]; then
99+
extras_flag=%{?!_python_no_extras_requires:--generate-extras}
100+
else
101+
extras_flag=
102+
fi
103+
# setuptools assumes no pre-existing dist-info
104+
rm -rfv *.dist-info/ >&2
105+
if [ -f %{__python3} ]; then
106+
RPM_TOXENV="%{toxenv}" HOSTNAME="rpmbuild" %{__python3} -s %{_rpmconfigdir}/redhat/pyproject_buildrequires.py $extras_flag --python3_pkgversion %{python3_pkgversion} %{?**}
107+
fi
108+
}
109+
110+
111+
%tox(e:) %{expand:\\\
112+
TOX_TESTENV_PASSENV="${TOX_TESTENV_PASSENV:-*}" \\
113+
PYTHONDONTWRITEBYTECODE=1 \\
114+
PATH="%{buildroot}%{_bindir}:$PATH" \\
115+
PYTHONPATH="${PYTHONPATH:-%{buildroot}%{python3_sitearch}:%{buildroot}%{python3_sitelib}}" \\
116+
HOSTNAME="rpmbuild" \\
117+
%{__python3} -m tox --current-env -q --recreate -e "%{-e:%{-e*}}%{!-e:%{toxenv}}" %{?*}
118+
}

pyproject/pyproject-install

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
# Shell wrapper for installing pyproject-based code
3+
# SPDX-License-Identifier: MIT
4+
5+
# Collect arguments
6+
__pyproject_pyinterp="$1"
7+
__pyproject_builddir="$2"
8+
__pyproject_wheeldir="$3"
9+
__pyproject_sitearchdir="$4"
10+
__pyproject_sitelibdir="$5"
11+
12+
specifier=$(ls ${__pyproject_wheeldir}/*.whl | xargs basename --multiple | sed -E 's/([^-]+)-([^-]+)-.+\\\.whl/\\\1==\\\2/')
13+
export TMPDIR="${PWD}/${__pyproject_builddir}"
14+
${__pyproject_pyinterp} -m pip install --root ${RPM_BUILD_ROOT} --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
15+
if [ -d ${RPM_BUILD_ROOT}/usr/bin ]; then
16+
pathfix.py -pni "%{__python3}" -k -as ${RPM_BUILD_ROOT}/usr/bin/*
17+
rm -rfv ${RPM_BUILD_ROOT}/usr/bin/__pycache__
18+
fi
19+
if [ -d ${RPM_BUILD_ROOT}${__pyproject_sitelibdir} ]; then
20+
sed -i 's/pip/deb/' ${RPM_BUILD_ROOT}${__pyproject_sitelibdir}/*.dist-info/INSTALLER
21+
fi
22+
if [ -d ${RPM_BUILD_ROOT}${__pyproject_sitearchdir} ]; then
23+
sed -i 's/pip/deb/' ${RPM_BUILD_ROOT}${__pyproject_sitearchdir}/*.dist-info/INSTALLER
24+
fi
25+
26+
27+
exit $?

pyproject/pyproject-wheel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
# Shell wrapper for make wheels with pip
3+
# SPDX-License-Identifier: MIT
4+
5+
# Collect arguments
6+
__pyproject_pyinterp="$1"
7+
__pyproject_builddir="$2"
8+
__pyproject_wheeldir="$3"
9+
__build_flags="${@:4}"
10+
11+
export TMPDIR="${PWD}/${__pyproject_builddir}"
12+
mkdir -p "${TMPDIR}"
13+
CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}" \
14+
${__pyproject_pyinterp} -m pip wheel --wheel-dir ${__pyproject_wheeldir} --no-deps --use-pep517 --no-build-isolation --disable-pip-version-check --no-clean --progress-bar off --verbose .
15+
16+
exit $?

0 commit comments

Comments
 (0)