From d50a401cd089620647a4fcd42f2d7b7821a9aed8 Mon Sep 17 00:00:00 2001 From: Jules Villard Date: Mon, 9 Nov 2015 06:04:42 -0800 Subject: [PATCH] replace string constants for json fields with variables Summary: public Now that we have modules, we can gather string constants in one place only. Reviewed By: jeremydubreil Differential Revision: D2615791 fb-gh-sync-id: 7cc21e4 --- infer/lib/python/inferTraceBugs | 42 ++++++++++++++++-------------- infer/lib/python/inferlib/utils.py | 30 +++++++++++++++++++++ scripts/build_integration_tests.py | 32 ++++++++++++++--------- 3 files changed, 72 insertions(+), 32 deletions(-) diff --git a/infer/lib/python/inferTraceBugs b/infer/lib/python/inferTraceBugs index 7d79d0ba4..326649511 100755 --- a/infer/lib/python/inferTraceBugs +++ b/infer/lib/python/inferTraceBugs @@ -59,11 +59,11 @@ base_parser.add_argument('--html', def describe_report(report, indent=0): - filename = report['file'] - kind = report['kind'] - line = report['line'] - error_type = report['type'] - msg = report['qualifier'] + filename = report[utils.JSON_INDEX_FILENAME] + kind = report[utils.JSON_INDEX_KIND] + line = report[utils.JSON_INDEX_LINE] + error_type = report[utils.JSON_INDEX_TYPE] + msg = report[utils.JSON_INDEX_QUALIFIER] return '{0}:{1}: {2}: {3}\n {4}{5}\n'.format( filename, line, @@ -92,27 +92,31 @@ class Tracer(object): pass def build_node(self, node): - if node['level'] > self.max_level: + if node[utils.JSON_INDEX_TRACE_LEVEL] > self.max_level: return - report_line = node['line_number'] - fname = node['filename'] + report_line = node[utils.JSON_INDEX_TRACE_LINE] + fname = node[utils.JSON_INDEX_TRACE_FILENAME] self.indenter.newline() - self.indenter.add('%s:%d: %s' % (fname, - report_line, - node['description'])) + self.indenter.add('%s:%d: %s' % ( + fname, + report_line, + node[utils.JSON_INDEX_TRACE_DESCRIPTION], + )) self.indenter.newline() if not self.args.no_source: - self.indenter.indent_push(node['level']) + self.indenter.indent_push(node[utils.JSON_INDEX_TRACE_LEVEL]) self.indenter.add(utils.build_source_context(fname, report_line)) self.indenter.indent_pop() self.indenter.newline() def build_trace(self, trace): total_nodes = len(trace) - hidden_nodes = len([None for n in trace if n['level'] > self.max_level]) + hidden_nodes = len( + filter(lambda n: n[utils.JSON_INDEX_TRACE_LEVEL] > self.max_level, + trace)) shown_nodes = total_nodes - hidden_nodes hidden_str = '' all_str = 'all ' @@ -127,8 +131,8 @@ class Tracer(object): self.build_node(node) def build_report(self, report): - traces = json.loads(report['trace']) - self.build_trace(traces['trace']) + traces = json.loads(report[utils.JSON_INDEX_TRACE]) + self.build_trace(traces[utils.JSON_INDEX_TRACE]) def __str__(self): return str(self.indenter) @@ -139,8 +143,8 @@ class Selector(object): self.args = args def has_trace(report): - trace = json.loads(report['trace']) - return len(trace['trace']) > 0 + trace = json.loads(report[utils.JSON_INDEX_TRACE]) + return len(trace[utils.JSON_INDEX_TRACE]) > 0 self.reports = [report for report in reports if has_trace(report)] def show_choices(self): @@ -308,8 +312,8 @@ def html_list_of_bugs(args, remote_source_template, selector): def source_uri(report): d = { - 'file-name': report['file'], - 'line-number': report['line'], + 'file-name': report[utils.JSON_INDEX_FILENAME], + 'line-number': report[utils.JSON_INDEX_LINE], } if remote_source_template is not None: link = remote_source_template.format(**d) diff --git a/infer/lib/python/inferlib/utils.py b/infer/lib/python/inferlib/utils.py index 3daeecf39..7bb0b4a4b 100644 --- a/infer/lib/python/inferlib/utils.py +++ b/infer/lib/python/inferlib/utils.py @@ -49,12 +49,42 @@ CSV_REPORT_FILENAME = 'report.csv' JSON_REPORT_FILENAME = 'report.json' BUGS_FILENAME = 'bugs.txt' +# indices in rows of csv reports +CSV_INDEX_CLASS = 0 CSV_INDEX_KIND = 1 CSV_INDEX_TYPE = 2 CSV_INDEX_QUALIFIER = 3 +CSV_INDEX_SEVERITY = 4 CSV_INDEX_LINE = 5 +CSV_INDEX_PROCEDURE = 6 +CSV_INDEX_PROCEDURE_ID = 7 CSV_INDEX_FILENAME = 8 +CSV_INDEX_TRACE = 9 +CSV_INDEX_KEY = 10 CSV_INDEX_QUALIFIER_TAGS = 11 +CSV_INDEX_HASH = 12 +CSV_INDEX_BUG_ID = 13 +CSV_INDEX_ALWAYS_REPORT = 14 +CSV_INDEX_ADVICE = 15 + +# field names in rows of json reports +JSON_INDEX_FILENAME = 'file' +JSON_INDEX_HASH = 'hash' +JSON_INDEX_KIND = 'kind' +JSON_INDEX_LINE = 'line' +JSON_INDEX_PROCEDURE = 'procedure' +JSON_INDEX_QUALIFIER = 'qualifier' +JSON_INDEX_QUALIFIER_TAGS = 'qualifier_tags' +JSON_INDEX_SEVERITY = 'file' +JSON_INDEX_TYPE = 'type' +JSON_INDEX_TRACE = 'trace' +JSON_INDEX_TRACE_LEVEL = 'level' +JSON_INDEX_TRACE_FILENAME = 'filename' +JSON_INDEX_TRACE_LINE = 'line_number' +JSON_INDEX_TRACE_DESCRIPTION = 'description' +JSON_INDEX_TRACE_NODE_TAGS = 'node_tags' +JSON_INDEX_TRACE_NODE_TAGS_TAG = 'tags' +JSON_INDEX_TRACE_NODE_TAGS_VALUE = 'value' QUALIFIER_TAGS = 'qualifier_tags' BUCKET_TAGS = 'bucket' diff --git a/scripts/build_integration_tests.py b/scripts/build_integration_tests.py index 7b886c04f..bbdadb399 100755 --- a/scripts/build_integration_tests.py +++ b/scripts/build_integration_tests.py @@ -14,6 +14,13 @@ import sys import tempfile import unittest +SCRIPTS_DIRECTORY = os.path.dirname(os.path.realpath(__file__)) +sys.path.insert(0, + os.path.join(SCRIPTS_DIRECTORY, + os.pardir, 'infer', 'lib', 'python')) + +from inferlib import utils + CURRENT_DIR = os.getcwd() REPORT_JSON = 'report.json' @@ -22,15 +29,10 @@ INFER = 'infer' RECORD_ENV = 'INFER_RECORD_INTEGRATION_TESTS' -FILE = 'file' -HASH = 'hash' -LINE = 'line' -PROCEDURE = 'procedure' -TYPE = 'type' REPORT_FIELDS = [ - FILE, - PROCEDURE, - TYPE, + utils.JSON_INDEX_FILENAME, + utils.JSON_INDEX_PROCEDURE, + utils.JSON_INDEX_TYPE, ] @@ -39,14 +41,18 @@ def should_record_tests(): def quote(s): - return '\"{}\"'.format(s) + return '\"%s\"' % s def string_of_error(e): - msg = e[TYPE] + ' in file ' + quote(e[FILE]) - msg += ', procedure ' + quote(e[PROCEDURE]) - if LINE in e: - msg += ' on line ' + e[LINE] + if utils.JSON_INDEX_LINE in e: + line = ' on line %s ' % e[utils.JSON_INDEX_LINE] + msg = '%s in file %s, procedure %s%s' % ( + e[utils.JSON_INDEX_TYPE], + quote(e[utils.JSON_INDEX_FILE]), + quote(e[utils.JSON_INDEX_PROCEDURE]), + line, + ) return msg