Fix minor style issues identified by pylint
Pylint is a static analyzer of Python code. It seems appropriate to use a static analyzer to analyze a static analyzer :-). None of the changes here fix a flaw in flawfinder. The goal here is to follow PEP 008 more closely in the hope that the changes make it easier for other developers to improve it further. Signed-off-by: David A. Wheeler <dwheeler@dwheeler.com>
This commit is contained in:
parent
595f45ad13
commit
d06466bd2a
84
flawfinder
84
flawfinder
|
@ -8,7 +8,7 @@ from __future__ import division
|
||||||
|
|
||||||
See the man page for a description of the options."""
|
See the man page for a description of the options."""
|
||||||
|
|
||||||
version="2.0.0"
|
version = "2.0.0"
|
||||||
|
|
||||||
# The default output is as follows:
|
# The default output is as follows:
|
||||||
# filename:line_number [risk_level] (type) function_name: message
|
# filename:line_number [risk_level] (type) function_name: message
|
||||||
|
@ -113,8 +113,8 @@ sloc = 0 # Physical SLOC
|
||||||
starttime = time.time() # Used to determine analyzed lines/second.
|
starttime = time.time() # Used to determine analyzed lines/second.
|
||||||
|
|
||||||
|
|
||||||
line_beginning = re.compile( r'(?m)^' )
|
line_beginning = re.compile(r'(?m)^')
|
||||||
blank_line = re.compile( r'(?m)^\s+$' )
|
blank_line = re.compile(r'(?m)^\s+$')
|
||||||
|
|
||||||
# Send warning message. This is written this way to work on
|
# Send warning message. This is written this way to work on
|
||||||
# Python version 2.5 through Python 3.
|
# Python version 2.5 through Python 3.
|
||||||
|
@ -168,12 +168,12 @@ def print_warning(message):
|
||||||
# unified format.
|
# unified format.
|
||||||
#
|
#
|
||||||
|
|
||||||
diff_index_filename = re.compile( r'^Index:\s+(?P<filename>.*)' )
|
diff_index_filename = re.compile(r'^Index:\s+(?P<filename>.*)')
|
||||||
diff_git_filename = re.compile( r'^diff --git a/.* b/(?P<filename>.*)$' )
|
diff_git_filename = re.compile(r'^diff --git a/.* b/(?P<filename>.*)$')
|
||||||
diff_newfile = re.compile( r'^\+\+\+\s(?P<filename>.*)$' )
|
diff_newfile = re.compile(r'^\+\+\+\s(?P<filename>.*)$')
|
||||||
diff_hunk = re.compile( r'^@@ -\d+(,\d+)?\s+\+(?P<linenumber>\d+)[, ].*@@$' )
|
diff_hunk = re.compile(r'^@@ -\d+(,\d+)?\s+\+(?P<linenumber>\d+)[, ].*@@$')
|
||||||
diff_line_added = re.compile( r'^\+[^+].*' )
|
diff_line_added = re.compile(r'^\+[^+].*')
|
||||||
diff_line_del = re.compile( r'^-[^-].*' )
|
diff_line_del = re.compile(r'^-[^-].*')
|
||||||
# The "+++" newfile entries have the filename, followed by a timestamp
|
# The "+++" newfile entries have the filename, followed by a timestamp
|
||||||
# or " (comment)" postpended.
|
# or " (comment)" postpended.
|
||||||
# Timestamps can be of these forms:
|
# Timestamps can be of these forms:
|
||||||
|
@ -181,10 +181,10 @@ diff_line_del = re.compile( r'^-[^-].*' )
|
||||||
# Mon Mar 10 15:13:12 1997
|
# Mon Mar 10 15:13:12 1997
|
||||||
# Also, "newfile" can have " (comment)" postpended. Find and eliminate this.
|
# Also, "newfile" can have " (comment)" postpended. Find and eliminate this.
|
||||||
# Note that the expression below is Y10K (and Y100K) ready. :-).
|
# Note that the expression below is Y10K (and Y100K) ready. :-).
|
||||||
diff_findjunk = re.compile( r'^(?P<filename>.*)((\s\d\d\d\d+-\d\d-\d\d\s+\d\d:\d[0-9:.]+Z?(\s+[\-\+0-9A-Z]+)?)|(\s[A-Za-z][a-z]+\s[A-za-z][a-z]+\s\d+\s\d+:\d[0-9:.]+Z?(\s[\-\+0-9]*)?\s\d\d\d\d+)|(\s\(.*\)))\s*$')
|
diff_findjunk = re.compile(r'^(?P<filename>.*)((\s\d\d\d\d+-\d\d-\d\d\s+\d\d:\d[0-9:.]+Z?(\s+[\-\+0-9A-Z]+)?)|(\s[A-Za-z][a-z]+\s[A-za-z][a-z]+\s\d+\s\d+:\d[0-9:.]+Z?(\s[\-\+0-9]*)?\s\d\d\d\d+)|(\s\(.*\)))\s*$')
|
||||||
|
|
||||||
def is_svn_diff(sLine):
|
def is_svn_diff(sLine):
|
||||||
if (sLine.find('Index:') != -1):
|
if sLine.find('Index:') != -1:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -203,14 +203,14 @@ def svn_diff_get_filename(sLine):
|
||||||
|
|
||||||
def gnu_diff_get_filename(sLine):
|
def gnu_diff_get_filename(sLine):
|
||||||
newfile_match = diff_newfile.match(sLine)
|
newfile_match = diff_newfile.match(sLine)
|
||||||
if (newfile_match):
|
if newfile_match:
|
||||||
patched_filename = string.strip(newfile_match.group('filename'))
|
patched_filename = string.strip(newfile_match.group('filename'))
|
||||||
# Clean up filename - remove trailing timestamp and/or (comment).
|
# Clean up filename - remove trailing timestamp and/or (comment).
|
||||||
return diff_findjunk.match(patched_filename)
|
return diff_findjunk.match(patched_filename)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
git_splitter=' b/'
|
git_splitter = ' b/'
|
||||||
len_git_splitter=len(git_splitter)
|
len_git_splitter = len(git_splitter)
|
||||||
|
|
||||||
def git_diff_get_filename(sLine):
|
def git_diff_get_filename(sLine):
|
||||||
return diff_git_filename.match(sLine)
|
return diff_git_filename.match(sLine)
|
||||||
|
@ -220,9 +220,9 @@ def git_diff_get_filename(sLine):
|
||||||
# We keep this information in a hash table for a quick access later.
|
# We keep this information in a hash table for a quick access later.
|
||||||
#
|
#
|
||||||
def load_patch_info(patch_file):
|
def load_patch_info(patch_file):
|
||||||
patch={}
|
patch = {}
|
||||||
line_counter= 0
|
line_counter = 0
|
||||||
initial_number= 0
|
initial_number = 0
|
||||||
index_statement = False # Set true if we see "Index:".
|
index_statement = False # Set true if we see "Index:".
|
||||||
try: hPatch = open(patch_file, 'r')
|
try: hPatch = open(patch_file, 'r')
|
||||||
except:
|
except:
|
||||||
|
@ -233,12 +233,12 @@ def load_patch_info(patch_file):
|
||||||
|
|
||||||
sLine = hPatch.readline()
|
sLine = hPatch.readline()
|
||||||
#Heuristic to determine if it's a svn diff, git diff, or a GNU diff.
|
#Heuristic to determine if it's a svn diff, git diff, or a GNU diff.
|
||||||
if (is_svn_diff(sLine)):
|
if is_svn_diff(sLine):
|
||||||
fn_get_filename=svn_diff_get_filename
|
fn_get_filename = svn_diff_get_filename
|
||||||
elif (is_git_diff(sLine)):
|
elif is_git_diff(sLine):
|
||||||
fn_get_filename=git_diff_get_filename
|
fn_get_filename = git_diff_get_filename
|
||||||
elif (is_gnu_diff(sLine)):
|
elif is_gnu_diff(sLine):
|
||||||
fn_get_filename=gnu_diff_get_filename
|
fn_get_filename = gnu_diff_get_filename
|
||||||
else:
|
else:
|
||||||
print "Error: Unrecognized patch format"
|
print "Error: Unrecognized patch format"
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
@ -248,25 +248,25 @@ def load_patch_info(patch_file):
|
||||||
# This is really a sequence of if ... elsif ... elsif..., but
|
# This is really a sequence of if ... elsif ... elsif..., but
|
||||||
# because Python forbids '=' in conditions, we do it this way.
|
# because Python forbids '=' in conditions, we do it this way.
|
||||||
filename_match = fn_get_filename(sLine)
|
filename_match = fn_get_filename(sLine)
|
||||||
if (filename_match):
|
if filename_match:
|
||||||
patched_filename = string.strip(filename_match.group('filename'))
|
patched_filename = string.strip(filename_match.group('filename'))
|
||||||
if (patched_file in patch):
|
if patched_file in patch:
|
||||||
error("filename occurs more than once in the patch: %s" %
|
error("filename occurs more than once in the patch: %s" %
|
||||||
patched_filename)
|
patched_filename)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
patch[patched_filename] = {}
|
patch[patched_filename] = {}
|
||||||
else:
|
else:
|
||||||
hunk_match = diff_hunk.match(sLine)
|
hunk_match = diff_hunk.match(sLine)
|
||||||
if (hunk_match):
|
if hunk_match:
|
||||||
if (patched_filename == ""):
|
if patched_filename == "":
|
||||||
error("wrong type of patch file : we have a line number without having seen a filename")
|
error("wrong type of patch file : we have a line number without having seen a filename")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
initial_number= hunk_match.group('linenumber')
|
initial_number = hunk_match.group('linenumber')
|
||||||
line_counter= 0
|
line_counter = 0
|
||||||
else:
|
else:
|
||||||
line_added_match = diff_line_added.match(sLine)
|
line_added_match = diff_line_added.match(sLine)
|
||||||
if (line_added_match):
|
if line_added_match:
|
||||||
line_added = line_counter + int(initial_number)
|
line_added = line_counter + int(initial_number)
|
||||||
patch[patched_filename][line_added] = True
|
patch[patched_filename][line_added] = True
|
||||||
# Let's also warn about the lines above and below this one,
|
# Let's also warn about the lines above and below this one,
|
||||||
|
@ -278,26 +278,25 @@ def load_patch_info(patch_file):
|
||||||
line_counter += 1
|
line_counter += 1
|
||||||
else:
|
else:
|
||||||
line_del_match = diff_line_del.match(sLine)
|
line_del_match = diff_line_del.match(sLine)
|
||||||
if (line_del_match == None):
|
if line_del_match == None:
|
||||||
line_counter += 1
|
line_counter += 1
|
||||||
|
|
||||||
sLine = hPatch.readline()
|
sLine = hPatch.readline()
|
||||||
if (sLine == ''): break # Done reading.
|
if sLine == '': break # Done reading.
|
||||||
|
|
||||||
return patch
|
return patch
|
||||||
|
|
||||||
|
|
||||||
def htmlize(s):
|
def htmlize(s):
|
||||||
# Take s, and return legal (UTF-8) HTML.
|
# Take s, and return legal (UTF-8) HTML.
|
||||||
s1 = string.replace(s,"&","&")
|
s1 = string.replace(s, "&", "&")
|
||||||
s2 = string.replace(s1,"<","<")
|
s2 = string.replace(s1, "<", "<")
|
||||||
s3 = string.replace(s2,">",">")
|
s3 = string.replace(s2, ">", ">")
|
||||||
return s3
|
return s3
|
||||||
|
|
||||||
def h(s):
|
def h(s):
|
||||||
# htmlize s if we're generating html, otherwise just return s.
|
# htmlize s if we're generating html, otherwise just return s.
|
||||||
if output_format: return htmlize(s)
|
if output_format: return htmlize(s)
|
||||||
else: return s
|
else: return s
|
||||||
|
|
||||||
def print_multi_line(text):
|
def print_multi_line(text):
|
||||||
# Print text as multiple indented lines.
|
# Print text as multiple indented lines.
|
||||||
|
@ -393,8 +392,9 @@ class Hit:
|
||||||
# Show as CSV format
|
# Show as CSV format
|
||||||
def show_csv(self):
|
def show_csv(self):
|
||||||
csv_writer.writerow([self.filename, self.line, self.column, self.level,
|
csv_writer.writerow([self.filename, self.line, self.column, self.level,
|
||||||
self.category, self.name, self.warning,
|
self.category, self.name, self.warning,
|
||||||
self.suggestion, self.note, self.cwes(), self.context_text ])
|
self.suggestion, self.note, self.cwes(),
|
||||||
|
self.context_text ])
|
||||||
|
|
||||||
def show(self):
|
def show(self):
|
||||||
if csv_output:
|
if csv_output:
|
||||||
|
|
7
pylintrc
7
pylintrc
|
@ -191,7 +191,8 @@ indent-after-paren=4
|
||||||
|
|
||||||
# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1
|
# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1
|
||||||
# tab).
|
# tab).
|
||||||
indent-string=' '
|
# Flawfinder specific: We use 2-space indents, not 4-space
|
||||||
|
indent-string=' '
|
||||||
|
|
||||||
# Maximum number of characters on a single line.
|
# Maximum number of characters on a single line.
|
||||||
max-line-length=100
|
max-line-length=100
|
||||||
|
@ -224,7 +225,9 @@ logging-modules=logging
|
||||||
[MISCELLANEOUS]
|
[MISCELLANEOUS]
|
||||||
|
|
||||||
# List of note tags to take in consideration, separated by a comma.
|
# List of note tags to take in consideration, separated by a comma.
|
||||||
notes=FIXME,XXX,TODO
|
# notes=FIXME,XXX,TODO
|
||||||
|
# Flawfinder specifics: We already note them, no need to report
|
||||||
|
notes=
|
||||||
|
|
||||||
|
|
||||||
[SIMILARITIES]
|
[SIMILARITIES]
|
||||||
|
|
Loading…
Reference in New Issue