Factorise config parsing + add config parsing to slither-format

pull/238/head
Josselin 6 years ago
parent 4330b9421a
commit bb57e940c3
  1. 41
      slither/__main__.py
  2. 46
      slither/utils/command_line.py
  3. 17
      utils/slither_format/__main__.py

@ -6,7 +6,6 @@ import inspect
import json import json
import logging import logging
import os import os
import subprocess
import sys import sys
import traceback import traceback
@ -22,7 +21,8 @@ from slither.slither import Slither
from slither.utils.colors import red, yellow, set_colorization_enabled from slither.utils.colors import red, yellow, set_colorization_enabled
from slither.utils.command_line import (output_detectors, output_results_to_markdown, from slither.utils.command_line import (output_detectors, output_results_to_markdown,
output_detectors_json, output_printers, output_detectors_json, output_printers,
output_to_markdown, output_wiki) output_to_markdown, output_wiki, defaults_flag_in_config,
read_config_file)
from crytic_compile import is_supported from crytic_compile import is_supported
from slither.exceptions import SlitherException from slither.exceptions import SlitherException
@ -253,30 +253,6 @@ def parse_filter_paths(args):
return args.filter_paths.split(',') return args.filter_paths.split(',')
return [] return []
# Those are the flags shared by the command line and the config file
defaults_flag_in_config = {
'detectors_to_run': 'all',
'printers_to_run': None,
'detectors_to_exclude': None,
'exclude_informational': False,
'exclude_low': False,
'exclude_medium': False,
'exclude_high': False,
'solc': 'solc',
'solc_args': None,
'disable_solc_warnings': False,
'json': None,
'truffle_version': None,
'disable_color': False,
'filter_paths': None,
'truffle_ignore_compile': False,
'truffle_build_directory': 'build/contracts',
'embark_ignore_compile': False,
'embark_overwrite_config': False,
# debug command
'legacy_ast': False,
'ignore_return_value': False
}
def parse_args(detector_classes, printer_classes): def parse_args(detector_classes, printer_classes):
parser = argparse.ArgumentParser(description='Slither. For usage information, see https://github.com/crytic/slither/wiki/Usage', parser = argparse.ArgumentParser(description='Slither. For usage information, see https://github.com/crytic/slither/wiki/Usage',
@ -435,18 +411,7 @@ def parse_args(detector_classes, printer_classes):
args = parser.parse_args() args = parser.parse_args()
if os.path.isfile(args.config_file): read_config_file(args)
try:
with open(args.config_file) as f:
config = json.load(f)
for key, elem in config.items():
if key not in defaults_flag_in_config:
logger.info(yellow('{} has an unknown key: {} : {}'.format(args.config_file, key, elem)))
continue
if getattr(args, key) == defaults_flag_in_config[key]:
setattr(args, key, elem)
except json.decoder.JSONDecodeError as e:
logger.error(red('Impossible to read {}, please check the file {}'.format(args.config_file, e)))
return args return args

@ -1,9 +1,53 @@
import json import json
import os
import logging
from collections import defaultdict from collections import defaultdict
from prettytable import PrettyTable from prettytable import PrettyTable
from .colors import yellow, red
from slither.detectors.abstract_detector import classification_txt from slither.detectors.abstract_detector import classification_txt
logger = logging.getLogger("Slither")
# Those are the flags shared by the command line and the config file
defaults_flag_in_config = {
'detectors_to_run': 'all',
'printers_to_run': None,
'detectors_to_exclude': None,
'exclude_informational': False,
'exclude_low': False,
'exclude_medium': False,
'exclude_high': False,
'solc': 'solc',
'solc_args': None,
'disable_solc_warnings': False,
'json': None,
'truffle_version': None,
'disable_color': False,
'filter_paths': None,
'truffle_ignore_compile': False,
'truffle_build_directory': 'build/contracts',
'embark_ignore_compile': False,
'embark_overwrite_config': False,
# debug command
'legacy_ast': False,
'ignore_return_value': False
}
def read_config_file(args):
if os.path.isfile(args.config_file):
try:
with open(args.config_file) as f:
config = json.load(f)
for key, elem in config.items():
if key not in defaults_flag_in_config:
logger.info(yellow('{} has an unknown key: {} : {}'.format(args.config_file, key, elem)))
continue
if getattr(args, key) == defaults_flag_in_config[key]:
setattr(args, key, elem)
except json.decoder.JSONDecodeError as e:
logger.error(red('Impossible to read {}, please check the file {}'.format(args.config_file, e)))
def output_to_markdown(detector_classes, printer_classes, filter_wiki): def output_to_markdown(detector_classes, printer_classes, filter_wiki):
def extract_help(cls): def extract_help(cls):

@ -1,13 +1,13 @@
import os, sys import sys
import argparse import argparse
from slither import Slither from slither import Slither
from slither.utils.colors import red from slither.utils.command_line import read_config_file
import logging import logging
from .slither_format import slither_format from .slither_format import slither_format
from crytic_compile import cryticparser from crytic_compile import cryticparser
logging.basicConfig() logging.basicConfig()
logging.getLogger("Slither").setLevel(logging.INFO) logger = logging.getLogger("Slither").setLevel(logging.INFO)
# Slither detectors for which slither-format currently works # Slither detectors for which slither-format currently works
available_detectors = ["unused-state", available_detectors = ["unused-state",
@ -41,6 +41,14 @@ def parse_args():
action='store_true', action='store_true',
default=False) default=False)
parser.add_argument('--config-file',
help='Provide a config file (default: slither.config.json)',
action='store',
dest='config_file',
default='slither.config.json')
group_detector = parser.add_argument_group('Detectors') group_detector = parser.add_argument_group('Detectors')
group_detector.add_argument('--detect', group_detector.add_argument('--detect',
help='Comma-separated list of detectors, defaults to all, ' help='Comma-separated list of detectors, defaults to all, '
@ -75,6 +83,9 @@ def main():
# Parse all arguments # Parse all arguments
args = parse_args() args = parse_args()
read_config_file(args)
# Perform slither analysis on the given filename # Perform slither analysis on the given filename
slither = Slither(args.filename, **vars(args)) slither = Slither(args.filename, **vars(args))

Loading…
Cancel
Save