From 8b79a441adcb77434f0d4c0d49bb559e313df5e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Mon, 18 May 2020 18:58:39 +0200 Subject: [PATCH] Implement %pyX_shebang_fix See https://lists.fedoraproject.org/archives/list/python-devel@lists.fedoraproject.org/thread/UGCMDDG3S32U7JJK36OEZNHLUVQQRG3M/ --- macros.python | 2 ++ macros.python2 | 2 ++ macros.python3 | 2 ++ python-rpm-macros.spec | 1 + tests/test_evals.py | 25 ++++++++++++++++++++++++- tests/tests.yml | 1 + 6 files changed, 32 insertions(+), 1 deletion(-) diff --git a/macros.python b/macros.python index 72bb64b..20cde47 100644 --- a/macros.python +++ b/macros.python @@ -8,6 +8,8 @@ %py_setup setup.py %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}} # 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 4442496..3426929 100644 --- a/macros.python2 +++ b/macros.python2 @@ -5,6 +5,8 @@ %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}} # 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 7c3b26c..efccee7 100644 --- a/macros.python3 +++ b/macros.python3 @@ -7,6 +7,8 @@ %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}} # 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 0bf9231..4c8abd4 100644 --- a/python-rpm-macros.spec +++ b/python-rpm-macros.spec @@ -83,6 +83,7 @@ install -m 644 %{SOURCE5} \ * Wed May 20 2020 Miro Hrončok - 3-56 - Implement %%py_provides - Implement %%pytest +- Implement %%pyX_shebang_fix - Strip tildes from %%version in %%pypi_source by default * Tue Apr 28 2020 Miro Hrončok - 3-55 diff --git a/tests/test_evals.py b/tests/test_evals.py index a07c676..b63e29e 100644 --- a/tests/test_evals.py +++ b/tests/test_evals.py @@ -5,7 +5,10 @@ import sys def rpm_eval(expression, **kwargs): cmd = ['rpmbuild'] for var, value in kwargs.items(): - cmd += ['--define', f'{var} {value}'] + if value is None: + cmd += ['--undefine', var] + else: + cmd += ['--define', f'{var} {value}'] cmd += ['--eval', expression] cp = subprocess.run(cmd, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -145,3 +148,23 @@ def test_pypi_source_explicit_tilde(): url = rpm_eval('%pypi_source foo 6~6', name='python-foo', pypi_name='foo', version='6')[0] assert url == 'https://files.pythonhosted.org/packages/source/f/foo/foo-6~6.tar.gz' + + +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' + + +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_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_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' diff --git a/tests/tests.yml b/tests/tests.yml index 56221e3..2411fa8 100644 --- a/tests/tests.yml +++ b/tests/tests.yml @@ -19,4 +19,5 @@ required_packages: - rpm-build - python-rpm-macros + - python3-rpm-macros - python3-pytest