From 3ec4e4ea2d8ce2855e06451b8a84b0179926ec01 Mon Sep 17 00:00:00 2001 From: Martin Liska Date: Tue, 3 Nov 2020 11:27:45 +0100 Subject: [PATCH] Automatically load rpmlintrc files. Load them from /home/abuild/rpmbuild/SOURCES and allow loading of multiple files (for now). rpmlint-mini/rpmlint-mini.config contains: ``` configs += glob.glob("/home/abuild/rpmbuild/SOURCES/*rpmlintrc") configs += glob.glob("/usr/src/packages/SOURCES/*rpmlintrc") ``` This is temporary hack for testing purpose. Tests in test_lint shared options_preset which was modified. --- rpmlint/cli.py | 4 ++++ rpmlint/lint.py | 54 +++++++++++++++++++++++++++-------------------- test/test_lint.py | 50 ++++++++++++++++++++++++------------------- 3 files changed, 63 insertions(+), 45 deletions(-) diff --git a/rpmlint/cli.py b/rpmlint/cli.py index 04e0f2bee..7506f3f92 100644 --- a/rpmlint/cli.py +++ b/rpmlint/cli.py @@ -105,6 +105,10 @@ def process_lint_args(argv): if not options.rpmlintrc.exists(): print_warning(f"User specified rpmlintrc '{options.rpmlintrc}' does not exist") exit(2) + # make it a list + options.rpmlintrc = [options.rpmlintrc] + else: + options.rpmlintrc = [] # validate all the rpmlfile options to be either file or folder f_path = [] invalid_path = False diff --git a/rpmlint/lint.py b/rpmlint/lint.py index 14d7c8720..34c12aad4 100644 --- a/rpmlint/lint.py +++ b/rpmlint/lint.py @@ -1,6 +1,8 @@ import cProfile import importlib import operator +import os +from pathlib import Path from pstats import Stats import sys from tempfile import gettempdir @@ -156,31 +158,36 @@ def _load_installed_rpms(self, packages): print_warning(f'(none): E: there is no installed rpm "{name}".') return existing_packages + def _find_rpmlintrc_files(self, path): + rpmlintrcs = [] + rpmlintrcs += sorted(path.glob('*.rpmlintrc')) + rpmlintrcs += sorted(path.glob('*-rpmlintrc')) + return rpmlintrcs + def _load_rpmlintrc(self): """ Load rpmlintrc from argument or load up from folder """ - if self.options['rpmlintrc']: - for rcfile in self.options['rpmlintrc']: - self.config.load_rpmlintrc(rcfile) - else: - # load only from the same folder specname.rpmlintrc or specname-rpmlintrc - # do this only in a case where there is one folder parameter or one file - # to avoid multiple folders handling - rpmlintrc = [] - if not len(self.options['rpmfile']) == 1: - return - pkg = self.options['rpmfile'][0] - if pkg.is_file(): - pkg = pkg.parent - rpmlintrc += sorted(pkg.glob('*.rpmlintrc')) - rpmlintrc += sorted(pkg.glob('*-rpmlintrc')) - if len(rpmlintrc) > 1: - # multiple rpmlintrcs are highly undesirable - print_warning('There are multiple items to be loaded for rpmlintrc, ignoring them: {}.'.format(' '.join(map(str, rpmlintrc)))) - elif len(rpmlintrc) == 1: - self.options['rpmlintrc'] = rpmlintrc[0] - self.config.load_rpmlintrc(rpmlintrc[0]) + if not self.options['rpmlintrc']: + # Skip auto-loading when running under PYTEST + if not os.environ.get('PYTEST_XDIST_TESTRUNUID'): + # first load SUSE-specific locations + self.options['rpmlintrc'] += self._find_rpmlintrc_files(Path('/home/abuild/rpmbuild/SOURCES')) + self.options['rpmlintrc'] += self._find_rpmlintrc_files(Path('/usr/src/packages/SOURCES/')) + if not self.options['rpmlintrc'] and len(self.options['rpmfile']) == 1: + # load only from the same folder specname.rpmlintrc or specname-rpmlintrc + # do this only in a case where there is one folder parameter or one file + # to avoid multiple folders handling + pkg = self.options['rpmfile'][0] + if pkg.is_file(): + pkg = pkg.parent + self.options['rpmlintrc'] += self._find_rpmlintrc_files(pkg) + + if len(self.options['rpmlintrc']) > 1: + # multiple rpmlintrcs are highly undesirable + print_warning('There are multiple items to be loaded: {}.'.format(' '.join(map(str, self.options['rpmlintrc'])))) + for rcfile in self.options['rpmlintrc']: + self.config.load_rpmlintrc(rcfile) def _print_header(self): """ @@ -194,8 +201,9 @@ def _print_header(self): for config in self.config.conf_files: print(f' {config}') if self.options['rpmlintrc']: - rpmlintrc = self.options['rpmlintrc'] - print(f'rpmlintrc: {rpmlintrc}') + print('rpmlintrc:') + for rcfile in self.options['rpmlintrc']: + print(f' {rcfile}') no_checks = len(self.config.configuration['Checks']) no_pkgs = len(self.options['installed']) + len(self.options['rpmfile']) print(f'{Color.Bold}checks: {no_checks}, packages: {no_pkgs}{Color.Reset}') diff --git a/test/test_lint.py b/test/test_lint.py index dafcca5ae..27ee48996 100644 --- a/test/test_lint.py +++ b/test/test_lint.py @@ -1,3 +1,4 @@ +import copy from pathlib import Path import pytest @@ -19,7 +20,7 @@ 'print_config': False, 'explain': '', 'rpmfile': '', - 'rpmlintrc': False, + 'rpmlintrc': [], 'installed': '', 'time_report': False, 'profile': False @@ -57,6 +58,11 @@ ] +def get_options(additional_options): + options = copy.deepcopy(options_preset) + return {**options, **additional_options} + + def _remove_except_zip(dictionary): """ In order to not lie in coverage redux the test run on the @@ -76,7 +82,7 @@ def test_configoutput(capsys): additional_options = { 'print_config': True, } - options = {**options_preset, **additional_options} + options = get_options(additional_options) linter = Lint(options) linter.run() out, err = capsys.readouterr() @@ -91,7 +97,7 @@ def test_explain_unknown(capsys): additional_options = { 'explain': message, } - options = {**options_preset, **additional_options} + options = get_options(additional_options) linter = Lint(options) linter.run() out, err = capsys.readouterr() @@ -104,7 +110,7 @@ def test_explain_known(capsys): additional_options = { 'explain': message, } - options = {**options_preset, **additional_options} + options = get_options(additional_options) linter = Lint(options) linter.run() out, err = capsys.readouterr() @@ -118,7 +124,7 @@ def test_explain_with_unknown(capsys): additional_options = { 'explain': message, } - options = {**options_preset, **additional_options} + options = get_options(additional_options) linter = Lint(options) linter.run() out, err = capsys.readouterr() @@ -139,7 +145,7 @@ def test_explain_no_binary_from_cfg(capsys): 'config': [testpath() / 'configs/descriptions.config'], 'explain': ['no-binary'] } - options = {**options_preset, **additional_options} + options = get_options(additional_options) linter = Lint(options) linter.run() out, err = capsys.readouterr() @@ -163,7 +169,7 @@ def test_explain_non_standard_dir_from_cfg(capsys): 'config': [testpath() / 'configs/descriptions.config'], 'explain': ['non-standard-dir-in-usr'] } - options = {**options_preset, **additional_options} + options = get_options(additional_options) linter = Lint(options) linter.run() out, err = capsys.readouterr() @@ -189,7 +195,7 @@ def test_descriptions_from_config(capsys, packages): 'rpmfile': [packages] } options_preset['verbose'] = True - options = {**options_preset, **additional_options} + options = get_options(additional_options) linter = Lint(options) linter.run() out, err = capsys.readouterr() @@ -209,7 +215,7 @@ def test_run_single(capsys, packages): additional_options = { 'rpmfile': [packages], } - options = {**options_preset, **additional_options} + options = get_options(additional_options) linter = Lint(options) linter.checks = _remove_except_zip(linter.checks) linter.run() @@ -226,7 +232,7 @@ def test_run_installed(capsys, packages): 'rpmfile': [packages], 'installed': ['binutils', 'rpm'], } - options = {**options_preset, **additional_options} + options = get_options(additional_options) linter = Lint(options) linter.checks = _remove_except_zip(linter.checks) linter.run() @@ -244,7 +250,7 @@ def test_run_strict(capsys, packages): 'rpmfile': [packages], 'strict': True, } - options = {**options_preset, **additional_options} + options = get_options(additional_options) linter = Lint(options) linter.checks = _remove_except_zip(linter.checks) linter.run() @@ -260,7 +266,7 @@ def test_run_installed_not_present(capsys): 'rpmfile': [], 'installed': ['non-existing-package'], } - options = {**options_preset, **additional_options} + options = get_options(additional_options) linter = Lint(options) linter.checks = _remove_except_zip(linter.checks) linter.run() @@ -276,7 +282,7 @@ def test_run_installed_and_no_files(capsys): 'rpmfile': [], 'installed': ['rpm'], } - options = {**options_preset, **additional_options} + options = get_options(additional_options) linter = Lint(options) linter.checks = _remove_except_zip(linter.checks) linter.run() @@ -291,7 +297,7 @@ def test_header_information(capsys): 'rpmfile': [], 'installed': ['python3-rpm'], } - options = {**options_preset, **additional_options} + options = get_options(additional_options) linter = Lint(options) linter.checks = _remove_except_zip(linter.checks) linter.run() @@ -310,7 +316,7 @@ def test_run_full_rpm(capsys, packages, configs): 'rpmfile': packages, } options_preset['config'] = configs - options = {**options_preset, **additional_options} + options = get_options(additional_options) linter = Lint(options) linter.run() out, err = capsys.readouterr() @@ -335,7 +341,7 @@ def test_run_full_specs(capsys, packages, configs): 'rpmfile': packages, } options_preset['config'] = configs - options = {**options_preset, **additional_options} + options = get_options(additional_options) linter = Lint(options) linter.run() out, err = capsys.readouterr() @@ -357,7 +363,7 @@ def test_run_full_directory(capsys, packages): additional_options = { 'rpmfile': [packages], } - options = {**options_preset, **additional_options} + options = get_options(additional_options) linter = Lint(options) linter.run() out, err = capsys.readouterr() @@ -382,7 +388,7 @@ def test_run_rpmlintrc_single_dir(capsys, packages): additional_options = { 'rpmfile': [packages], } - options = {**options_preset, **additional_options} + options = get_options(additional_options) linter = Lint(options) linter.run() out, err = capsys.readouterr() @@ -397,12 +403,12 @@ def test_run_rpmlintrc_multiple(capsys, packages): additional_options = { 'rpmfile': [packages], } - options = {**options_preset, **additional_options} + options = get_options(additional_options) linter = Lint(options) linter.run() out, err = capsys.readouterr() - assert 'rpmlintrc:' not in out - assert 'There are multiple items to be loaded for rpmlintrc' in err + assert 'rpmlintrc:' in out + assert 'There are multiple items to be loaded' in err assert '0 badness' in out @@ -414,7 +420,7 @@ def test_run_rpmlintrc_single_file(capsys, packages): 'rpmfile': [packages], 'rpmlintrc': [TEST_RPMLINTRC] } - options = {**options_preset, **additional_options} + options = get_options(additional_options) linter = Lint(options) linter.run() out, err = capsys.readouterr()