Clean slither/slither.py initialization

pull/172/head
Josselin 6 years ago
parent 3731a8c5a6
commit 63453a6b66
  1. 61
      slither/slither.py

@ -22,7 +22,7 @@ class Slither(SlitherSolc):
def __init__(self, contract, **kwargs):
'''
Args:
contract (str| list(json)
contract (str| list(json))
Keyword Args:
solc (str): solc binary location (default 'solc')
disable_solc_warnings (bool): True to disable solc warnings (default false)
@ -31,15 +31,30 @@ class Slither(SlitherSolc):
is_truffle (bool): is a truffle directory (default false)
paths_to_filter (list(str)): list of path to filter (default [])
'''
solc = kwargs.get('solc', 'solc')
disable_solc_warnings = kwargs.get('disable_solc_warnings', False)
solc_arguments= kwargs.get('solc_arguments', '')
ast_format = kwargs.get('ast_format', '--ast-compact-json')
is_truffle = kwargs.get('is_truffle', False)
paths_to_filter = kwargs.get('paths_to_filter', [])
# truffle directory
if is_truffle:
self._init_from_truffle(contract)
# list of files provided (see --splitted option)
elif isinstance(contract, list):
self._init_from_list(contract)
# .json or .sol provided
else:
self._init_from_solc(contract, **kwargs)
self._detectors = []
self._printers = []
paths_to_filter = kwargs.get('paths_to_filter', [])
for p in paths_to_filter:
self.add_path_to_filter(p)
self._analyze_contracts()
self.load_previous_results()
def _init_from_truffle(self, contract):
if not os.path.isdir(os.path.join(contract, 'build'))\
or not os.path.isdir(os.path.join(contract, 'build', 'contracts')):
logger.info(red('No truffle build directory found, did you run `truffle compile`?'))
@ -56,8 +71,23 @@ class Slither(SlitherSolc):
path = contract_loaded['attributes']['absolutePath']
self._parse_contracts_from_loaded_json(contract_loaded, path)
# list of files provided (see --splitted option)
elif isinstance(contract, list):
def _init_from_solc(self, contract, **kwargs):
solc = kwargs.get('solc', 'solc')
disable_solc_warnings = kwargs.get('disable_solc_warnings', False)
solc_arguments = kwargs.get('solc_arguments', '')
ast_format = kwargs.get('ast_format', '--ast-compact-json')
contracts_json = self._run_solc(contract,
solc,
disable_solc_warnings,
solc_arguments,
ast_format)
super(Slither, self).__init__(contract)
for c in contracts_json:
self._parse_contracts_from_json(c)
def _init_from_list(self, contract):
super(Slither, self).__init__('')
for c in contract:
if 'absolutePath' in c:
@ -65,21 +95,6 @@ class Slither(SlitherSolc):
else:
path = c['attributes']['absolutePath']
self._parse_contracts_from_loaded_json(c, path)
# .json or .sol provided
else:
contracts_json = self._run_solc(contract, solc, disable_solc_warnings, solc_arguments, ast_format)
super(Slither, self).__init__(contract)
for c in contracts_json:
self._parse_contracts_from_json(c)
self._detectors = []
self._printers = []
for p in paths_to_filter:
self.add_path_to_filter(p)
self._analyze_contracts()
self.load_previous_results()
@property
def detectors(self):

Loading…
Cancel
Save