Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_warnprint(capsys):
"""
Check we print stuff to stderr
"""
message = 'I am writing to stderr'
helpers.print_warning(message)
out, err = capsys.readouterr()
assert message not in out
assert message in err
def validate_files(self, files):
"""
Run all the check for passed file list
"""
if not files:
if self.packages_checked == 0:
# print warning only if we didn't process even installed files
print_warning('There are no files to process nor additional arguments.')
print_warning('Nothing to do, aborting.')
return
# check all elements if they are a folder or a file with proper suffix
# and expand everything
packages = self._expand_filelist(files)
for pkg in packages:
self.validate_file(pkg)
def validate_file(self, pname):
try:
if pname.suffix == '.rpm' or pname.suffix == '.spm':
with Pkg(pname, self.config.configuration['ExtractDir']) as pkg:
self.run_checks(pkg)
elif pname.suffix == '.spec':
with FakePkg(pname) as pkg:
self.run_spec_checks(pkg)
except Exception as e:
print_warning(f'(none): E: while reading {pname}: {e}')
def load_config(self, config=None):
"""
Load the configuration files and append it to local dictionary with the
content of already loaded options.
"""
if config and config not in self.conf_files:
# just add the config at the end of the list, someone injected
# config file to us
if config.exists():
self.conf_files.append(config)
try:
cfg = toml.load(self.conf_files)
except toml.decoder.TomlDecodeError as terr:
print_warning(f'(none): W: error parsing configuration files: {terr}')
cfg = None
self.configuration = cfg
def _extract(self, dirname):
if not Path(dirname).is_dir():
print_warning('Unable to access dir %s' % dirname)
else:
dirname = dirname if dirname != '/' else None
self.__tmpdir = tempfile.TemporaryDirectory(
prefix='rpmlint.%s.' % Path(self.filename).name, dir=dirname
)
dirname = self.__tmpdir.name
# TODO: sequence based command invocation
# TODO: warn some way if this fails (e.g. rpm2cpio not installed)
command_str = \
'rpm2cpio %(f)s | cpio -id -D %(d)s ; chmod -R +rX %(d)s' % \
{'f': quote(str(self.filename)), 'd': quote(dirname)}
subprocess.run(command_str, shell=True, env=ENGLISH_ENVIROMENT)
self.extracted = True
return dirname
def _init_checker(self, lang='en_US'):
"""
Initialize a checker of selected language if it is not yet present
lang: language to initialize the dictionary
"""
# C language means English
if lang == 'C':
lang = 'en_US'
# test if we actually have working enchant
if not ENCHANT:
print_warning('(none): W: unable to init enchant, spellchecking disabled.')
return
# there might not be myspell/aspell/etc dicts present
broker = Broker()
if not broker.dict_exists(lang):
print_warning(f'(none): W: unable to load spellchecking dictionary for {lang}.')
return
if lang not in self._enchant_checkers:
checker = SpellChecker(lang, filters=[EmailFilter, URLFilter, WikiWordFilter])
self._enchant_checkers[lang] = checker
self.config.load_rpmlintrc(self.options['rpmlintrc'])
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])