-
Notifications
You must be signed in to change notification settings - Fork 232
/
main.py
197 lines (156 loc) · 5.42 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
from __future__ import absolute_import
import functools
import json
import re
from .exceptions import (
SolcError,
ContractsNotFound,
)
from .utils.filesystem import (
is_executable_available,
)
from .wrapper import (
get_solc_binary_path,
solc_wrapper,
)
import semantic_version
VERSION_DEV_DATE_MANGLER_RE = re.compile(r'(\d{4})\.0?(\d{1,2})\.0?(\d{1,2})')
strip_zeroes_from_month_and_day = functools.partial(VERSION_DEV_DATE_MANGLER_RE.sub,
r'\g<1>.\g<2>.\g<3>')
def is_solc_available():
solc_binary = get_solc_binary_path()
return is_executable_available(solc_binary)
def get_solc_version_string(**kwargs):
kwargs['version'] = True
stdoutdata, stderrdata, command, proc = solc_wrapper(**kwargs)
_, _, version_string = stdoutdata.partition('\n')
if not version_string or not version_string.startswith('Version: '):
raise SolcError(
command=command,
return_code=proc.returncode,
stdin_data=None,
stdout_data=stdoutdata,
stderr_data=stderrdata,
message="Unable to extract version string from command output",
)
return version_string
def get_solc_version(**kwargs):
# semantic_version as of 2017-5-5 expects only one + to be used in string
return semantic_version.Version(
strip_zeroes_from_month_and_day(
get_solc_version_string(**kwargs)
[len('Version: '):]
.replace('++', 'pp')))
def solc_supports_standard_json_interface(**kwargs):
return get_solc_version() in semantic_version.Spec('>=0.4.11')
def _parse_compiler_output(stdoutdata):
output = json.loads(stdoutdata)
if "contracts" not in output:
return {}
contracts = output['contracts']
sources = output['sources']
for source, data in contracts.items():
data['abi'] = json.loads(data['abi'])
data['ast'] = sources[source.split(':')[0]]['AST']
return contracts
ALL_OUTPUT_VALUES = (
"abi",
"asm",
"ast",
"bin",
"bin-runtime",
"clone-bin",
"devdoc",
"interface",
"opcodes",
"userdoc",
)
def compile_source(source,
allow_empty=False,
output_values=ALL_OUTPUT_VALUES,
**kwargs):
if 'stdin' in kwargs:
raise ValueError(
"The `stdin` keyword is not allowed in the `compile_source` function"
)
if 'combined_json' in kwargs:
raise ValueError(
"The `combined_json` keyword is not allowed in the `compile_source` function"
)
combined_json = ','.join(output_values)
compiler_kwargs = dict(stdin=source, combined_json=combined_json, **kwargs)
stdoutdata, stderrdata, command, proc = solc_wrapper(**compiler_kwargs)
contracts = _parse_compiler_output(stdoutdata)
if not contracts and not allow_empty:
raise ContractsNotFound(
command=command,
return_code=proc.returncode,
stdin_data=source,
stdout_data=stdoutdata,
stderr_data=stderrdata,
)
return contracts
def compile_files(source_files,
allow_empty=False,
output_values=ALL_OUTPUT_VALUES,
**kwargs):
if 'combined_json' in kwargs:
raise ValueError(
"The `combined_json` keyword is not allowed in the `compile_files` function"
)
combined_json = ','.join(output_values)
compiler_kwargs = dict(source_files=source_files, combined_json=combined_json, **kwargs)
stdoutdata, stderrdata, command, proc = solc_wrapper(**compiler_kwargs)
contracts = _parse_compiler_output(stdoutdata)
if not contracts and not allow_empty:
raise ContractsNotFound(
command=command,
return_code=proc.returncode,
stdin_data=None,
stdout_data=stdoutdata,
stderr_data=stderrdata,
)
return contracts
def compile_standard(input_data, allow_empty=False, **kwargs):
if not input_data.get('sources') and not allow_empty:
raise ContractsNotFound(
command=None,
return_code=None,
stdin_data=json.dumps(input_data, sort_keys=True, indent=2),
stdout_data=None,
stderr_data=None,
)
stdoutdata, stderrdata, command, proc = solc_wrapper(
stdin=json.dumps(input_data),
standard_json=True,
**kwargs
)
compiler_output = json.loads(stdoutdata)
if 'errors' in compiler_output:
has_errors = any(error['severity'] == 'error' for error in compiler_output['errors'])
if has_errors:
error_message = "\n".join(tuple(
error['formattedMessage']
for error in compiler_output['errors']
if error['severity'] == 'error'
))
raise SolcError(
command,
proc.returncode,
json.dumps(input_data),
stdoutdata,
stderrdata,
message=error_message,
)
return compiler_output
def link_code(unlinked_data, libraries):
libraries_arg = ','.join((
':'.join((lib_name, lib_address))
for lib_name, lib_address in libraries.items()
))
stdoutdata, stderrdata, _, _ = solc_wrapper(
stdin=unlinked_data,
link=True,
libraries=libraries_arg,
)
return stdoutdata.strip()