From 4085ef49f2f7759d50835f2139c4c010d3aedb83 Mon Sep 17 00:00:00 2001 From: Miro Hrončok Date: Jul 19 2022 09:42:56 +0000 Subject: Define %python3_cache_tag / %python_cache_tag, e.g. cpython-311 When reviewing https://src.fedoraproject.org/rpms/pyproject-rpm-macros/pull-request/291 we have discovered that there is no macronized way to get this part of some paths and that packagers need to hardcode it as cpython-%{python3_version_nodots}. This way, we have a standardized macro packagers (and other macros) can use. --- diff --git a/macros.python b/macros.python index cfe89b6..f060340 100644 --- a/macros.python +++ b/macros.python @@ -10,6 +10,7 @@ %python_platform %(RPM_BUILD_ROOT= %{__python} -Esc "import sysconfig; print(sysconfig.get_platform())") %python_platform_triplet %(RPM_BUILD_ROOT= %{__python} -Esc "import sysconfig; print(sysconfig.get_config_var('MULTIARCH'))") %python_ext_suffix %(RPM_BUILD_ROOT= %{__python} -Esc "import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))") +%python_cache_tag %(RPM_BUILD_ROOT= %{__python} -Esc "import sys; print(sys.implementation.cache_tag)") %py_setup setup.py %py_shbang_opts -s diff --git a/macros.python3 b/macros.python3 index 3977e7a..4f2b2d2 100644 --- a/macros.python3 +++ b/macros.python3 @@ -8,6 +8,7 @@ %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)") %py3dir %{_builddir}/python3-%{name}-%{version}-%{release} %py3_shbang_opts -s @@ -103,6 +104,7 @@ 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 } diff --git a/python-rpm-macros.spec b/python-rpm-macros.spec index 126fdca..15ac23b 100644 --- a/python-rpm-macros.spec +++ b/python-rpm-macros.spec @@ -49,7 +49,7 @@ elseif posix.stat('macros.python-srpm') then end } Version: %{__default_python3_version} -Release: 1%{?dist} +Release: 2%{?dist} BuildArch: noarch @@ -151,6 +151,9 @@ grep -E '^#[^%%]*%%[^%%]' %{buildroot}%{rpmmacrodir}/macros.* && exit 1 || true %changelog +* Mon Jun 20 2022 Miro Hrončok - 3.11-2 +- Define %%python3_cache_tag / %%python_cache_tag, e.g. cpython-311 + * Mon Jun 13 2022 Tomáš Hrnčiar - 3.11-1 - Update main Python to Python 3.11 - https://fedoraproject.org/wiki/Changes/Python3.11 diff --git a/tests/test_evals.py b/tests/test_evals.py index 2489644..7a5c66f 100644 --- a/tests/test_evals.py +++ b/tests/test_evals.py @@ -72,8 +72,17 @@ def get_alt_x_y(): raise ValueError(f"${env_name} must be X.Y") return alternate_python_version -# We don't use the decorator, to be able to call the function itselef + +def get_alt_xy(): + """ + Same as get_alt_x_y() but without a dot + """ + return get_alt_x_y().replace(".", "") + + +# We don't use decorators, to be able to call the functions directly alt_x_y = pytest.fixture(scope="session")(get_alt_x_y) +alt_xy = pytest.fixture(scope="session")(get_alt_xy) def shell_stdout(script): @@ -638,6 +647,7 @@ unversioned_macros = pytest.mark.parametrize('macro', [ '%python_platform', '%python_platform_triplet', '%python_ext_suffix', + '%python_cache_tag', '%py_shebang_fix', '%py_build', '%py_build_egg', @@ -676,6 +686,18 @@ def test_ext_suffix(): assert rpm_eval("%python3_ext_suffix") == [f".cpython-{XY}-x86_64-linux-gnu.so"] +def test_cache_tag(): + assert rpm_eval("%python3_cache_tag") == [f"cpython-{XY}"] + + +def test_cache_tag_alternate_python(alt_x_y, alt_xy): + assert rpm_eval("%python_cache_tag", __python=f"/usr/bin/python{alt_x_y}") == [f"cpython-{alt_xy}"] + + +def test_cache_tag_alternate_python3(alt_x_y, alt_xy): + assert rpm_eval("%python3_cache_tag", __python3=f"/usr/bin/python{alt_x_y}") == [f"cpython-{alt_xy}"] + + def test_python_sitelib_value_python3(): macro = '%python_sitelib' assert rpm_eval(macro, __python='%__python3') == [f'/usr/lib/python{X_Y}/site-packages']