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

View File

@ -48,148 +48,141 @@ class InitScriptCheck(AbstractCheck.AbstractCheck):
initscript_list = [] initscript_list = []
for fname, pkgfile in pkg.files().items(): 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): if not fname.startswith('/etc/init.d/') and \
printError(pkg, 'init-script-name-with-dot', fname) not fname.startswith('/etc/rc.d/init.d/'):
# check chkconfig call in %post and %preun continue
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)
preun = pkg[rpm.RPMTAG_PREUN] or pkg[rpm.RPMTAG_PREUNPROG] basename = os.path.basename(fname)
if not preun: initscript_list.append(basename)
printError(pkg, if pkgfile.mode & 0500 != 0500:
'init-script-without-chkconfig-preun', fname) printError(pkg, 'init-script-non-executable', fname)
else:
if not chkconfig_regex.search(preun):
printError(pkg, 'preun-without-chkconfig', fname)
status_found = False if dot_in_name_regex.match(basename):
reload_found = False printError(pkg, 'init-script-name-with-dot', fname)
chkconfig_content_found = False # check chkconfig call in %post and %preun
subsys_regex_found = False postin = pkg[rpm.RPMTAG_POSTIN] or pkg[rpm.RPMTAG_POSTINPROG]
in_lsb_tag = False if not postin:
in_lsb_description = False printError(pkg, 'init-script-without-chkconfig-postin', fname)
lastline = '' elif not chkconfig_regex.search(postin):
lsb_tags = {} printError(pkg, 'postin-without-chkconfig', fname)
# check common error in file content
content = None preun = pkg[rpm.RPMTAG_PREUN] or pkg[rpm.RPMTAG_PREUNPROG]
try: if not preun:
content = Pkg.readlines(pkgfile.path) printError(pkg, 'init-script-without-chkconfig-preun', fname)
except Exception, e: elif not chkconfig_regex.search(preun):
printWarning(pkg, 'read-error', e) 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 continue
content_str = "".join(content) if line.endswith('### END INIT INFO'):
for line in content: in_lsb_tag = False
line = line[:-1] # chomp for kw, vals in lsb_tags.items():
# TODO check if there is only one line like this if len(vals) != 1:
if line.startswith('### BEGIN INIT INFO'): printError(pkg, 'redundant-lsb-keyword', kw)
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)
for kw in RECOMMENDED_LSB_KEYWORDS: for kw in RECOMMENDED_LSB_KEYWORDS:
if kw not in lsb_tags: if kw not in lsb_tags:
printWarning(pkg, printWarning(pkg, 'missing-lsb-keyword',
'missing-lsb-keyword', "%s in %s" % (kw, fname))
"%s in %s" % (kw, fname)) if in_lsb_tag:
if in_lsb_tag: # TODO maybe we do not have to handle this ?
# TODO maybe we do not have to handle this ? if lastline.endswith('\\'):
if lastline.endswith('\\'): line = lastline + line
line = lastline + line else:
else: res = lsb_tags_regex.search(line)
res = lsb_tags_regex.search(line) if not res:
if not res: cres = lsb_cont_regex.search(line)
cres = lsb_cont_regex.search(line) if not (in_lsb_description and cres):
if not (in_lsb_description and cres): in_lsb_description = False
in_lsb_description = False printError(pkg,
printError(pkg, 'malformed-line-in-lsb-comment-block',
'malformed-line-in-lsb-comment-block', line)
line)
else:
lsb_tags["Description"][-1] += \
" " + cres.group(1)
else: else:
tag = res.group(1) lsb_tags["Description"][-1] += \
if not tag.startswith('X-') and \ " " + cres.group(1)
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)
else: else:
if res.group(1) != '-': tag = res.group(1)
printWarning(pkg, if not tag.startswith('X-') and \
'service-default-enabled', fname) tag not in LSB_KEYWORDS:
printError(pkg, 'unknown-lsb-keyword', line)
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: else:
i = name.find('}') in_lsb_description = (tag == 'Description')
if i != -1: if tag not in lsb_tags:
name = name[0:i] lsb_tags[tag] = []
error = name != basename lsb_tags[tag].append(res.group(2))
if error and len(name): lastline = line
if name[0] == '$':
printWarning(pkg, 'incoherent-subsys',
fname, name)
else:
printError(pkg, 'incoherent-subsys',
fname, name)
if "Default-Start" in lsb_tags: if not status_found and status_regex.search(line):
if "".join(lsb_tags["Default-Start"]): 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) printWarning(pkg, 'service-default-enabled', fname)
if not status_found: res = subsys_regex.search(line)
printError(pkg, 'no-status-entry', fname) if res:
if not reload_found: subsys_regex_found = True
printWarning(pkg, 'no-reload-entry', fname) name = res.group(1)
if not chkconfig_content_found: if name != basename:
printError(pkg, 'no-chkconfig-line', fname) error = True
if not subsys_regex_found: if name[0] == '$':
printError(pkg, 'subsys-not-used', fname) 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') goodnames = (pkg.name.lower(), pkg.name.lower() + 'd')
if len(initscript_list) == 1 and initscript_list[0] not in goodnames: 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] postin = pkg[rpm.RPMTAG_POSTIN] or pkg[rpm.RPMTAG_POSTINPROG]
if not postin: if not postin:
printError(pkg, 'menu-without-postin') printError(pkg, 'menu-without-postin')
else: elif not update_menus_regex.search(postin):
if not update_menus_regex.search(postin): printError(pkg, 'postin-without-update-menus')
printError(pkg, 'postin-without-update-menus')
postun = pkg[rpm.RPMTAG_POSTUN] or pkg[rpm.RPMTAG_POSTUNPROG] postun = pkg[rpm.RPMTAG_POSTUN] or pkg[rpm.RPMTAG_POSTUNPROG]
if not postun: if not postun:
printError(pkg, 'menu-without-postun') printError(pkg, 'menu-without-postun')
else: elif not update_menus_regex.search(postun):
if not update_menus_regex.search(postun): printError(pkg, 'postun-without-update-menus')
printError(pkg, 'postun-without-update-menus')
directory = pkg.dirName() directory = pkg.dirName()
for f in menus: for f in menus:
@ -220,7 +218,8 @@ class MenuCheck(AbstractCheck.AbstractCheck):
cmd = Pkg.getstatusoutput(('/lib/cpp', directory + f), True)[1] cmd = Pkg.getstatusoutput(('/lib/cpp', directory + f), True)[1]
for line in cmd.splitlines(): for line in cmd.splitlines():
if not line.startswith('?'): continue if not line.startswith('?'):
continue
res = package_regex.search(line) res = package_regex.search(line)
if res: if res:
package = res.group(1) package = res.group(1)
@ -237,35 +236,36 @@ class MenuCheck(AbstractCheck.AbstractCheck):
command_line = (res.group(1) or res.group(2)).split() command_line = (res.group(1) or res.group(2)).split()
command = command_line[0] command = command_line[0]
for launcher in launchers: for launcher in launchers:
if launcher[0].search(command): if not launcher[0].search(command):
found = False continue
if launcher[1]: found = False
if ('/bin/' + command_line[0] in files or if launcher[1]:
'/usr/bin/' + command_line[0] in files if ('/bin/' + command_line[0] in files or
or '/usr/X11R6/bin/' + command_line[0] '/usr/bin/' + command_line[0] in files
in files): or '/usr/X11R6/bin/' + command_line[0]
found = True in files):
else: found = True
for l in launcher[1]: else:
if l in pkg.req_names(): for l in launcher[1]:
found = True if l in pkg.req_names():
break found = True
if not found: break
printError(pkg, if not found:
'use-of-launcher-in-menu-but-no-requires-on', printError(pkg,
launcher[1][0]) 'use-of-launcher-in-menu-but-no-requires-on',
command = command_line[1] launcher[1][0])
break command = command_line[1]
break
if command[0] == '/': if command[0] == '/':
if command not in files: if command not in files:
printWarning(pkg, 'menu-command-not-in-package', printWarning(pkg, 'menu-command-not-in-package',
command) command)
else: elif not ('/bin/' + command in files or
if not ('/bin/' + command in files or '/usr/bin/' + command in files or
'/usr/bin/' + command in files or '/usr/X11R6/bin/' + command in files):
'/usr/X11R6/bin/' + command in files): printWarning(pkg, 'menu-command-not-in-package',
printWarning(pkg, 'menu-command-not-in-package', command)
command)
else: else:
printWarning(pkg, 'missing-menu-command') printWarning(pkg, 'missing-menu-command')
command = False command = False
@ -311,10 +311,9 @@ class MenuCheck(AbstractCheck.AbstractCheck):
grp = res.groups() grp = res.groups()
section = grp[1] or grp[2] section = grp[1] or grp[2]
# don't warn entries for sections # don't warn entries for sections
if command: if command and section not in valid_sections:
if section not in valid_sections: printError(pkg, 'invalid-menu-section',
printError(pkg, 'invalid-menu-section', section, f)
section, f)
else: else:
printInfo(pkg, 'unable-to-parse-menu-section', printInfo(pkg, 'unable-to-parse-menu-section',
line) line)

View File

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

View File

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

View File

@ -173,10 +173,10 @@ class SpecCheck(AbstractCheck.AbstractCheck):
if fname.endswith('.spec'): if fname.endswith('.spec'):
self._spec_file = pkgfile.path self._spec_file = pkgfile.path
if fname == pkg.name + ".spec": if fname == pkg.name + ".spec":
wrong_spec = False wrong_spec = False
break break
else: else:
wrong_spec = True wrong_spec = True
if not self._spec_file: if not self._spec_file:
printError(pkg, "no-spec-file") printError(pkg, "no-spec-file")
else: else:
@ -267,19 +267,19 @@ class SpecCheck(AbstractCheck.AbstractCheck):
continue continue
if current_section in ('prep', 'build'): if current_section in ('prep', 'build') and \
if contains_buildroot(line): contains_buildroot(line):
printWarning(pkg, 'rpm-buildroot-usage', printWarning(pkg, 'rpm-buildroot-usage', '%' + current_section,
'%' + current_section, line[:-1].strip()) line[:-1].strip())
if make_check_regex.search(line) and current_section not in \ if make_check_regex.search(line) and current_section not in \
('check', 'changelog', 'package', 'description'): ('check', 'changelog', 'package', 'description'):
printWarning(pkg, 'make-check-outside-check-section', line[:-1]) printWarning(pkg, 'make-check-outside-check-section', line[:-1])
if current_section in buildroot_clean and \ if current_section in buildroot_clean and \
not buildroot_clean[current_section]: not buildroot_clean[current_section] and \
if contains_buildroot(line) and rm_regex.search(line): contains_buildroot(line) and rm_regex.search(line):
buildroot_clean[current_section] = True buildroot_clean[current_section] = True
if ifarch_regex.search(line): if ifarch_regex.search(line):
if_depth = if_depth + 1 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: if not enchant or not dict_found:
for seq in str.split(): for seq in str.split():
for word in re.split('[^a-z]+', seq.lower()): for word in re.split('[^a-z]+', seq.lower()):
if len(word) > 0: if len(word) == 0:
correct = BAD_WORDS.get(word) continue
if not correct: correct = BAD_WORDS.get(word)
continue if not correct:
if word[0] == '\'': continue
word = word[1:] if word[0] == '\'':
if word[-1] == '\'': word = word[1:]
word = word[:-1] if word[-1] == '\'':
if word in warned or word in ignored: word = word[:-1]
continue if word in warned or word in ignored:
printWarning(pkg, 'spelling-error', fmt % lang, continue
word, '->', correct) printWarning(pkg, 'spelling-error', fmt % lang, word, '->',
warned.add(word) correct)
warned.add(word)
class TagsCheck(AbstractCheck.AbstractCheck): class TagsCheck(AbstractCheck.AbstractCheck):
@ -510,12 +511,13 @@ class TagsCheck(AbstractCheck.AbstractCheck):
AbstractCheck.AbstractCheck.__init__(self, 'TagsCheck') AbstractCheck.AbstractCheck.__init__(self, 'TagsCheck')
def _unexpanded_macros(self, pkg, tagname, value, is_url=False): def _unexpanded_macros(self, pkg, tagname, value, is_url=False):
if value: if not value:
for match in AbstractCheck.macro_regex.findall(str(value)): return
# Do not warn about %XX URL escapes for match in AbstractCheck.macro_regex.findall(str(value)):
if is_url and re.match('^%[0-9A-F][0-9A-F]$', match, re.I): # Do not warn about %XX URL escapes
continue if is_url and re.match('^%[0-9A-F][0-9A-F]$', match, re.I):
printWarning(pkg, 'unexpanded-macro', tagname, match) continue
printWarning(pkg, 'unexpanded-macro', tagname, match)
def check(self, pkg): def check(self, pkg):
@ -547,9 +549,8 @@ class TagsCheck(AbstractCheck.AbstractCheck):
if epoch is None: if epoch is None:
if use_epoch: if use_epoch:
printError(pkg, 'no-epoch-tag') printError(pkg, 'no-epoch-tag')
else: elif epoch > 99:
if epoch > 99: printWarning(pkg, 'unreasonable-epoch', epoch)
printWarning(pkg, 'unreasonable-epoch', epoch)
if use_epoch: if use_epoch:
for o in pkg.obsoletes(): for o in pkg.obsoletes():
@ -580,10 +581,10 @@ class TagsCheck(AbstractCheck.AbstractCheck):
if d[0].startswith('/usr/local/'): if d[0].startswith('/usr/local/'):
printError(pkg, 'invalid-dependency', d[0]) printError(pkg, 'invalid-dependency', d[0])
if not devel_depend and not is_devel and not is_source: if not devel_depend and not is_devel and not is_source and \
if FilesCheck.devel_regex.search(d[0]): FilesCheck.devel_regex.search(d[0]):
printError(pkg, 'devel-dependency', d[0]) printError(pkg, 'devel-dependency', d[0])
devel_depend = True devel_depend = True
if is_source and lib_devel_number_regex.search(d[0]): if is_source and lib_devel_number_regex.search(d[0]):
printError(pkg, 'invalid-build-requires', d[0]) printError(pkg, 'invalid-build-requires', d[0])
if not is_source and not is_devel: 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()] obs_names = [x[0] for x in pkg.obsoletes()]
prov_names = [x[0] for x in pkg.provides()] prov_names = [x[0] for x in pkg.provides()]
for o in obs_names: for o in (x for x in obs_names if x not in prov_names):
if o not in prov_names: printWarning(pkg, 'obsolete-not-provided', o)
printWarning(pkg, 'obsolete-not-provided', o)
for o in pkg.obsoletes(): for o in pkg.obsoletes():
self._unexpanded_macros(pkg, 'Obsoletes %s' % \ self._unexpanded_macros(pkg, 'Obsoletes %s' % \
apply(Pkg.formatRequire, o), o[1]) apply(Pkg.formatRequire, o), o[1])
@ -771,9 +771,8 @@ class TagsCheck(AbstractCheck.AbstractCheck):
# https://bugzilla.redhat.com/460872 # https://bugzilla.redhat.com/460872
useless_provides = [] useless_provides = []
for p in prov_names: for p in prov_names:
if prov_names.count(p) != 1: if prov_names.count(p) != 1 and p not in useless_provides:
if p not in useless_provides: useless_provides.append(p)
useless_provides.append(p)
for p in useless_provides: for p in useless_provides:
printError(pkg, 'useless-provides', p) printError(pkg, 'useless-provides', p)