Code cleanups, avoid deeply nested blocks etc.

git-svn-id: svn+ssh://rpmlint.zarb.org/home/projects/rpmlint/svn/trunk@1754 9bc8b190-ac0f-0410-8968-dc7d1f502856
This commit is contained in:
Ville Skyttä 2010-03-30 20:34:48 +00:00
parent 8420e06175
commit f3410598ad
7 changed files with 219 additions and 232 deletions

View File

@ -546,18 +546,16 @@ class FilesCheck(AbstractCheck.AbstractCheck):
if not postin:
printError(pkg,
'info-files-without-install-info-postin', f)
else:
if not install_info_regex.search(postin):
printError(pkg, 'postin-without-install-info', f)
elif not install_info_regex.search(postin):
printError(pkg, 'postin-without-install-info', f)
if not postun and not preun:
printError(pkg,
'info-files-without-install-info-postun', f)
else:
if (not postun or
not install_info_regex.search(postun)) and \
(not preun or not install_info_regex.search(preun)):
printError(pkg, 'postin-without-install-info', f)
elif (not postun or
not install_info_regex.search(postun)) and \
(not preun or not install_info_regex.search(preun)):
printError(pkg, 'postin-without-install-info', f)
# check perl temp file
if perl_temp_file_regex.search(f):
@ -614,14 +612,13 @@ class FilesCheck(AbstractCheck.AbstractCheck):
if not python_dep_error:
res = python_regex.search(f)
if res:
if not (pkg.check_versioned_dep('python-base',
res.group(1)) or
pkg.check_versioned_dep('python',
res.group(1))):
printError(pkg, 'no-dependency-on', 'python-base',
res.group(1))
python_dep_error = True
if res and not (pkg.check_versioned_dep('python-base',
res.group(1)) or
pkg.check_versioned_dep('python',
res.group(1))):
printError(pkg, 'no-dependency-on', 'python-base',
res.group(1))
python_dep_error = True
res = python_bytecode_regex.search(f)
if res:

View File

@ -48,148 +48,141 @@ class InitScriptCheck(AbstractCheck.AbstractCheck):
initscript_list = []
for fname, pkgfile in pkg.files().items():
if fname.startswith('/etc/init.d/') or \
fname.startswith('/etc/rc.d/init.d/'):
basename = os.path.basename(fname)
initscript_list.append(basename)
if pkgfile.mode & 0500 != 0500:
printError(pkg, 'init-script-non-executable', fname)
if dot_in_name_regex.match(basename):
printError(pkg, 'init-script-name-with-dot', fname)
# check chkconfig call in %post and %preun
postin = pkg[rpm.RPMTAG_POSTIN] or pkg[rpm.RPMTAG_POSTINPROG]
if not postin:
printError(pkg,
'init-script-without-chkconfig-postin', fname)
else:
if not chkconfig_regex.search(postin):
printError(pkg, 'postin-without-chkconfig', fname)
if not fname.startswith('/etc/init.d/') and \
not fname.startswith('/etc/rc.d/init.d/'):
continue
preun = pkg[rpm.RPMTAG_PREUN] or pkg[rpm.RPMTAG_PREUNPROG]
if not preun:
printError(pkg,
'init-script-without-chkconfig-preun', fname)
else:
if not chkconfig_regex.search(preun):
printError(pkg, 'preun-without-chkconfig', fname)
basename = os.path.basename(fname)
initscript_list.append(basename)
if pkgfile.mode & 0500 != 0500:
printError(pkg, 'init-script-non-executable', fname)
status_found = False
reload_found = False
chkconfig_content_found = False
subsys_regex_found = False
in_lsb_tag = False
in_lsb_description = False
lastline = ''
lsb_tags = {}
# check common error in file content
content = None
try:
content = Pkg.readlines(pkgfile.path)
except Exception, e:
printWarning(pkg, 'read-error', e)
if dot_in_name_regex.match(basename):
printError(pkg, 'init-script-name-with-dot', fname)
# check chkconfig call in %post and %preun
postin = pkg[rpm.RPMTAG_POSTIN] or pkg[rpm.RPMTAG_POSTINPROG]
if not postin:
printError(pkg, 'init-script-without-chkconfig-postin', fname)
elif not chkconfig_regex.search(postin):
printError(pkg, 'postin-without-chkconfig', fname)
preun = pkg[rpm.RPMTAG_PREUN] or pkg[rpm.RPMTAG_PREUNPROG]
if not preun:
printError(pkg, 'init-script-without-chkconfig-preun', fname)
elif not chkconfig_regex.search(preun):
printError(pkg, 'preun-without-chkconfig', fname)
status_found = False
reload_found = False
chkconfig_content_found = False
subsys_regex_found = False
in_lsb_tag = False
in_lsb_description = False
lastline = ''
lsb_tags = {}
# check common error in file content
content = None
try:
content = Pkg.readlines(pkgfile.path)
except Exception, e:
printWarning(pkg, 'read-error', e)
continue
content_str = "".join(content)
for line in content:
line = line[:-1] # chomp
# TODO check if there is only one line like this
if line.startswith('### BEGIN INIT INFO'):
in_lsb_tag = True
continue
content_str = "".join(content)
for line in content:
line = line[:-1] # chomp
# TODO check if there is only one line like this
if line.startswith('### BEGIN INIT INFO'):
in_lsb_tag = True
continue
if line.endswith('### END INIT INFO'):
in_lsb_tag = False
for kw, vals in lsb_tags.items():
if len(vals) != 1:
printError(pkg, 'redundant-lsb-keyword', kw)
if line.endswith('### END INIT INFO'):
in_lsb_tag = False
for kw, vals in lsb_tags.items():
if len(vals) != 1:
printError(pkg, 'redundant-lsb-keyword', kw)
for kw in RECOMMENDED_LSB_KEYWORDS:
if kw not in lsb_tags:
printWarning(pkg,
'missing-lsb-keyword',
"%s in %s" % (kw, fname))
if in_lsb_tag:
# TODO maybe we do not have to handle this ?
if lastline.endswith('\\'):
line = lastline + line
else:
res = lsb_tags_regex.search(line)
if not res:
cres = lsb_cont_regex.search(line)
if not (in_lsb_description and cres):
in_lsb_description = False
printError(pkg,
'malformed-line-in-lsb-comment-block',
line)
else:
lsb_tags["Description"][-1] += \
" " + cres.group(1)
for kw in RECOMMENDED_LSB_KEYWORDS:
if kw not in lsb_tags:
printWarning(pkg, 'missing-lsb-keyword',
"%s in %s" % (kw, fname))
if in_lsb_tag:
# TODO maybe we do not have to handle this ?
if lastline.endswith('\\'):
line = lastline + line
else:
res = lsb_tags_regex.search(line)
if not res:
cres = lsb_cont_regex.search(line)
if not (in_lsb_description and cres):
in_lsb_description = False
printError(pkg,
'malformed-line-in-lsb-comment-block',
line)
else:
tag = res.group(1)
if not tag.startswith('X-') and \
tag not in LSB_KEYWORDS:
printError(pkg, 'unknown-lsb-keyword', line)
else:
in_lsb_description = (tag == 'Description')
if tag not in lsb_tags:
lsb_tags[tag] = []
lsb_tags[tag].append(res.group(2))
lastline = line
if not status_found and status_regex.search(line):
status_found = True
if not reload_found and reload_regex.search(line):
reload_found = True
res = chkconfig_content_regex.search(line)
if res:
chkconfig_content_found = True
if use_deflevels:
if res.group(1) == '-':
printWarning(pkg, 'no-default-runlevel', fname)
lsb_tags["Description"][-1] += \
" " + cres.group(1)
else:
if res.group(1) != '-':
printWarning(pkg,
'service-default-enabled', fname)
res = subsys_regex.search(line)
if res:
subsys_regex_found = True
name = res.group(1)
if name != basename:
error = True
if name[0] == '$':
value = Pkg.substitute_shell_vars(
name, content_str)
if value == basename:
error = False
tag = res.group(1)
if not tag.startswith('X-') and \
tag not in LSB_KEYWORDS:
printError(pkg, 'unknown-lsb-keyword', line)
else:
i = name.find('}')
if i != -1:
name = name[0:i]
error = name != basename
if error and len(name):
if name[0] == '$':
printWarning(pkg, 'incoherent-subsys',
fname, name)
else:
printError(pkg, 'incoherent-subsys',
fname, name)
in_lsb_description = (tag == 'Description')
if tag not in lsb_tags:
lsb_tags[tag] = []
lsb_tags[tag].append(res.group(2))
lastline = line
if "Default-Start" in lsb_tags:
if "".join(lsb_tags["Default-Start"]):
if not status_found and status_regex.search(line):
status_found = True
if not reload_found and reload_regex.search(line):
reload_found = True
res = chkconfig_content_regex.search(line)
if res:
chkconfig_content_found = True
if use_deflevels:
if res.group(1) == '-':
printWarning(pkg, 'no-default-runlevel', fname)
elif res.group(1) != '-':
printWarning(pkg, 'service-default-enabled', fname)
if not status_found:
printError(pkg, 'no-status-entry', fname)
if not reload_found:
printWarning(pkg, 'no-reload-entry', fname)
if not chkconfig_content_found:
printError(pkg, 'no-chkconfig-line', fname)
if not subsys_regex_found:
printError(pkg, 'subsys-not-used', fname)
res = subsys_regex.search(line)
if res:
subsys_regex_found = True
name = res.group(1)
if name != basename:
error = True
if name[0] == '$':
value = Pkg.substitute_shell_vars(name, content_str)
if value == basename:
error = False
else:
i = name.find('}')
if i != -1:
name = name[0:i]
error = name != basename
if error and len(name):
if name[0] == '$':
printWarning(pkg, 'incoherent-subsys', fname,
name)
else:
printError(pkg, 'incoherent-subsys', fname,
name)
if "Default-Start" in lsb_tags:
if "".join(lsb_tags["Default-Start"]):
printWarning(pkg, 'service-default-enabled', fname)
if not status_found:
printError(pkg, 'no-status-entry', fname)
if not reload_found:
printWarning(pkg, 'no-reload-entry', fname)
if not chkconfig_content_found:
printError(pkg, 'no-chkconfig-line', fname)
if not subsys_regex_found:
printError(pkg, 'subsys-not-used', fname)
goodnames = (pkg.name.lower(), pkg.name.lower() + 'd')
if len(initscript_list) == 1 and initscript_list[0] not in goodnames:

View File

@ -203,16 +203,14 @@ class MenuCheck(AbstractCheck.AbstractCheck):
postin = pkg[rpm.RPMTAG_POSTIN] or pkg[rpm.RPMTAG_POSTINPROG]
if not postin:
printError(pkg, 'menu-without-postin')
else:
if not update_menus_regex.search(postin):
printError(pkg, 'postin-without-update-menus')
elif not update_menus_regex.search(postin):
printError(pkg, 'postin-without-update-menus')
postun = pkg[rpm.RPMTAG_POSTUN] or pkg[rpm.RPMTAG_POSTUNPROG]
if not postun:
printError(pkg, 'menu-without-postun')
else:
if not update_menus_regex.search(postun):
printError(pkg, 'postun-without-update-menus')
elif not update_menus_regex.search(postun):
printError(pkg, 'postun-without-update-menus')
directory = pkg.dirName()
for f in menus:
@ -220,7 +218,8 @@ class MenuCheck(AbstractCheck.AbstractCheck):
cmd = Pkg.getstatusoutput(('/lib/cpp', directory + f), True)[1]
for line in cmd.splitlines():
if not line.startswith('?'): continue
if not line.startswith('?'):
continue
res = package_regex.search(line)
if res:
package = res.group(1)
@ -237,35 +236,36 @@ class MenuCheck(AbstractCheck.AbstractCheck):
command_line = (res.group(1) or res.group(2)).split()
command = command_line[0]
for launcher in launchers:
if launcher[0].search(command):
found = False
if launcher[1]:
if ('/bin/' + command_line[0] in files or
'/usr/bin/' + command_line[0] in files
or '/usr/X11R6/bin/' + command_line[0]
in files):
found = True
else:
for l in launcher[1]:
if l in pkg.req_names():
found = True
break
if not found:
printError(pkg,
'use-of-launcher-in-menu-but-no-requires-on',
launcher[1][0])
command = command_line[1]
break
if not launcher[0].search(command):
continue
found = False
if launcher[1]:
if ('/bin/' + command_line[0] in files or
'/usr/bin/' + command_line[0] in files
or '/usr/X11R6/bin/' + command_line[0]
in files):
found = True
else:
for l in launcher[1]:
if l in pkg.req_names():
found = True
break
if not found:
printError(pkg,
'use-of-launcher-in-menu-but-no-requires-on',
launcher[1][0])
command = command_line[1]
break
if command[0] == '/':
if command not in files:
printWarning(pkg, 'menu-command-not-in-package',
command)
else:
if not ('/bin/' + command in files or
'/usr/bin/' + command in files or
'/usr/X11R6/bin/' + command in files):
printWarning(pkg, 'menu-command-not-in-package',
command)
elif not ('/bin/' + command in files or
'/usr/bin/' + command in files or
'/usr/X11R6/bin/' + command in files):
printWarning(pkg, 'menu-command-not-in-package',
command)
else:
printWarning(pkg, 'missing-menu-command')
command = False
@ -311,10 +311,9 @@ class MenuCheck(AbstractCheck.AbstractCheck):
grp = res.groups()
section = grp[1] or grp[2]
# don't warn entries for sections
if command:
if section not in valid_sections:
printError(pkg, 'invalid-menu-section',
section, f)
if command and section not in valid_sections:
printError(pkg, 'invalid-menu-section',
section, f)
else:
printInfo(pkg, 'unable-to-parse-menu-section',
line)

View File

@ -195,9 +195,8 @@ class PostCheck(AbstractCheck.AbstractCheck):
res = single_command_regex.search(script)
if res:
printWarning(pkg, 'one-line-command-in-' + tag[2], res.group(1))
else:
if prog not in empty_shells and prog in valid_shells:
printWarning(pkg, 'empty-' + tag[2])
elif prog not in empty_shells and prog in valid_shells:
printWarning(pkg, 'empty-' + tag[2])
# Create an object to enable the auto registration of the test
check = PostCheck()

View File

@ -40,10 +40,10 @@ class SourceCheck(AbstractCheck.AbstractCheck):
printError(pkg, 'multiple-specfiles', spec_file, fname)
else:
spec_file = fname
elif source_regex.search(fname) and compress_ext:
if not fname.endswith(compress_ext):
printWarning(pkg, 'source-or-patch-not-compressed',
compress_ext, fname)
elif source_regex.search(fname) and compress_ext and \
not fname.endswith(compress_ext):
printWarning(pkg, 'source-or-patch-not-compressed',
compress_ext, fname)
perm = pkgfile.mode & 07777
if perm not in valid_src_perms:
printWarning(pkg, 'strange-permission', fname, oct(perm))

View File

@ -173,10 +173,10 @@ class SpecCheck(AbstractCheck.AbstractCheck):
if fname.endswith('.spec'):
self._spec_file = pkgfile.path
if fname == pkg.name + ".spec":
wrong_spec = False
break
wrong_spec = False
break
else:
wrong_spec = True
wrong_spec = True
if not self._spec_file:
printError(pkg, "no-spec-file")
else:
@ -267,19 +267,19 @@ class SpecCheck(AbstractCheck.AbstractCheck):
continue
if current_section in ('prep', 'build'):
if contains_buildroot(line):
printWarning(pkg, 'rpm-buildroot-usage',
'%' + current_section, line[:-1].strip())
if current_section in ('prep', 'build') and \
contains_buildroot(line):
printWarning(pkg, 'rpm-buildroot-usage', '%' + current_section,
line[:-1].strip())
if make_check_regex.search(line) and current_section not in \
('check', 'changelog', 'package', 'description'):
printWarning(pkg, 'make-check-outside-check-section', line[:-1])
if current_section in buildroot_clean and \
not buildroot_clean[current_section]:
if contains_buildroot(line) and rm_regex.search(line):
buildroot_clean[current_section] = True
not buildroot_clean[current_section] and \
contains_buildroot(line) and rm_regex.search(line):
buildroot_clean[current_section] = True
if ifarch_regex.search(line):
if_depth = if_depth + 1

View File

@ -489,19 +489,20 @@ def spell_check(pkg, str, fmt, lang, ignored):
if not enchant or not dict_found:
for seq in str.split():
for word in re.split('[^a-z]+', seq.lower()):
if len(word) > 0:
correct = BAD_WORDS.get(word)
if not correct:
continue
if word[0] == '\'':
word = word[1:]
if word[-1] == '\'':
word = word[:-1]
if word in warned or word in ignored:
continue
printWarning(pkg, 'spelling-error', fmt % lang,
word, '->', correct)
warned.add(word)
if len(word) == 0:
continue
correct = BAD_WORDS.get(word)
if not correct:
continue
if word[0] == '\'':
word = word[1:]
if word[-1] == '\'':
word = word[:-1]
if word in warned or word in ignored:
continue
printWarning(pkg, 'spelling-error', fmt % lang, word, '->',
correct)
warned.add(word)
class TagsCheck(AbstractCheck.AbstractCheck):
@ -510,12 +511,13 @@ class TagsCheck(AbstractCheck.AbstractCheck):
AbstractCheck.AbstractCheck.__init__(self, 'TagsCheck')
def _unexpanded_macros(self, pkg, tagname, value, is_url=False):
if value:
for match in AbstractCheck.macro_regex.findall(str(value)):
# Do not warn about %XX URL escapes
if is_url and re.match('^%[0-9A-F][0-9A-F]$', match, re.I):
continue
printWarning(pkg, 'unexpanded-macro', tagname, match)
if not value:
return
for match in AbstractCheck.macro_regex.findall(str(value)):
# Do not warn about %XX URL escapes
if is_url and re.match('^%[0-9A-F][0-9A-F]$', match, re.I):
continue
printWarning(pkg, 'unexpanded-macro', tagname, match)
def check(self, pkg):
@ -547,9 +549,8 @@ class TagsCheck(AbstractCheck.AbstractCheck):
if epoch is None:
if use_epoch:
printError(pkg, 'no-epoch-tag')
else:
if epoch > 99:
printWarning(pkg, 'unreasonable-epoch', epoch)
elif epoch > 99:
printWarning(pkg, 'unreasonable-epoch', epoch)
if use_epoch:
for o in pkg.obsoletes():
@ -580,10 +581,10 @@ class TagsCheck(AbstractCheck.AbstractCheck):
if d[0].startswith('/usr/local/'):
printError(pkg, 'invalid-dependency', d[0])
if not devel_depend and not is_devel and not is_source:
if FilesCheck.devel_regex.search(d[0]):
printError(pkg, 'devel-dependency', d[0])
devel_depend = True
if not devel_depend and not is_devel and not is_source and \
FilesCheck.devel_regex.search(d[0]):
printError(pkg, 'devel-dependency', d[0])
devel_depend = True
if is_source and lib_devel_number_regex.search(d[0]):
printError(pkg, 'invalid-build-requires', d[0])
if not is_source and not is_devel:
@ -760,9 +761,8 @@ class TagsCheck(AbstractCheck.AbstractCheck):
obs_names = [x[0] for x in pkg.obsoletes()]
prov_names = [x[0] for x in pkg.provides()]
for o in obs_names:
if o not in prov_names:
printWarning(pkg, 'obsolete-not-provided', o)
for o in (x for x in obs_names if x not in prov_names):
printWarning(pkg, 'obsolete-not-provided', o)
for o in pkg.obsoletes():
self._unexpanded_macros(pkg, 'Obsoletes %s' % \
apply(Pkg.formatRequire, o), o[1])
@ -771,9 +771,8 @@ class TagsCheck(AbstractCheck.AbstractCheck):
# https://bugzilla.redhat.com/460872
useless_provides = []
for p in prov_names:
if prov_names.count(p) != 1:
if p not in useless_provides:
useless_provides.append(p)
if prov_names.count(p) != 1 and p not in useless_provides:
useless_provides.append(p)
for p in useless_provides:
printError(pkg, 'useless-provides', p)