%py(3)_check_import: Process .pth files in site(arch|lib)

Related: rhbz#1950291
This commit is contained in:
Miro Hrončok 2021-11-02 15:05:11 +00:00
parent b2a5690d6b
commit e247e4dbae
6 changed files with 52 additions and 0 deletions

View File

@ -3,7 +3,9 @@
import argparse import argparse
import importlib import importlib
import fnmatch import fnmatch
import os
import re import re
import site
import sys import sys
from contextlib import contextmanager from contextlib import contextmanager
@ -139,6 +141,18 @@ def remove_unwanteds_from_sys_path():
sys.path = old_sys_path sys.path = old_sys_path
def addsitedirs_from_environ():
'''Load directories from the _PYTHONSITE environment variable (separated by :)
and load the ones already present in sys.path via site.addsitedir()
to handle .pth files in them.
This is needed to properly import old-style namespace packages with nspkg.pth files.
See https://bugzilla.redhat.com/2018551 for a more detailed rationale.'''
for path in os.getenv('_PYTHONSITE', '').split(':'):
if path in sys.path:
site.addsitedir(path)
def main(argv=None): def main(argv=None):
cli_args = argparser().parse_args(argv) cli_args = argparser().parse_args(argv)
@ -149,6 +163,7 @@ def main(argv=None):
modules = read_modules_from_all_args(cli_args) modules = read_modules_from_all_args(cli_args)
with remove_unwanteds_from_sys_path(): with remove_unwanteds_from_sys_path():
addsitedirs_from_environ()
import_modules(modules) import_modules(modules)

View File

@ -79,6 +79,7 @@
%py_check_import(e:tf:) %{expand:\\\ %py_check_import(e:tf:) %{expand:\\\
PATH="%{buildroot}%{_bindir}:$PATH"\\\ PATH="%{buildroot}%{_bindir}:$PATH"\\\
PYTHONPATH="${PYTHONPATH:-%{buildroot}%{python_sitearch}:%{buildroot}%{python_sitelib}}"\\\ PYTHONPATH="${PYTHONPATH:-%{buildroot}%{python_sitearch}:%{buildroot}%{python_sitelib}}"\\\
_PYTHONSITE="%{buildroot}%{python_sitearch}:%{buildroot}%{python_sitelib}"\\\
PYTHONDONTWRITEBYTECODE=1\\\ PYTHONDONTWRITEBYTECODE=1\\\
%{lua: %{lua:
local command = "%{__python} " local command = "%{__python} "

View File

@ -77,6 +77,7 @@
%py3_check_import(e:tf:) %{expand:\\\ %py3_check_import(e:tf:) %{expand:\\\
PATH="%{buildroot}%{_bindir}:$PATH"\\\ PATH="%{buildroot}%{_bindir}:$PATH"\\\
PYTHONPATH="${PYTHONPATH:-%{buildroot}%{python3_sitearch}:%{buildroot}%{python3_sitelib}}"\\\ PYTHONPATH="${PYTHONPATH:-%{buildroot}%{python3_sitearch}:%{buildroot}%{python3_sitelib}}"\\\
_PYTHONSITE="%{buildroot}%{python3_sitearch}:%{buildroot}%{python3_sitelib}"\\\
PYTHONDONTWRITEBYTECODE=1\\\ PYTHONDONTWRITEBYTECODE=1\\\
%{lua: %{lua:
local command = "%{__python3} " local command = "%{__python3} "

View File

@ -104,6 +104,7 @@ install -m 644 import_all_modules.py %{buildroot}%{_rpmconfigdir}/redhat/
* Mon Nov 01 2021 Karolina Surma <ksurma@redhat.com> - 3.9-46 * Mon Nov 01 2021 Karolina Surma <ksurma@redhat.com> - 3.9-46
- Fix multiline arguments processing for %%py_check_import - Fix multiline arguments processing for %%py_check_import
- Fix %%py_shebang_flags handling within %%py_check_import - Fix %%py_shebang_flags handling within %%py_check_import
- Process .pth files in buildroot's sitedirs in %%py_check_import
* Mon Oct 25 2021 Karolina Surma <ksurma@redhat.com> - 3.9-45 * Mon Oct 25 2021 Karolina Surma <ksurma@redhat.com> - 3.9-45
- Introduce -f (read from file) option to %%py{3}_check_import - Introduce -f (read from file) option to %%py{3}_check_import

View File

@ -696,6 +696,7 @@ def test_py3_check_import(args, expected_args, __python3, lib):
expected = textwrap.dedent(fr""" expected = textwrap.dedent(fr"""
PATH="BUILDROOT/usr/bin:$PATH" PATH="BUILDROOT/usr/bin:$PATH"
PYTHONPATH="${{PYTHONPATH:-BUILDROOT/usr/{lib}/python{x_y}/site-packages:BUILDROOT/usr/lib/python{x_y}/site-packages}}" PYTHONPATH="${{PYTHONPATH:-BUILDROOT/usr/{lib}/python{x_y}/site-packages:BUILDROOT/usr/lib/python{x_y}/site-packages}}"
_PYTHONSITE="BUILDROOT/usr/{lib}/python{x_y}/site-packages:BUILDROOT/usr/lib/python{x_y}/site-packages"
PYTHONDONTWRITEBYTECODE=1 PYTHONDONTWRITEBYTECODE=1
{__python3 or '/usr/bin/python3'} -s RPMCONFIGDIR/redhat/import_all_modules.py {expected_args} {__python3 or '/usr/bin/python3'} -s RPMCONFIGDIR/redhat/import_all_modules.py {expected_args}
""") """)

View File

@ -391,3 +391,36 @@ def test_module_list_from_relative_path(tmp_path, monkeypatch):
]) ])
assert 'wave' in sys.modules assert 'wave' in sys.modules
@pytest.mark.parametrize('arch_in_path', [True, False])
def test_pth_files_are_read_from__PYTHONSITE(arch_in_path, tmp_path, monkeypatch, capsys):
sitearch = tmp_path / 'lib64'
sitearch.mkdir()
sitelib = tmp_path / 'lib'
sitelib.mkdir()
for where, word in (sitearch, "ARCH"), (sitelib, "LIB"), (sitelib, "MOD"):
module = where / f'print{word}.py'
module.write_text(f'print("{word}")')
pth_sitearch = sitearch / 'ARCH.pth'
pth_sitearch.write_text('import printARCH\n')
pth_sitelib = sitelib / 'LIB.pth'
pth_sitelib.write_text('import printLIB\n')
if arch_in_path:
sys.path.append(str(sitearch))
sys.path.append(str(sitelib))
# we always add sitearch to _PYTHONSITE
# but when not in sys.path, it should not be processed for .pth files
monkeypatch.setenv('_PYTHONSITE', f'{sitearch}:{sitelib}')
modules_main(['printMOD'])
out, err = capsys.readouterr()
if arch_in_path:
assert out == 'ARCH\nLIB\nMOD\n'
else:
assert out == 'LIB\nMOD\n'