2019-03-27 02:07:55 +01:00
|
|
|
#!/usr/bin/python
|
2019-02-27 14:14:19 +01:00
|
|
|
|
|
|
|
"""
|
2019-02-28 11:09:10 +01:00
|
|
|
Best-practices tracker for Tor source code.
|
2019-02-27 14:14:19 +01:00
|
|
|
|
|
|
|
Go through the various .c files and collect metrics about them. If the metrics
|
|
|
|
violate some of our best practices and they are not found in the optional
|
2019-02-28 11:09:10 +01:00
|
|
|
exceptions file, then log a problem about them.
|
2019-02-27 14:14:19 +01:00
|
|
|
|
2019-08-21 16:00:05 +02:00
|
|
|
We currently do metrics about file size, function size and number of includes,
|
2019-08-29 15:20:27 +02:00
|
|
|
for C source files and headers.
|
2019-02-27 14:14:19 +01:00
|
|
|
|
2019-02-28 11:09:10 +01:00
|
|
|
practracker.py should be run with its second argument pointing to the Tor
|
|
|
|
top-level source directory like this:
|
|
|
|
$ python3 ./scripts/maint/practracker/practracker.py .
|
|
|
|
|
2019-03-26 13:42:14 +01:00
|
|
|
To regenerate the exceptions file so that it allows all current
|
|
|
|
problems in the Tor source, use the --regen flag:
|
|
|
|
$ python3 --regen ./scripts/maint/practracker/practracker.py .
|
2019-02-27 14:14:19 +01:00
|
|
|
"""
|
|
|
|
|
2019-03-14 01:15:32 +01:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2019-02-27 14:14:19 +01:00
|
|
|
import os, sys
|
|
|
|
|
|
|
|
import metrics
|
|
|
|
import util
|
2019-02-27 17:24:10 +01:00
|
|
|
import problem
|
2019-08-05 23:17:50 +02:00
|
|
|
import includes
|
2019-02-27 14:14:19 +01:00
|
|
|
|
2019-02-28 11:09:10 +01:00
|
|
|
# The filename of the exceptions file (it should be placed in the practracker directory)
|
|
|
|
EXCEPTIONS_FNAME = "./exceptions.txt"
|
2019-02-27 14:14:19 +01:00
|
|
|
|
|
|
|
# Recommended file size
|
|
|
|
MAX_FILE_SIZE = 3000 # lines
|
|
|
|
# Recommended function size
|
|
|
|
MAX_FUNCTION_SIZE = 100 # lines
|
|
|
|
# Recommended number of #includes
|
|
|
|
MAX_INCLUDE_COUNT = 50
|
2019-08-05 16:31:02 +02:00
|
|
|
# Recommended file size for headers
|
|
|
|
MAX_H_FILE_SIZE = 500
|
|
|
|
# Recommended include count for headers
|
|
|
|
MAX_H_INCLUDE_COUNT = 15
|
2019-08-05 23:17:50 +02:00
|
|
|
# Recommended number of dependency violations
|
|
|
|
MAX_DEP_VIOLATIONS = 0
|
2019-02-27 14:14:19 +01:00
|
|
|
|
2019-07-17 15:20:58 +02:00
|
|
|
# Map from problem type to functions that adjust for tolerance
|
|
|
|
TOLERANCE_FNS = {
|
|
|
|
'include-count': lambda n: int(n*1.1),
|
|
|
|
'function-size': lambda n: int(n*1.1),
|
2019-08-05 23:17:50 +02:00
|
|
|
'file-size': lambda n: int(n*1.02),
|
|
|
|
'dependency-violation': lambda n: (n+2)
|
2019-07-17 15:20:58 +02:00
|
|
|
}
|
|
|
|
|
2019-02-27 14:14:19 +01:00
|
|
|
#######################################################
|
|
|
|
|
2019-02-28 11:09:10 +01:00
|
|
|
# The Tor source code topdir
|
|
|
|
TOR_TOPDIR = None
|
|
|
|
|
2019-02-27 14:14:19 +01:00
|
|
|
#######################################################
|
|
|
|
|
2019-03-22 23:45:06 +01:00
|
|
|
if sys.version_info[0] <= 2:
|
|
|
|
def open_file(fname):
|
|
|
|
return open(fname, 'r')
|
|
|
|
else:
|
|
|
|
def open_file(fname):
|
|
|
|
return open(fname, 'r', encoding='utf-8')
|
|
|
|
|
2019-02-27 17:24:10 +01:00
|
|
|
def consider_file_size(fname, f):
|
2019-07-30 15:20:08 +02:00
|
|
|
"""Consider the size of 'f' and yield an FileSizeItem for it.
|
|
|
|
"""
|
2019-02-27 18:30:39 +01:00
|
|
|
file_size = metrics.get_file_len(f)
|
2019-07-30 15:20:08 +02:00
|
|
|
yield problem.FileSizeItem(fname, file_size)
|
2019-02-27 14:14:19 +01:00
|
|
|
|
2019-02-27 17:24:10 +01:00
|
|
|
def consider_includes(fname, f):
|
2019-07-30 15:20:08 +02:00
|
|
|
"""Consider the #include count in for 'f' and yield an IncludeCountItem
|
|
|
|
for it.
|
|
|
|
"""
|
2019-02-27 16:05:00 +01:00
|
|
|
include_count = metrics.get_include_count(f)
|
2019-02-27 14:14:19 +01:00
|
|
|
|
2019-07-30 15:20:08 +02:00
|
|
|
yield problem.IncludeCountItem(fname, include_count)
|
2019-02-27 14:14:19 +01:00
|
|
|
|
2019-02-27 17:24:10 +01:00
|
|
|
def consider_function_size(fname, f):
|
2019-07-30 15:20:08 +02:00
|
|
|
"""yield a FunctionSizeItem for every function in f.
|
|
|
|
"""
|
2019-02-27 18:30:39 +01:00
|
|
|
|
|
|
|
for name, lines in metrics.get_function_lines(f):
|
|
|
|
canonical_function_name = "%s:%s()" % (fname, name)
|
2019-07-30 15:20:08 +02:00
|
|
|
yield problem.FunctionSizeItem(canonical_function_name, lines)
|
2019-02-27 14:14:19 +01:00
|
|
|
|
2019-08-05 23:35:20 +02:00
|
|
|
def consider_include_violations(fname, real_fname, f):
|
|
|
|
n = 0
|
|
|
|
for item in includes.consider_include_rules(real_fname, f):
|
|
|
|
n += 1
|
|
|
|
if n:
|
|
|
|
yield problem.DependencyViolationItem(fname, n)
|
|
|
|
|
|
|
|
|
2019-02-27 14:14:19 +01:00
|
|
|
#######################################################
|
|
|
|
|
2019-02-27 17:24:10 +01:00
|
|
|
def consider_all_metrics(files_list):
|
2019-07-30 15:20:08 +02:00
|
|
|
"""Consider metrics for all files, and yield a sequence of problem.Item
|
|
|
|
object for those issues."""
|
2019-02-27 14:14:19 +01:00
|
|
|
for fname in files_list:
|
2019-03-22 23:45:06 +01:00
|
|
|
with open_file(fname) as f:
|
2019-07-30 15:20:08 +02:00
|
|
|
for item in consider_metrics_for_file(fname, f):
|
|
|
|
yield item
|
2019-02-27 14:14:19 +01:00
|
|
|
|
2019-02-27 17:24:10 +01:00
|
|
|
def consider_metrics_for_file(fname, f):
|
2019-02-27 14:14:19 +01:00
|
|
|
"""
|
2019-07-30 15:20:08 +02:00
|
|
|
Yield a sequence of problem.Item objects for all of the metrics in
|
|
|
|
'f'.
|
2019-02-27 14:14:19 +01:00
|
|
|
"""
|
2019-08-05 23:17:50 +02:00
|
|
|
real_fname = fname
|
2019-02-27 18:30:39 +01:00
|
|
|
# Strip the useless part of the path
|
|
|
|
if fname.startswith(TOR_TOPDIR):
|
|
|
|
fname = fname[len(TOR_TOPDIR):]
|
|
|
|
|
2019-02-27 14:14:19 +01:00
|
|
|
# Get file length
|
2019-07-30 15:20:08 +02:00
|
|
|
for item in consider_file_size(fname, f):
|
|
|
|
yield item
|
2019-02-27 14:14:19 +01:00
|
|
|
|
|
|
|
# Consider number of #includes
|
|
|
|
f.seek(0)
|
2019-07-30 15:20:08 +02:00
|
|
|
for item in consider_includes(fname, f):
|
|
|
|
yield item
|
2019-02-27 14:14:19 +01:00
|
|
|
|
|
|
|
# Get function length
|
|
|
|
f.seek(0)
|
2019-07-30 15:20:08 +02:00
|
|
|
for item in consider_function_size(fname, f):
|
|
|
|
yield item
|
2019-02-27 17:24:10 +01:00
|
|
|
|
2019-08-05 23:31:49 +02:00
|
|
|
# Check for "upward" includes
|
|
|
|
f.seek(0)
|
2019-08-05 23:35:20 +02:00
|
|
|
for item in consider_include_violations(fname, real_fname, f):
|
|
|
|
yield item
|
2019-08-05 23:17:50 +02:00
|
|
|
|
2019-03-25 20:51:48 +01:00
|
|
|
HEADER="""\
|
|
|
|
# Welcome to the exceptions file for Tor's best-practices tracker!
|
|
|
|
#
|
|
|
|
# Each line of this file represents a single violation of Tor's best
|
2019-03-25 21:06:26 +01:00
|
|
|
# practices -- typically, a violation that we had before practracker.py
|
|
|
|
# first existed.
|
2019-03-25 20:51:48 +01:00
|
|
|
#
|
|
|
|
# There are three kinds of problems that we recognize right now:
|
|
|
|
# function-size -- a function of more than {MAX_FUNCTION_SIZE} lines.
|
2019-08-29 15:20:27 +02:00
|
|
|
# file-size -- a .c file of more than {MAX_FILE_SIZE} lines, or a .h
|
2019-08-21 16:00:05 +02:00
|
|
|
# file with more than {MAX_H_FILE_SIZE} lines.
|
2019-08-29 15:20:27 +02:00
|
|
|
# include-count -- a .c file with more than {MAX_INCLUDE_COUNT} #includes,
|
|
|
|
or a .h file with more than {MAX_H_INCLUDE_COUNT} #includes.
|
2019-08-21 16:00:05 +02:00
|
|
|
# dependency-violation -- a file includes a header that it should
|
|
|
|
# not, according to an advisory .may_include file.
|
2019-03-25 20:51:48 +01:00
|
|
|
#
|
|
|
|
# Each line below represents a single exception that practracker should
|
|
|
|
# _ignore_. Each line has four parts:
|
|
|
|
# 1. The word "problem".
|
|
|
|
# 2. The kind of problem.
|
|
|
|
# 3. The location of the problem: either a filename, or a
|
|
|
|
# filename:functionname pair.
|
|
|
|
# 4. The magnitude of the problem to ignore.
|
|
|
|
#
|
|
|
|
# So for example, consider this line:
|
|
|
|
# problem file-size /src/core/or/connection_or.c 3200
|
|
|
|
#
|
|
|
|
# It tells practracker to allow the mentioned file to be up to 3200 lines
|
|
|
|
# long, even though ordinarily it would warn about any file with more than
|
|
|
|
# {MAX_FILE_SIZE} lines.
|
|
|
|
#
|
|
|
|
# You can either edit this file by hand, or regenerate it completely by
|
|
|
|
# running `make practracker-regen`.
|
2019-03-25 21:06:26 +01:00
|
|
|
#
|
|
|
|
# Remember: It is better to fix the problem than to add a new exception!
|
2019-03-25 20:51:48 +01:00
|
|
|
|
|
|
|
""".format(**globals())
|
|
|
|
|
2019-03-25 20:52:43 +01:00
|
|
|
def main(argv):
|
2019-03-25 21:06:26 +01:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
progname = argv[0]
|
|
|
|
parser = argparse.ArgumentParser(prog=progname)
|
|
|
|
parser.add_argument("--regen", action="store_true",
|
|
|
|
help="Regenerate the exceptions file")
|
2019-08-01 15:35:33 +02:00
|
|
|
parser.add_argument("--list-overbroad", action="store_true",
|
2019-07-17 15:06:34 +02:00
|
|
|
help="List over-strict exceptions")
|
2019-03-25 21:06:26 +01:00
|
|
|
parser.add_argument("--exceptions",
|
|
|
|
help="Override the location for the exceptions file")
|
2019-07-17 15:20:58 +02:00
|
|
|
parser.add_argument("--strict", action="store_true",
|
|
|
|
help="Make all warnings into errors")
|
2019-07-30 17:49:50 +02:00
|
|
|
parser.add_argument("--terse", action="store_true",
|
|
|
|
help="Do not emit helpful instructions.")
|
2019-08-05 16:31:02 +02:00
|
|
|
parser.add_argument("--max-h-file-size", default=MAX_H_FILE_SIZE,
|
2019-08-29 15:20:27 +02:00
|
|
|
help="Maximum lines per .h file")
|
2019-08-05 16:31:02 +02:00
|
|
|
parser.add_argument("--max-h-include-count", default=MAX_H_INCLUDE_COUNT,
|
2019-08-29 15:20:27 +02:00
|
|
|
help="Maximum includes per .h file")
|
2019-07-30 17:49:50 +02:00
|
|
|
parser.add_argument("--max-file-size", default=MAX_FILE_SIZE,
|
2019-08-29 15:20:27 +02:00
|
|
|
help="Maximum lines per .c file")
|
2019-07-30 17:49:50 +02:00
|
|
|
parser.add_argument("--max-include-count", default=MAX_INCLUDE_COUNT,
|
2019-08-29 15:20:27 +02:00
|
|
|
help="Maximum includes per .c file")
|
2019-07-30 17:49:50 +02:00
|
|
|
parser.add_argument("--max-function-size", default=MAX_FUNCTION_SIZE,
|
|
|
|
help="Maximum lines per function")
|
2019-08-05 23:17:50 +02:00
|
|
|
parser.add_argument("--max-dependency-violations", default=MAX_DEP_VIOLATIONS,
|
|
|
|
help="Maximum number of dependency violations to allow")
|
2019-09-02 21:31:31 +02:00
|
|
|
parser.add_argument("--include-dir", action="append",
|
|
|
|
default=["src"],
|
|
|
|
help="A directory (under topdir) to search for source")
|
2019-03-25 21:06:26 +01:00
|
|
|
parser.add_argument("topdir", default=".", nargs="?",
|
|
|
|
help="Top-level directory for the tor source")
|
|
|
|
args = parser.parse_args(argv[1:])
|
2019-02-28 11:09:10 +01:00
|
|
|
|
|
|
|
global TOR_TOPDIR
|
2019-03-25 21:06:26 +01:00
|
|
|
TOR_TOPDIR = args.topdir
|
|
|
|
if args.exceptions:
|
|
|
|
exceptions_file = args.exceptions
|
|
|
|
else:
|
|
|
|
exceptions_file = os.path.join(TOR_TOPDIR, "scripts/maint/practracker", EXCEPTIONS_FNAME)
|
2019-02-28 11:09:10 +01:00
|
|
|
|
2019-07-30 17:49:50 +02:00
|
|
|
# 0) Configure our thresholds of "what is a problem actually"
|
|
|
|
filt = problem.ProblemFilter()
|
2019-08-05 16:31:02 +02:00
|
|
|
filt.addThreshold(problem.FileSizeItem("*.c", int(args.max_file_size)))
|
|
|
|
filt.addThreshold(problem.IncludeCountItem("*.c", int(args.max_include_count)))
|
|
|
|
filt.addThreshold(problem.FileSizeItem("*.h", int(args.max_h_file_size)))
|
|
|
|
filt.addThreshold(problem.IncludeCountItem("*.h", int(args.max_h_include_count)))
|
|
|
|
filt.addThreshold(problem.FunctionSizeItem("*.c", int(args.max_function_size)))
|
2019-08-26 18:30:18 +02:00
|
|
|
filt.addThreshold(problem.DependencyViolationItem("*.c", int(args.max_dependency_violations)))
|
|
|
|
filt.addThreshold(problem.DependencyViolationItem("*.h", int(args.max_dependency_violations)))
|
2019-07-30 17:49:50 +02:00
|
|
|
|
2019-09-18 14:49:57 +02:00
|
|
|
if args.list_overbroad and args.regen:
|
|
|
|
print("Cannot use --regen with --list-overbroad",
|
|
|
|
file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2019-02-27 14:14:19 +01:00
|
|
|
# 1) Get all the .c files we care about
|
2019-09-02 21:31:31 +02:00
|
|
|
files_list = util.get_tor_c_files(TOR_TOPDIR, args.include_dir)
|
2019-02-27 14:14:19 +01:00
|
|
|
|
2019-02-27 17:24:10 +01:00
|
|
|
# 2) Initialize problem vault and load an optional exceptions file so that
|
|
|
|
# we don't warn about the past
|
2019-03-25 21:06:26 +01:00
|
|
|
if args.regen:
|
|
|
|
tmpname = exceptions_file + ".tmp"
|
|
|
|
tmpfile = open(tmpname, "w")
|
2019-07-30 17:54:05 +02:00
|
|
|
problem_file = tmpfile
|
2019-08-01 16:25:20 +02:00
|
|
|
problem_file.write(HEADER)
|
2019-03-25 21:06:26 +01:00
|
|
|
ProblemVault = problem.ProblemVault()
|
|
|
|
else:
|
|
|
|
ProblemVault = problem.ProblemVault(exceptions_file)
|
2019-07-30 17:54:05 +02:00
|
|
|
problem_file = sys.stdout
|
2019-02-27 14:14:19 +01:00
|
|
|
|
2019-09-18 14:49:57 +02:00
|
|
|
if args.list_overbroad:
|
|
|
|
# If we're listing overbroad exceptions, don't list problems.
|
|
|
|
problem_file = util.NullFile()
|
|
|
|
|
2019-07-17 15:20:58 +02:00
|
|
|
# 2.1) Adjust the exceptions so that we warn only about small problems,
|
|
|
|
# and produce errors on big ones.
|
2019-08-01 15:35:33 +02:00
|
|
|
if not (args.regen or args.list_overbroad or args.strict):
|
2019-07-17 15:20:58 +02:00
|
|
|
ProblemVault.set_tolerances(TOLERANCE_FNS)
|
|
|
|
|
2019-02-27 17:24:10 +01:00
|
|
|
# 3) Go through all the files and report problems if they are not exceptions
|
2019-07-30 15:20:08 +02:00
|
|
|
found_new_issues = 0
|
2019-07-30 17:49:50 +02:00
|
|
|
for item in filt.filter(consider_all_metrics(files_list)):
|
2019-07-30 16:15:11 +02:00
|
|
|
status = ProblemVault.register_problem(item)
|
|
|
|
if status == problem.STATUS_ERR:
|
2019-07-30 17:54:05 +02:00
|
|
|
print(item, file=problem_file)
|
2019-07-30 15:20:08 +02:00
|
|
|
found_new_issues += 1
|
2019-07-30 16:15:11 +02:00
|
|
|
elif status == problem.STATUS_WARN:
|
2019-07-30 17:54:05 +02:00
|
|
|
# warnings always go to stdout.
|
|
|
|
print("(warning) {}".format(item))
|
2019-02-27 18:30:39 +01:00
|
|
|
|
2019-03-25 21:06:26 +01:00
|
|
|
if args.regen:
|
|
|
|
tmpfile.close()
|
|
|
|
os.rename(tmpname, exceptions_file)
|
|
|
|
sys.exit(0)
|
|
|
|
|
2019-02-28 11:09:10 +01:00
|
|
|
# If new issues were found, try to give out some advice to the developer on how to resolve it.
|
2019-07-30 17:49:50 +02:00
|
|
|
if found_new_issues and not args.regen and not args.terse:
|
2019-03-12 14:32:22 +01:00
|
|
|
new_issues_str = """\
|
2019-07-17 14:09:47 +02:00
|
|
|
FAILURE: practracker found {} new problem(s) in the code: see warnings above.
|
2019-03-12 14:32:22 +01:00
|
|
|
|
|
|
|
Please fix the problems if you can, and update the exceptions file
|
|
|
|
({}) if you can't.
|
|
|
|
|
|
|
|
See doc/HACKING/HelpfulTools.md for more information on using practracker.\
|
2019-07-17 15:28:48 +02:00
|
|
|
|
|
|
|
You can disable this message by setting the TOR_DISABLE_PRACTRACKER environment
|
|
|
|
variable.
|
2019-07-17 14:09:47 +02:00
|
|
|
""".format(found_new_issues, exceptions_file)
|
2019-02-28 11:09:10 +01:00
|
|
|
print(new_issues_str)
|
|
|
|
|
2019-08-01 15:35:33 +02:00
|
|
|
if args.list_overbroad:
|
2019-07-17 15:06:34 +02:00
|
|
|
def k_fn(tup):
|
|
|
|
return tup[0].key()
|
2019-08-01 15:35:33 +02:00
|
|
|
for (ex,p) in sorted(ProblemVault.list_overbroad_exceptions(), key=k_fn):
|
2019-07-17 15:06:34 +02:00
|
|
|
if p is None:
|
|
|
|
print(ex, "->", 0)
|
|
|
|
else:
|
|
|
|
print(ex, "->", p.metric_value)
|
|
|
|
|
2019-02-28 11:09:10 +01:00
|
|
|
sys.exit(found_new_issues)
|
2019-02-27 14:14:19 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2019-08-01 14:40:56 +02:00
|
|
|
if os.environ.get("TOR_DISABLE_PRACTRACKER"):
|
|
|
|
sys.exit(0)
|
2019-03-25 20:52:43 +01:00
|
|
|
main(sys.argv)
|