Allow to pass the %%py_import_check if no modules are detected

Add option '-M' to %%py_import_check
- if there are no arguments to the macro and -M option is set, the check
  will pass
- if there are arguments to the macro and they evaluate to the empty
  list of modules, and the -M option is set, the check will pass
This is only allowed to use in fully automated environment and cannot be
accepted in Fedora's official packages.
This commit is contained in:
Karolina Surma 2023-09-01 15:23:11 +02:00
commit 4e63af0532
6 changed files with 34 additions and 4 deletions

View file

@ -85,8 +85,10 @@ def read_modules_from_all_args(args):
modules = filter_top_level_modules_only(modules)
# Error when someone accidentally managed to filter out everything
# and it's not an automated environment where it's allowed
if len(modules) == 0:
raise ValueError('No modules to check were left')
if not args.no_modules:
raise ValueError('No modules to check were left')
return modules
@ -120,6 +122,11 @@ def argparser():
'-e', '--exclude', action='append',
help='Provide modules globs to be excluded from the check.',
)
parser.add_argument(
'-M', '--no-modules', action='store_true',
help='Don\'t fail the check even if no Python modules were detected.',
)
return parser
@ -158,6 +165,10 @@ def main(argv=None):
cli_args = argparser().parse_args(argv)
if not cli_args.modules and not cli_args.filename:
# This is a special case allowing to pass the check even when no modules are
# provided via CLI. There's no need to run the rest of the script
if cli_args.no_modules:
return
raise ValueError('No modules to check were provided')
modules = read_modules_from_all_args(cli_args)

View file

@ -71,10 +71,11 @@
# Respect the custom values of %%py_shebang_flags or set nothing if it's undefined.
# Filter and check import on only top-level modules using -t flag.
# Exclude unwanted modules by passing their globs to -e option.
# Use -M flag in automated environments to pass the check even if no modules are detected.
# Useful as a smoke test in %%check when running tests is not feasible.
# Use spaces or commas as separators if providing list directly.
# Use newlines as separators if providing list in a file.
%py_check_import(e:tf:) %{expand:\\\
%py_check_import(e:tf:M) %{expand:\\\
PATH="%{buildroot}%{_bindir}:$PATH"\\\
PYTHONPATH="${PYTHONPATH:-%{buildroot}%{python_sitearch}:%{buildroot}%{python_sitelib}}"\\\
_PYTHONSITE="%{buildroot}%{python_sitearch}:%{buildroot}%{python_sitelib}"\\\

View file

@ -69,10 +69,11 @@
# Respect the custom values of %%py3_shebang_flags or set nothing if it's undefined.
# Filter and check import on only top-level modules using -t flag.
# Exclude unwanted modules by passing their globs to -e option.
# Use -M flag in automated environments to pass the check even if no modules are detected.
# Useful as a smoke test in %%check when running tests is not feasible.
# Use spaces or commas as separators if providing list directly.
# Use newlines as separators if providing list in a file.
%py3_check_import(e:tf:) %{expand:\\\
%py3_check_import(e:tf:M) %{expand:\\\
PATH="%{buildroot}%{_bindir}:$PATH"\\\
PYTHONPATH="${PYTHONPATH:-%{buildroot}%{python3_sitearch}:%{buildroot}%{python3_sitelib}}"\\\
_PYTHONSITE="%{buildroot}%{python3_sitearch}:%{buildroot}%{python3_sitelib}"\\\

View file

@ -53,7 +53,7 @@ elseif posix.stat('macros.python-srpm') then
end
}
Version: %{__default_python3_version}
Release: 3%{?dist}
Release: 4%{?dist}
BuildArch: noarch
@ -163,6 +163,10 @@ grep -E '^#[^%%]*%%[^%%]' %{buildroot}%{rpmmacrodir}/macros.* && exit 1 || true
%changelog
* Fri Sep 01 2023 Karolina Surma <ksurma@redhat.com> - 3.12-4
- Add '-M' option to %%py_import_check that allows to pass the check
if no Python modules are detected
* Wed Aug 09 2023 Karolina Surma <ksurma@redhat.com> - 3.12-3
- Declare the license as an SPDX expression

View file

@ -830,6 +830,7 @@ def test_python3_sitearch_value_alternate_prefix(lib):
('six.quarter six.half,, SIX', 'six.quarter six.half,, SIX'),
('-f foo.txt six\nsix.half\nSIX', '-f foo.txt six six.half SIX'),
('six \\ -e six.half', 'six -e six.half'),
('-M', '-M'),
]
)
@pytest.mark.parametrize('__python3',

View file

@ -112,6 +112,18 @@ def test_main_raises_error_when_no_modules_provided():
modules_main([])
def test_main_no_error_when_no_modules_provided():
'''With -M it\'s possible to run the script when no modules are detected.'''
modules_main(['-M'])
def test_main_no_error_if_all_excluded():
'''With -M it\'s possible to run the script when all modules are excluded.'''
modules_main(['-M', 'foo', '-e', 'foo'])
def test_import_all_modules_does_not_import():
'''Ensure the files from /usr/lib/rpm/redhat cannot be imported and
checked for import'''