From 601d83d35aecbbf616f134b25cb3fa45cf5b118d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Tue, 8 Dec 2020 12:10:29 +0100 Subject: [PATCH] Support defining %py3_shebang_flags to %nil --- macros.python | 8 +++++++- macros.python2 | 8 +++++++- macros.python3 | 8 +++++++- python-rpm-macros.spec | 5 ++++- tests/test_evals.py | 38 +++++++++++++++++++++++++++----------- 5 files changed, 52 insertions(+), 15 deletions(-) diff --git a/macros.python b/macros.python index d5f380e..ed0ec66 100644 --- a/macros.python +++ b/macros.python @@ -12,7 +12,13 @@ %py_shbang_opts -s %py_shbang_opts_nodash %(opts=%{py_shbang_opts}; echo ${opts#-}) %py_shebang_flags %(opts=%{py_shbang_opts}; echo ${opts#-}) -%py_shebang_fix %{expand:/usr/bin/pathfix.py -pni %{__python} -k%{?py_shebang_flags:a %py_shebang_flags}} +%py_shebang_fix %{expand:\\\ + if [ -z "%{?py_shebang_flags}" ]; then + shebang_flags="-k" + else + shebang_flags="-ka%{py_shebang_flags}" + fi + /usr/bin/pathfix.py -pni %{__python} $shebang_flags} # Use the slashes after expand so that the command starts on the same line as # the macro diff --git a/macros.python2 b/macros.python2 index 609f0bc..0bc62fc 100644 --- a/macros.python2 +++ b/macros.python2 @@ -6,7 +6,13 @@ %py2_shbang_opts -s %py2_shbang_opts_nodash %(opts=%{py2_shbang_opts}; echo ${opts#-}) %py2_shebang_flags %(opts=%{py2_shbang_opts}; echo ${opts#-}) -%py2_shebang_fix %{expand:/usr/bin/pathfix.py -pni %{__python2} -k%{?py2_shebang_flags:a %py2_shebang_flags}} +%py2_shebang_fix %{expand:\\\ + if [ -z "%{?py_shebang_flags}" ]; then + shebang_flags="-k" + else + shebang_flags="-ka%{py2_shebang_flags}" + fi + /usr/bin/pathfix.py -pni %{__python2} $shebang_flags} # Use the slashes after expand so that the command starts on the same line as # the macro diff --git a/macros.python3 b/macros.python3 index dbd48e8..b39589c 100644 --- a/macros.python3 +++ b/macros.python3 @@ -10,7 +10,13 @@ %py3_shbang_opts -s %py3_shbang_opts_nodash %(opts=%{py3_shbang_opts}; echo ${opts#-}) %py3_shebang_flags %(opts=%{py3_shbang_opts}; echo ${opts#-}) -%py3_shebang_fix %{expand:/usr/bin/pathfix.py -pni %{__python3} -k%{?py3_shebang_flags:a %py3_shebang_flags}} +%py3_shebang_fix %{expand:\\\ + if [ -z "%{?py3_shebang_flags}" ]; then + shebang_flags="-k" + else + shebang_flags="-ka%{py3_shebang_flags}" + fi + /usr/bin/pathfix.py -pni %{__python3} $shebang_flags} # Use the slashes after expand so that the command starts on the same line as # the macro diff --git a/python-rpm-macros.spec b/python-rpm-macros.spec index 8c90167..eb7e7e5 100644 --- a/python-rpm-macros.spec +++ b/python-rpm-macros.spec @@ -1,6 +1,6 @@ Name: python-rpm-macros Version: 3 -Release: 59%{?dist} +Release: 60%{?dist} Summary: The unversioned Python RPM macros # macros: MIT, compileall2.py: PSFv2 @@ -80,6 +80,9 @@ install -m 644 %{SOURCE5} \ %changelog +* Tue Dec 08 2020 Miro Hrončok - 3-60 +- Support defining %%py3_shebang_flags to %%nil + * Thu Sep 24 2020 Miro Hrončok - 3-59 - Add %%python3_platform_triplet and %%python3_ext_suffix - https://fedoraproject.org/wiki/Changes/Python_Upstream_Architecture_Names diff --git a/tests/test_evals.py b/tests/test_evals.py index 69c08db..0d72220 100644 --- a/tests/test_evals.py +++ b/tests/test_evals.py @@ -26,6 +26,13 @@ def rpm_eval(expression, fails=False, **kwargs): return cp.stdout.strip().splitlines() +def shell_stdout(script): + return subprocess.check_output(script, + env={**os.environ, 'LANG': 'C.utf-8'}, + text=True, + shell=True).rstrip() + + def test_python_provide_python(): assert rpm_eval('%python_provide python-foo') == [] @@ -161,23 +168,32 @@ def test_pypi_source_explicit_tilde(): def test_py3_shebang_fix(): - cmd = rpm_eval('%py3_shebang_fix arg1 arg2 arg3')[0] - assert cmd == '/usr/bin/pathfix.py -pni /usr/bin/python3 -ka s arg1 arg2 arg3' + cmd = rpm_eval('%py3_shebang_fix arg1 arg2 arg3')[-1].strip() + assert cmd == '/usr/bin/pathfix.py -pni /usr/bin/python3 $shebang_flags arg1 arg2 arg3' -def test_py3_shebang_fix_custom_flags(): - cmd = rpm_eval('%py3_shebang_fix arg1 arg2 arg3', py3_shebang_flags='Es')[0] - assert cmd == '/usr/bin/pathfix.py -pni /usr/bin/python3 -ka Es arg1 arg2 arg3' +def test_py3_shebang_fix_default_shebang_flags(): + lines = rpm_eval('%py3_shebang_fix arg1 arg2') + lines[-1] = 'echo $shebang_flags' + assert shell_stdout('\n'.join(lines)) == '-kas' -def test_py3_shebang_fix_empty_flags(): - cmd = rpm_eval('%py3_shebang_fix arg1 arg2 arg3', py3_shebang_flags=None)[0] - assert cmd == '/usr/bin/pathfix.py -pni /usr/bin/python3 -k arg1 arg2 arg3' +def test_py3_shebang_fix_custom_shebang_flags(): + lines = rpm_eval('%py3_shebang_fix arg1 arg2', py3_shebang_flags='Es') + lines[-1] = 'echo $shebang_flags' + assert shell_stdout('\n'.join(lines)) == '-kaEs' -def test_py_shebang_fix_custom(): - cmd = rpm_eval('%py_shebang_fix arg1 arg2 arg3', __python='/usr/bin/pypy')[0] - assert cmd == '/usr/bin/pathfix.py -pni /usr/bin/pypy -ka s arg1 arg2 arg3' +@pytest.mark.parametrize('flags', [None, '%{nil}']) +def test_py3_shebang_fix_no_shebang_flags(flags): + lines = rpm_eval('%py3_shebang_fix arg1 arg2', py3_shebang_flags=flags) + lines[-1] = 'echo $shebang_flags' + assert shell_stdout('\n'.join(lines)) == '-k' + + +def test_py_shebang_fix_custom_python(): + cmd = rpm_eval('%py_shebang_fix arg1 arg2 arg3', __python='/usr/bin/pypy')[-1].strip() + assert cmd == '/usr/bin/pathfix.py -pni /usr/bin/pypy $shebang_flags arg1 arg2 arg3' def test_pycached_in_sitelib():