solidity/scripts/bytecodecompare/prepare_report.py

42 lines
1.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import sys
import glob
import subprocess
import json
2020-01-15 13:21:33 +00:00
SOLC_BIN = sys.argv[1]
2020-01-23 14:06:47 +00:00
REPORT_FILE = open("report.txt", mode="w", encoding='utf8')
2017-03-22 19:56:44 +00:00
for optimize in [False, True]:
for f in sorted(glob.glob("*.sol")):
sources = {}
2020-01-23 14:06:47 +00:00
sources[f] = {'content': open(f, mode='r', encoding='utf8').read()}
2020-01-15 13:21:33 +00:00
input_json = {
'language': 'Solidity',
'sources': sources,
'settings': {
'optimizer': {
'enabled': optimize
},
2020-01-15 13:21:33 +00:00
'outputSelection': {'*': {'*': ['evm.bytecode.object', 'metadata']}}
}
}
2020-01-15 13:21:33 +00:00
args = [SOLC_BIN, '--standard-json']
2017-03-22 19:56:44 +00:00
if optimize:
args += ['--optimize']
proc = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = proc.communicate(json.dumps(input_json).encode('utf-8'))
2017-03-22 19:56:44 +00:00
try:
result = json.loads(out.decode('utf-8').strip())
for filename in sorted(result['contracts'].keys()):
for contractName in sorted(result['contracts'][filename].keys()):
contractData = result['contracts'][filename][contractName]
if 'evm' in contractData and 'bytecode' in contractData['evm']:
2020-01-15 13:21:33 +00:00
REPORT_FILE.write(filename + ':' + contractName + ' ' + contractData['evm']['bytecode']['object'] + '\n')
else:
2020-01-15 13:21:33 +00:00
REPORT_FILE.write(filename + ':' + contractName + ' NO BYTECODE\n')
REPORT_FILE.write(filename + ':' + contractName + ' ' + contractData['metadata'] + '\n')
except KeyError:
2020-01-15 13:21:33 +00:00
REPORT_FILE.write(f + ": ERROR\n")