diff --git a/import_all_modules.py b/import_all_modules.py index 3930236..ab6eaea 100644 --- a/import_all_modules.py +++ b/import_all_modules.py @@ -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) diff --git a/macros.python b/macros.python index 4568614..f3ae76d 100644 --- a/macros.python +++ b/macros.python @@ -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}"\\\ diff --git a/macros.python3 b/macros.python3 index 0e4678e..53855b3 100644 --- a/macros.python3 +++ b/macros.python3 @@ -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}"\\\ diff --git a/python-rpm-macros.spec b/python-rpm-macros.spec index 68415be..d93d69a 100644 --- a/python-rpm-macros.spec +++ b/python-rpm-macros.spec @@ -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 - 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 - 3.12-3 - Declare the license as an SPDX expression diff --git a/tests/test_evals.py b/tests/test_evals.py index 294faaa..c5f3317 100644 --- a/tests/test_evals.py +++ b/tests/test_evals.py @@ -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', diff --git a/tests/test_import_all_modules.py b/tests/test_import_all_modules.py index 71feeff..22dc968 100644 --- a/tests/test_import_all_modules.py +++ b/tests/test_import_all_modules.py @@ -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'''