Skip to content

Commit

Permalink
Automatically load rpmlintrc files.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
marxin committed Jun 24, 2021
1 parent e0919ca commit 3ec4e4e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 45 deletions.
4 changes: 4 additions & 0 deletions rpmlint/cli.py
Expand Up @@ -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
Expand Down
54 changes: 31 additions & 23 deletions 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
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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}')
Expand Down
50 changes: 28 additions & 22 deletions test/test_lint.py
@@ -1,3 +1,4 @@
import copy
from pathlib import Path

import pytest
Expand All @@ -19,7 +20,7 @@
'print_config': False,
'explain': '',
'rpmfile': '',
'rpmlintrc': False,
'rpmlintrc': [],
'installed': '',
'time_report': False,
'profile': False
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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


Expand All @@ -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()
Expand Down

0 comments on commit 3ec4e4e

Please sign in to comment.