Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit c16304e

Browse files
committed
Add option for extra_args to solc invocation
Since `compile()` has a `**kwargs` at the end we can easily add an optional `extra_args` argument to all the compiler calls. We for example need this if we would like to specify specific locations on the system path where contracts can be imported from.
1 parent 507099b commit c16304e

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

ethereum/_solidity.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ def get_solidity():
4040
return solc_wrapper
4141

4242

43-
def solc_arguments(libraries=None, combined='bin,abi', optimize=True):
43+
def solc_arguments(libraries=None, combined='bin,abi', optimize=True, extra_args=''):
4444
""" Build the arguments to call the solc binary. """
4545
args = [
4646
'--combined-json', combined,
4747
'--add-std',
48+
extra_args
4849
]
4950

5051
if optimize:
@@ -221,7 +222,7 @@ def solidity_unresolved_symbols(hex_code):
221222
return set(re.findall(r"_.{39}", hex_code))
222223

223224

224-
def compile_file(filepath, libraries=None, combined='bin,abi', optimize=True):
225+
def compile_file(filepath, libraries=None, combined='bin,abi', optimize=True, extra_args=''):
225226
""" Return the compile contract code.
226227
227228
Args:
@@ -236,7 +237,7 @@ def compile_file(filepath, libraries=None, combined='bin,abi', optimize=True):
236237

237238
workdir, filename = os.path.split(filepath)
238239

239-
args = solc_arguments(libraries=libraries, combined=combined, optimize=optimize)
240+
args = solc_arguments(libraries=libraries, combined=combined, optimize=optimize, extra_args=extra_args)
240241
args.insert(0, get_compiler_path())
241242
args.append(filename)
242243

@@ -245,18 +246,19 @@ def compile_file(filepath, libraries=None, combined='bin,abi', optimize=True):
245246
return solc_parse_output(output)
246247

247248

248-
def compile_contract(filepath, contract_name, libraries=None, combined='bin,abi', optimize=True):
249+
def compile_contract(filepath, contract_name, libraries=None, combined='bin,abi', optimize=True, extra_args=''):
249250
all_contracts = compile_file(
250251
filepath,
251252
libraries=libraries,
252253
combined=combined,
253254
optimize=optimize,
255+
extra_args=extra_args
254256
)
255257

256258
return all_contracts[contract_name]
257259

258260

259-
def compile_last_contract(filepath, libraries=None, combined='bin,abi', optimize=True):
261+
def compile_last_contract(filepath, libraries=None, combined='bin,abi', optimize=True, extra_args=''):
260262
with open(filepath) as handler:
261263
all_names = solidity_names(handler.read())
262264

@@ -273,11 +275,12 @@ def compile_last_contract(filepath, libraries=None, combined='bin,abi', optimize
273275
libraries=libraries,
274276
combined=combined,
275277
optimize=optimize,
278+
extra_args=extra_args
276279
)
277280

278281

279-
def compile_code(sourcecode, libraries=None, combined='bin,abi', optimize=True):
280-
args = solc_arguments(libraries=libraries, combined=combined, optimize=optimize)
282+
def compile_code(sourcecode, libraries=None, combined='bin,abi', optimize=True, extra_args=''):
283+
args = solc_arguments(libraries=libraries, combined=combined, optimize=optimize, extra_args=extra_args)
281284
args.insert(0, get_compiler_path())
282285

283286
process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
@@ -297,17 +300,17 @@ class Solc(object):
297300
compiler_version = staticmethod(compiler_version)
298301

299302
@staticmethod
300-
def _code_or_path(sourcecode, path, contract_name, libraries, combined):
303+
def _code_or_path(sourcecode, path, contract_name, libraries, combined, extra_args):
301304
warnings.warn('solc_wrapper is deprecated, please use the functions compile_file or compile_code')
302305

303306
if sourcecode and path:
304307
raise ValueError('sourcecode and path are mutually exclusive.')
305308

306309
if path and contract_name:
307-
return compile_contract(path, contract_name, libraries=libraries, combined=combined)
310+
return compile_contract(path, contract_name, libraries=libraries, combined=combined, extra_args=extra_args)
308311

309312
if path:
310-
return compile_last_contract(path, libraries=libraries, combined=combined)
313+
return compile_last_contract(path, libraries=libraries, combined=combined, extra_args=extra_args)
311314

312315
all_names = solidity_names(sourcecode)
313316
all_contract_names = [
@@ -316,20 +319,20 @@ def _code_or_path(sourcecode, path, contract_name, libraries, combined):
316319
]
317320
last_contract = all_contract_names[-1]
318321

319-
result = compile_code(sourcecode, libraries=libraries, combined=combined)
322+
result = compile_code(sourcecode, libraries=libraries, combined=combined, extra_args=extra_args)
320323
return result[last_contract]
321324

322325
@classmethod
323-
def compile(cls, code, path=None, libraries=None, contract_name=''):
326+
def compile(cls, code, path=None, libraries=None, contract_name='', extra_args=''):
324327
""" Return the binary of last contract in code. """
325-
result = cls._code_or_path(code, path, contract_name, libraries, 'bin')
328+
result = cls._code_or_path(code, path, contract_name, libraries, 'bin', extra_args)
326329
return result['bin']
327330

328331
@classmethod
329-
def mk_full_signature(cls, code, path=None, libraries=None, contract_name=''):
332+
def mk_full_signature(cls, code, path=None, libraries=None, contract_name='', extra_args=''):
330333
"returns signature of last contract in code"
331334

332-
result = cls._code_or_path(code, path, contract_name, libraries, 'abi')
335+
result = cls._code_or_path(code, path, contract_name, libraries, 'abi', extra_args)
333336
return result['abi']
334337

335338
@classmethod

0 commit comments

Comments
 (0)