From 69b1b30d532a8c4320956b60aeef1af22845b11c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Thu, 16 Jul 2020 18:21:15 +0200 Subject: [PATCH] Make the unversioned %__python macro error See https://fedoraproject.org/wiki/Changes/PythonMacroError While doing it, make %python macros more consistent with %python3 macros, mostly wrt whitespace but also to use python -m pip over plain pip etc. One significant change is the removal of sleeps from python macros, this could affect packages that use python macros to build for Python 2 while also using python3 macros to build for Python 3. In reality, I consider that unlikely. The sleep in python2 macros stays. The --strip-file-prefix option was already removed from %pyX_install_wheel but we forgot to remove it from %py_install_wheel. --- macros.python | 7 ++----- macros.python-srpm | 14 +++----------- macros.python2 | 4 ++-- macros.python3 | 6 +++--- python-rpm-macros.spec | 7 ++++++- tests/test_evals.py | 30 ++++++++++++++++++++++++++++++ 6 files changed, 46 insertions(+), 22 deletions(-) diff --git a/macros.python b/macros.python index 713f746..2ecb49d 100644 --- a/macros.python +++ b/macros.python @@ -16,19 +16,16 @@ %py_build() %{expand:\\\ CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\ %{__python} %{py_setup} %{?py_setup_args} build --executable="%{__python} %{py_shbang_opts}" %{?*} - sleep 1 } %py_build_egg() %{expand:\\\ CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\ %{__python} %{py_setup} %{?py_setup_args} bdist_egg %{?*} - sleep 1 } %py_build_wheel() %{expand:\\\ CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\ %{__python} %{py_setup} %{?py_setup_args} bdist_wheel %{?*} - sleep 1 } %py_install() %{expand:\\\ @@ -39,12 +36,12 @@ %py_install_egg() %{expand:\\\ mkdir -p %{buildroot}%{python_sitelib} - easy_install -m --prefix %{buildroot}%{_prefix} -Z dist/*-py%{python_version}.egg %{?*} + %{__python} -m easy_install -m --prefix %{buildroot}%{_prefix} -Z dist/*-py%{python_version}.egg %{?*} rm -rfv %{buildroot}%{_bindir}/__pycache__ } %py_install_wheel() %{expand:\\\ - pip install -I dist/%{1} --root %{buildroot} --strip-file-prefix %{buildroot} --no-deps + %{__python} -m pip install -I dist/%{1} --root %{buildroot} --no-deps rm -rfv %{buildroot}%{_bindir}/__pycache__ for distinfo in %{buildroot}%{python_sitelib}/*.dist-info %{buildroot}%{python_sitearch}/*.dist-info; do if [ -f ${distinfo}/direct_url.json ]; then diff --git a/macros.python-srpm b/macros.python-srpm index c0d31b0..fa9f06a 100644 --- a/macros.python-srpm +++ b/macros.python-srpm @@ -11,19 +11,11 @@ %python2 %__python2 %python3 %__python3 -# For backwards compatibility only -# See the comments in https://src.fedoraproject.org/rpms/python-rpm-macros/pull-request/22 -%__python /usr/bin/python +# See https://fedoraproject.org/wiki/Changes/PythonMacroError +%__python %{error:attempt to use unversioned python, define %%__python to %{__python2} or %{__python3} explicitly} # Users can use %%python only if they redefined %%__python (e.g. to %%__python3) -%python() %{lua:\ - __python = rpm.expand("%__python")\ - if __python == "/usr/bin/python" then\ - rpm.expand("%{error:Cannot use %%python if %%__python wasn't redefined to something other than /usr/bin/python.}")\ - else\ - print(__python)\ - end\ -} +%python %__python # There are multiple Python 3 versions packaged, but only one can be the "main" version # That means that it owns the "python3" namespace: diff --git a/macros.python2 b/macros.python2 index 0e4b0cb..df8e804 100644 --- a/macros.python2 +++ b/macros.python2 @@ -41,12 +41,12 @@ %py2_install_egg() %{expand:\\\ mkdir -p %{buildroot}%{python2_sitelib} - easy_install-%{python2_version} -m --prefix %{buildroot}%{_prefix} -Z dist/*-py%{python2_version}.egg %{?*} + %{__python2} -m easy_install -m --prefix %{buildroot}%{_prefix} -Z dist/*-py%{python2_version}.egg %{?*} rm -rfv %{buildroot}%{_bindir}/__pycache__ } %py2_install_wheel() %{expand:\\\ - pip%{python2_version} install -I dist/%{1} --root %{buildroot} --no-deps + %{__python2} -m pip install -I dist/%{1} --root %{buildroot} --no-deps rm -rfv %{buildroot}%{_bindir}/__pycache__ for distinfo in %{buildroot}%{python2_sitelib}/*.dist-info %{buildroot}%{python2_sitearch}/*.dist-info; do if [ -f ${distinfo}/direct_url.json ]; then diff --git a/macros.python3 b/macros.python3 index 765e56b..691e035 100644 --- a/macros.python3 +++ b/macros.python3 @@ -30,17 +30,17 @@ %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__ + rm -rfv %{buildroot}%{_bindir}/__pycache__ } %py3_install_egg() %{expand:\\\ mkdir -p %{buildroot}%{python3_sitelib} - easy_install-%{python3_version} -m --prefix %{buildroot}%{_prefix} -Z dist/*-py%{python3_version}.egg %{?*} + %{__python3} -m easy_install -m --prefix %{buildroot}%{_prefix} -Z dist/*-py%{python3_version}.egg %{?*} rm -rfv %{buildroot}%{_bindir}/__pycache__ } %py3_install_wheel() %{expand:\\\ - pip%{python3_version} install -I dist/%{1} --root %{buildroot} --no-deps + %{__python3} -m pip install -I dist/%{1} --root %{buildroot} --no-deps 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 diff --git a/python-rpm-macros.spec b/python-rpm-macros.spec index eb381a8..5e5dac3 100644 --- a/python-rpm-macros.spec +++ b/python-rpm-macros.spec @@ -1,6 +1,6 @@ Name: python-rpm-macros Version: 3.9 -Release: 5%{?dist} +Release: 6%{?dist} Summary: The common Python RPM macros # macros and lua: MIT, compileall2.py: PSFv2 @@ -107,6 +107,11 @@ install -m 644 compileall2.py %{buildroot}%{_rpmconfigdir}/redhat/ %changelog +* Thu Jul 16 2020 Miro Hrončok - 3.9-6 +- Make the unversioned %%__python macro error +- https://fedoraproject.org/wiki/Changes/PythonMacroError +- Make %%python macros more consistent with %%python3 macros + * Wed Jul 08 2020 Miro Hrončok - 3.9-5 - Introduce %%python_extras_subpkg - Adapt %%py_dist_name to keep square brackets diff --git a/tests/test_evals.py b/tests/test_evals.py index ce38793..917b355 100644 --- a/tests/test_evals.py +++ b/tests/test_evals.py @@ -362,3 +362,33 @@ def test_python_extras_subpkg_F(): It contains no code, just makes sure the dependencies are installed. """).lstrip().splitlines() assert lines == expected + + +unversioned_macros = pytest.mark.parametrize('macro', [ + '%__python', + '%python', + '%python_version', + '%python_version_nodots', + '%python_sitelib', + '%python_sitearch', + '%py_shebang_fix', + '%py_build', + '%py_build_egg', + '%py_build_wheel', + '%py_install', + '%py_install_egg', + '%py_install_wheel', +]) + + +@unversioned_macros +def test_unversioned_python_errors(macro): + lines = rpm_eval(macro, fails=True) + assert lines[0] == ('error: attempt to use unversioned python, ' + 'define %__python to /usr/bin/python2 or /usr/bin/python3 explicitly') + + +@unversioned_macros +def test_unversioned_python_works_when_defined(macro): + macro3 = macro.replace('python', 'python3').replace('py_', 'py3_') + assert rpm_eval(macro, __python='/usr/bin/python3') == rpm_eval(macro3)