python-rpm-macros/macros.python3

121 lines
5.7 KiB
Plaintext
Raw Normal View History

Set $RPM_BUILD_ROOT in %{python3_...} macros, for alternate sysconfig install scheme Our Pythons currently patches distutils to install packages to /usr/lib(64)/pythonX.Y/site-packages when the $RPM_BUILD_ROOT environment variable is set (and to /usr/local/lib(64)/pythonX.Y/site-packages otherwise). With the deprecation of distutils [1] we want to change the patch to create and use a different sysconfig install scheme [2]. However, we have realized that macros defined as %(%{__python3} ...) don't "see" the environment variables set by rpmbuild because they are expanded earlier and hence e.g. %{python3_sitelib} evaluates to /usr/local/lib/python3.X/site-packages -- which is not desired. To be able to reliably detect an RPM build environment by checking the presence of the $RPM_BUILD_ROOT environment variable, we manually set it in the macro definitions. Since %{buildroot} in not fully populated (e.g. it can expand literally to /home/anna/rpmbuild/BUILDROOT/%{NAME}-%{VERSION}-%{RELEASE}.x86_64), we don't use it here. The variable simply needs to present in the environment. See also the analysis of the build failures when this is not done [3]. [1] https://www.python.org/dev/peps/pep-0632/ [2] https://bugs.python.org/issue43976 [3] https://src.fedoraproject.org/rpms/python3.10/pull-request/63#comment-79042 ------------------------------------------------------------------------------ Note about the backport: This is not required with the default %__python3 value, but it is required when %__python3 is redefined to /usr/bin/python3.11 (or newer).
2021-09-03 14:06:03 +00:00
# nb: $RPM_BUILD_ROOT is not set when the macros are expanded (at spec parse time)
# so we set it manually (to empty string), making our Python prefer the correct install scheme location
%python3_sitelib %(RPM_BUILD_ROOT= %{__python3} -Ic "import sysconfig; print(sysconfig.get_path('purelib'))")
%python3_sitearch %(RPM_BUILD_ROOT= %{__python3} -Ic "import sysconfig; print(sysconfig.get_path('platlib'))")
%python3_version %(RPM_BUILD_ROOT= %{__python3} -Ic "import sys; sys.stdout.write('{0.major}.{0.minor}'.format(sys.version_info))")
%python3_version_nodots %(RPM_BUILD_ROOT= %{__python3} -Ic "import sys; sys.stdout.write('{0.major}{0.minor}'.format(sys.version_info))")
%python3_platform %(RPM_BUILD_ROOT= %{__python3} -Ic "import sysconfig; print(sysconfig.get_platform())")
%python3_platform_triplet %(RPM_BUILD_ROOT= %{__python3} -Ic "import sysconfig; print(sysconfig.get_config_var('MULTIARCH'))")
%python3_ext_suffix %(RPM_BUILD_ROOT= %{__python3} -Ic "import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))")
%python3_cache_tag %(RPM_BUILD_ROOT= %{__python3} -Ic "import sys; print(sys.implementation.cache_tag)")
2016-01-12 23:52:28 +00:00
%py3dir %{_builddir}/python3-%{name}-%{version}-%{release}
%_py3_shebang_s s
%py3_shbang_opts -%{?_py3_shebang_s}
%py3_shbang_opts_nodash %(opts=%{py3_shbang_opts}; echo ${opts#-})
%py3_shebang_flags %(opts=%{py3_shbang_opts}; echo ${opts#-})
%py3_shebang_fix %{expand:\\\
if [ -f /usr/bin/pathfix%{python3_version}.py ]; then
pathfix=/usr/bin/pathfix%{python3_version}.py
else
# older versions of Python don't have it and must BR /usr/bin/pathfix.py from python3-devel explicitly
pathfix=/usr/bin/pathfix.py
fi
if [ -z "%{?py3_shebang_flags}" ]; then
shebang_flags="-k"
else
shebang_flags="-ka%{py3_shebang_flags}"
fi
$pathfix -pni %{__python3} $shebang_flags}
2016-01-12 23:52:28 +00:00
# Use the slashes after expand so that the command starts on the same line as
# the macro
%py3_build() %{expand:\\\
CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
%{__python3} %{py_setup} %{?py_setup_args} build --executable="%{__python3} %{py3_shbang_opts}" %{?*}
2016-01-12 23:52:28 +00:00
}
%py3_build_egg() %{expand:\\\
CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
%{__python3} %{py_setup} %{?py_setup_args} bdist_egg %{?*}
}
%py3_build_wheel() %{expand:\\\
CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
%{__python3} %{py_setup} %{?py_setup_args} bdist_wheel %{?*}
}
%py3_install() %{expand:\\\
CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
%{__python3} %{py_setup} %{?py_setup_args} install -O1 --skip-build --root %{buildroot} %{?*}
rm -rfv %{buildroot}%{_bindir}/__pycache__
2016-01-12 23:52:28 +00:00
}
%py3_install_egg() %{expand:\\\
mkdir -p %{buildroot}%{python3_sitelib}
%{__python3} -m easy_install -m --prefix %{buildroot}%{_prefix} -Z dist/*-py%{python3_version}.egg %{?*}
rm -rfv %{buildroot}%{_bindir}/__pycache__
}
%py3_install_wheel() %{expand:\\\
%{__python3} -m pip install -I dist/%{1} --root %{buildroot} --no-deps --no-index --no-warn-script-location
rm -rfv %{buildroot}%{_bindir}/__pycache__
for distinfo in %{buildroot}%{python3_sitelib}/*.dist-info %{buildroot}%{python3_sitearch}/*.dist-info; do
if [ -f ${distinfo}/direct_url.json ]; then
rm -fv ${distinfo}/direct_url.json
sed -i '/direct_url.json/d' ${distinfo}/RECORD
fi
done
2016-11-17 04:32:46 +00:00
}
Introduce %py3_check_import With $PATH and $PYTHONPATH set to the %buildroot, the macro tries to import the given Python 3 module(s). Useful as a smoke test in %check when ruining tests is not feasible. Accepts spaces or commas as separators. Package python-six: %check %py3_check_import six Executing(%check): ... ... + PATH=... + PYTHONPATH=... + PYTHONDONTWRITEBYTECODE=1 + /usr/bin/python3 -c 'import six' + RPM_EC=0 ++ jobs -p + exit 0 %py3_check_import six seven ... + /usr/bin/python3 -c 'import six, seven' Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'seven' error: Bad exit status from ... (%check) ... %py3_check_import five, six, seven + /usr/bin/python3 -c 'import five, six, seven' Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'five' error: Bad exit status from ... (%check) Package python-packaging: %py3_check_import packaging, packaging.markers packaging.requirements, packaging.tags Executing(%check): ... ... + PATH=... + PYTHONPATH=... + PYTHONDONTWRITEBYTECODE=1 + /usr/bin/python3 -c 'import packaging, packaging.markers, packaging.requirements, packaging.tags' + RPM_EC=0 ++ jobs -p + exit 0 %py3_check_import packaging, packaging.markers packaging.notachance, packaging.tags ... + /usr/bin/python3 -c 'import packaging, packaging.markers, packaging.notachance, packaging.tags' Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'packaging.notachance' error: Bad exit status from ... (%check)
2021-07-07 12:46:04 +00:00
# With $PATH and $PYTHONPATH set to the %%buildroot,
# try to import the Python 3 module(s) given as command-line args or read from file (-f).
# Respect the custom values of %%py3_shebang_flags or set nothing if it's undefined.
# Filter and check import on only top-level modules using -t flag.
# Exclude unwanted modules by passing their globs to -e option.
Introduce %py3_check_import With $PATH and $PYTHONPATH set to the %buildroot, the macro tries to import the given Python 3 module(s). Useful as a smoke test in %check when ruining tests is not feasible. Accepts spaces or commas as separators. Package python-six: %check %py3_check_import six Executing(%check): ... ... + PATH=... + PYTHONPATH=... + PYTHONDONTWRITEBYTECODE=1 + /usr/bin/python3 -c 'import six' + RPM_EC=0 ++ jobs -p + exit 0 %py3_check_import six seven ... + /usr/bin/python3 -c 'import six, seven' Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'seven' error: Bad exit status from ... (%check) ... %py3_check_import five, six, seven + /usr/bin/python3 -c 'import five, six, seven' Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'five' error: Bad exit status from ... (%check) Package python-packaging: %py3_check_import packaging, packaging.markers packaging.requirements, packaging.tags Executing(%check): ... ... + PATH=... + PYTHONPATH=... + PYTHONDONTWRITEBYTECODE=1 + /usr/bin/python3 -c 'import packaging, packaging.markers, packaging.requirements, packaging.tags' + RPM_EC=0 ++ jobs -p + exit 0 %py3_check_import packaging, packaging.markers packaging.notachance, packaging.tags ... + /usr/bin/python3 -c 'import packaging, packaging.markers, packaging.notachance, packaging.tags' Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'packaging.notachance' error: Bad exit status from ... (%check)
2021-07-07 12:46:04 +00:00
# Useful as a smoke test in %%check when running tests is not feasible.
# Use spaces or commas as separators if providing list directly.
# Use newlines as separators if providing list in a file.
%py3_check_import(e:tf:) %{expand:\\\
Introduce %py3_check_import With $PATH and $PYTHONPATH set to the %buildroot, the macro tries to import the given Python 3 module(s). Useful as a smoke test in %check when ruining tests is not feasible. Accepts spaces or commas as separators. Package python-six: %check %py3_check_import six Executing(%check): ... ... + PATH=... + PYTHONPATH=... + PYTHONDONTWRITEBYTECODE=1 + /usr/bin/python3 -c 'import six' + RPM_EC=0 ++ jobs -p + exit 0 %py3_check_import six seven ... + /usr/bin/python3 -c 'import six, seven' Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'seven' error: Bad exit status from ... (%check) ... %py3_check_import five, six, seven + /usr/bin/python3 -c 'import five, six, seven' Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'five' error: Bad exit status from ... (%check) Package python-packaging: %py3_check_import packaging, packaging.markers packaging.requirements, packaging.tags Executing(%check): ... ... + PATH=... + PYTHONPATH=... + PYTHONDONTWRITEBYTECODE=1 + /usr/bin/python3 -c 'import packaging, packaging.markers, packaging.requirements, packaging.tags' + RPM_EC=0 ++ jobs -p + exit 0 %py3_check_import packaging, packaging.markers packaging.notachance, packaging.tags ... + /usr/bin/python3 -c 'import packaging, packaging.markers, packaging.notachance, packaging.tags' Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'packaging.notachance' error: Bad exit status from ... (%check)
2021-07-07 12:46:04 +00:00
PATH="%{buildroot}%{_bindir}:$PATH"\\\
PYTHONPATH="${PYTHONPATH:-%{buildroot}%{python3_sitearch}:%{buildroot}%{python3_sitelib}}"\\\
_PYTHONSITE="%{buildroot}%{python3_sitearch}:%{buildroot}%{python3_sitelib}"\\\
Introduce %py3_check_import With $PATH and $PYTHONPATH set to the %buildroot, the macro tries to import the given Python 3 module(s). Useful as a smoke test in %check when ruining tests is not feasible. Accepts spaces or commas as separators. Package python-six: %check %py3_check_import six Executing(%check): ... ... + PATH=... + PYTHONPATH=... + PYTHONDONTWRITEBYTECODE=1 + /usr/bin/python3 -c 'import six' + RPM_EC=0 ++ jobs -p + exit 0 %py3_check_import six seven ... + /usr/bin/python3 -c 'import six, seven' Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'seven' error: Bad exit status from ... (%check) ... %py3_check_import five, six, seven + /usr/bin/python3 -c 'import five, six, seven' Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'five' error: Bad exit status from ... (%check) Package python-packaging: %py3_check_import packaging, packaging.markers packaging.requirements, packaging.tags Executing(%check): ... ... + PATH=... + PYTHONPATH=... + PYTHONDONTWRITEBYTECODE=1 + /usr/bin/python3 -c 'import packaging, packaging.markers, packaging.requirements, packaging.tags' + RPM_EC=0 ++ jobs -p + exit 0 %py3_check_import packaging, packaging.markers packaging.notachance, packaging.tags ... + /usr/bin/python3 -c 'import packaging, packaging.markers, packaging.notachance, packaging.tags' Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'packaging.notachance' error: Bad exit status from ... (%check)
2021-07-07 12:46:04 +00:00
PYTHONDONTWRITEBYTECODE=1\\\
%{lua:
local command = "%{__python3} "
if rpm.expand("%{?py3_shebang_flags}") ~= "" then
command = command .. "-%{py3_shebang_flags}"
end
command = command .. " %{_rpmconfigdir}/redhat/import_all_modules.py "
-- handle multiline arguments correctly, see https://bugzilla.redhat.com/2018809
local args=rpm.expand('%{?**}'):gsub("[%s\\\\]*%s+", " ")
print(command .. args)
}
}
Introduce %py3_check_import With $PATH and $PYTHONPATH set to the %buildroot, the macro tries to import the given Python 3 module(s). Useful as a smoke test in %check when ruining tests is not feasible. Accepts spaces or commas as separators. Package python-six: %check %py3_check_import six Executing(%check): ... ... + PATH=... + PYTHONPATH=... + PYTHONDONTWRITEBYTECODE=1 + /usr/bin/python3 -c 'import six' + RPM_EC=0 ++ jobs -p + exit 0 %py3_check_import six seven ... + /usr/bin/python3 -c 'import six, seven' Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'seven' error: Bad exit status from ... (%check) ... %py3_check_import five, six, seven + /usr/bin/python3 -c 'import five, six, seven' Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'five' error: Bad exit status from ... (%check) Package python-packaging: %py3_check_import packaging, packaging.markers packaging.requirements, packaging.tags Executing(%check): ... ... + PATH=... + PYTHONPATH=... + PYTHONDONTWRITEBYTECODE=1 + /usr/bin/python3 -c 'import packaging, packaging.markers, packaging.requirements, packaging.tags' + RPM_EC=0 ++ jobs -p + exit 0 %py3_check_import packaging, packaging.markers packaging.notachance, packaging.tags ... + /usr/bin/python3 -c 'import packaging, packaging.markers, packaging.notachance, packaging.tags' Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'packaging.notachance' error: Bad exit status from ... (%check)
2021-07-07 12:46:04 +00:00
# This only supports Python 3.5+ and will never work with Python 2.
# Hence, it has no Python version in the name.
%pycached() %{lua:
path = rpm.expand("%{?*}")
if (string.sub(path, "-3") ~= ".py") then
rpm.expand("%{error:%%pycached can only be used with paths explicitly ending with .py}")
else
print(path)
pyminor = path:match("/python3.(%d+)/") or "*"
dirname = path:match("(.*/)")
modulename = path:match(".*/([^/]+).py")
-- %%python3_cache_tag is not used here because this macro supports not-installed CPythons
print("\\n" .. dirname .. "__pycache__/" .. modulename .. ".cpython-3" .. pyminor .. "{,.opt-?}.pyc")
end
}
# This is intended for Python 3 only, hence also no Python version in the name.
%__pytest /usr/bin/pytest%(test %{python3_pkgversion} == 3 || echo -%{python3_version})
%pytest %{expand:\\\
CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
PATH="%{buildroot}%{_bindir}:$PATH"\\\
PYTHONPATH="${PYTHONPATH:-%{buildroot}%{python3_sitearch}:%{buildroot}%{python3_sitelib}}"\\\
PYTHONDONTWRITEBYTECODE=1\\\
%{?__pytest_addopts:PYTEST_ADDOPTS="${PYTEST_ADDOPTS:-} %{__pytest_addopts}"}\\\
%__pytest}