From e5429a7a486e470b514aed207457fb237d061598 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 | 7 ++++++- macros.python2 | 8 +++++++- macros.python3 | 7 ++++++- python-rpm-macros.spec | 5 ++++- tests/test_evals.py | 34 +++++++++++++++++++++++++--------- 5 files changed, 48 insertions(+), 13 deletions(-) diff --git a/macros.python b/macros.python index 7059a8e..fb0d061 100644 --- a/macros.python +++ b/macros.python @@ -19,7 +19,12 @@ # 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 - $pathfix -pni %{__python} -k%{?py_shebang_flags:a %py_shebang_flags}} + if [ -z "%{?py_shebang_flags}" ]; then + shebang_flags="-k" + else + shebang_flags="-ka%{py_shebang_flags}" + fi + $pathfix -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 2ebec9f..d4d4eda 100644 --- a/macros.python2 +++ b/macros.python2 @@ -7,7 +7,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 9e37a8a..323e675 100644 --- a/macros.python3 +++ b/macros.python3 @@ -17,7 +17,12 @@ # 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 - $pathfix -pni %{__python3} -k%{?py3_shebang_flags:a %py3_shebang_flags}} + if [ -z "%{?py3_shebang_flags}" ]; then + shebang_flags="-k" + else + shebang_flags="-ka%{py3_shebang_flags}" + fi + $pathfix -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 e2c7092..06a9a49 100644 --- a/python-rpm-macros.spec +++ b/python-rpm-macros.spec @@ -1,6 +1,6 @@ Name: python-rpm-macros Version: 3.9 -Release: 10%{?dist} +Release: 11%{?dist} Summary: The common Python RPM macros # macros and lua: MIT, compileall2.py: PSFv2 @@ -107,6 +107,9 @@ install -m 644 compileall2.py %{buildroot}%{_rpmconfigdir}/redhat/ %changelog +* Tue Dec 08 2020 Miro Hrončok - 3.9-11 +- Support defining %%py3_shebang_flags to %%nil + * Mon Sep 14 2020 Miro Hrončok - 3.9-10 - 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 62bd39c..1de3a7b 100644 --- a/tests/test_evals.py +++ b/tests/test_evals.py @@ -42,6 +42,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() + + @pytest.mark.parametrize('argument, result', [ ('a', 'a'), ('a-a', 'a-a'), @@ -252,22 +259,31 @@ def test_pypi_source_explicit_tilde(): def test_py3_shebang_fix(): cmd = rpm_eval('%py3_shebang_fix arg1 arg2 arg3')[-1].strip() - assert cmd == '$pathfix -pni /usr/bin/python3 -ka s arg1 arg2 arg3' + assert cmd == '$pathfix -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')[-1].strip() - assert cmd == '$pathfix -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)[-1].strip() - assert cmd == '$pathfix -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(): +@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 == '$pathfix -pni /usr/bin/pypy -ka s arg1 arg2 arg3' + assert cmd == '$pathfix -pni /usr/bin/pypy $shebang_flags arg1 arg2 arg3' def test_pycached_in_sitelib():