Upgradeability check:

- Fix bug: the json output was always enabled
- Move output_json to a shared module
pull/348/head
Josselin 5 years ago
parent eb580f1ad9
commit 6f2c8e713f
  1. 28
      slither/__main__.py
  2. 40
      slither/tools/upgradeability/__main__.py
  3. 36
      slither/utils/json_utils.py

@ -19,6 +19,7 @@ from slither.detectors.abstract_detector import (AbstractDetector,
from slither.printers import all_printers
from slither.printers.abstract_printer import AbstractPrinter
from slither.slither import Slither
from slither.utils.json_utils import output_json
from slither.utils.output_capture import StandardOutputCapture
from slither.utils.colors import red, yellow, set_colorization_enabled
from slither.utils.command_line import (output_detectors, output_results_to_markdown,
@ -103,33 +104,6 @@ def process_from_asts(filenames, args, detector_classes, printer_classes):
# endregion
###################################################################################
###################################################################################
# region Output
###################################################################################
###################################################################################
def output_json(filename, error, results):
# Create our encapsulated JSON result.
json_result = {
"success": error is None,
"error": error,
"results": results
}
# Determine if we should output to stdout
if filename is None:
# Write json to console
print(json.dumps(json_result))
else:
# Write json to file
if os.path.isfile(filename):
logger.info(yellow(f'{filename} exists already, the overwrite is prevented'))
else:
with open(filename, 'w', encoding='utf8') as f:
json.dump(json_result, f, indent=2)
# endregion
###################################################################################

@ -1,19 +1,16 @@
import logging
import argparse
import sys
import json
import os
from slither import Slither
from slither.utils.colors import red, yellow, set_colorization_enabled
from crytic_compile import cryticparser
from slither.exceptions import SlitherException
from slither.utils.json_utils import output_json
from .compare_variables_order import compare_variables_order_implementation, compare_variables_order_proxy
from .compare_function_ids import compare_function_ids
from .check_initialization import check_initialization
from collections import OrderedDict
logging.basicConfig()
logger = logging.getLogger("Slither-check-upgradeability")
@ -47,36 +44,7 @@ def parse_args():
return parser.parse_args()
###################################################################################
###################################################################################
# region Output
###################################################################################
###################################################################################
def output_json(filename, error, results):
# Create our encapsulated JSON result.
json_result = {
"success": error == None,
"error": error,
"results": {
"upgradeability-check": results
}
}
# Determine if we should output to stdout
if filename is None:
# Write json to console
print(json.dumps(json_result))
else:
# Write json to file
if os.path.isfile(filename):
logger.info(yellow(f'{filename} exists already, the overwrite is prevented'))
else:
with open(filename, 'w', encoding='utf8') as f:
json.dump(json_result, f, indent=2)
# endregion
###################################################################################
###################################################################################
# region Main
@ -98,8 +66,6 @@ def main():
# Define some variables for potential JSON output
json_results = {}
output_error = ''
outputting_json = args.json is not None
outputting_json_stdout = args.json == '-'
json_results['check-initialization'] = check_initialization(v1)
@ -133,8 +99,8 @@ def main():
output_error = None
# If we are outputting JSON, capture the redirected output and disable the redirect to output the final JSON.
if outputting_json:
output_json(None if outputting_json_stdout else args.json, output_error, json_results)
if args.json:
output_json(args.json, output_error, {"upgradeability-check": json_results})
# endregion

@ -0,0 +1,36 @@
import os
import json
import logging
from slither.utils.colors import yellow
logger = logging.getLogger("Slither")
def output_json(filename, error, results):
"""
:param filename: Filename where the json will be written. If None or "-", write to stdout
:param error: Error to report
:param results: Results to report
:param logger: Logger where to log potential info
:return:
"""
# Create our encapsulated JSON result.
json_result = {
"success": error is None,
"error": error,
"results": results
}
if filename == "-":
filename = None
# Determine if we should output to stdout
if filename is None:
# Write json to console
print(json.dumps(json_result))
else:
# Write json to file
if os.path.isfile(filename):
logger.info(yellow(f'{filename} exists already, the overwrite is prevented'))
else:
with open(filename, 'w', encoding='utf8') as f:
json.dump(json_result, f, indent=2)
Loading…
Cancel
Save