From adba3886828e92f49cd84e60f14b07bdabbef42b Mon Sep 17 00:00:00 2001 From: Mikko Ohtamaa Date: Fri, 29 Jul 2016 17:53:34 -0500 Subject: [PATCH 1/3] Fail gracefully if solc doesn't find any contracts. Information how to install solc. Watch out for py.test / tox caches. --- .gitignore | 3 +++ README.md | 4 +++- solc/exceptions.py | 4 ++++ solc/main.py | 11 ++++++++++- tests/compilation/test_compile_empty.py | 17 +++++++++++++++++ 5 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 tests/compilation/test_compile_empty.py diff --git a/.gitignore b/.gitignore index 4763086..46b4938 100644 --- a/.gitignore +++ b/.gitignore @@ -49,3 +49,6 @@ docs/_build # Known Contracts **/known_contracts.json + +# py.test cache +.cache diff --git a/README.md b/README.md index d9b78c0..fa09c88 100644 --- a/README.md +++ b/README.md @@ -5,13 +5,15 @@ [![PyPi downloads](https://pypip.in/d/py-solc/badge.png)](https://pypi.python.org/pypi/py-solc) -Python wrapper around the `solc` solidity compiler. +Python wrapper around the `solc` Solidity compiler. # Dependency This library requires the `solc` executable to be present. +solc 0.3.5 or newer is required. [solc installation instructions](http://solidity.readthedocs.io/en/latest/installing-solidity.html) + # Quickstart diff --git a/solc/exceptions.py b/solc/exceptions.py index 08993f8..1fb91d0 100644 --- a/solc/exceptions.py +++ b/solc/exceptions.py @@ -4,3 +4,7 @@ class SolcError(Exception): class CompileError(Exception): pass + + +class ContractsNotFound(Exception): + """No contracts was found in the target folder.""" \ No newline at end of file diff --git a/solc/main.py b/solc/main.py index d434034..5793ecb 100644 --- a/solc/main.py +++ b/solc/main.py @@ -6,6 +6,7 @@ from .exceptions import ( SolcError, + ContractsNotFound, ) from .utils.formatting import ( @@ -39,7 +40,15 @@ def get_solc_version(): def _parse_compiler_output(stdoutdata): - contracts = json.loads(stdoutdata)['contracts'] + + output = json.loads(stdoutdata) + + if not "contracts" in output: + # {'sources': {}, 'version': 'xxx'} + # solc did not pick up any contracts + raise ContractsNotFound(output) + + contracts = output['contracts'] for _, data in contracts.items(): data['abi'] = json.loads(data['abi']) diff --git a/tests/compilation/test_compile_empty.py b/tests/compilation/test_compile_empty.py new file mode 100644 index 0000000..2282e76 --- /dev/null +++ b/tests/compilation/test_compile_empty.py @@ -0,0 +1,17 @@ +import tempfile + +import pytest + +from solc import ( + compile_files, +) + +from solc.exceptions import ContractsNotFound + + +def test_compile_empty_folder(): + """Execute compile on a folder without contracts.""" + + with tempfile.TemporaryDirectory() as tmpdirname: + with pytest.raises(ContractsNotFound): + compile_files(tmpdirname) From 2b8adba04f23f408a53baed1b6775d57141448ff Mon Sep 17 00:00:00 2001 From: Mikko Ohtamaa Date: Sun, 31 Jul 2016 12:31:13 -0500 Subject: [PATCH 2/3] Cleaned up flake8 --- solc/exceptions.py | 2 +- solc/main.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/solc/exceptions.py b/solc/exceptions.py index 1fb91d0..216b80d 100644 --- a/solc/exceptions.py +++ b/solc/exceptions.py @@ -7,4 +7,4 @@ class CompileError(Exception): class ContractsNotFound(Exception): - """No contracts was found in the target folder.""" \ No newline at end of file + """No contracts was found in the target folder.""" diff --git a/solc/main.py b/solc/main.py index 5793ecb..835e22c 100644 --- a/solc/main.py +++ b/solc/main.py @@ -43,7 +43,7 @@ def _parse_compiler_output(stdoutdata): output = json.loads(stdoutdata) - if not "contracts" in output: + if "contracts" not in output: # {'sources': {}, 'version': 'xxx'} # solc did not pick up any contracts raise ContractsNotFound(output) From cae8d2996c476c4d82d80a0fc1d81e33aebabd79 Mon Sep 17 00:00:00 2001 From: Mikko Ohtamaa Date: Sun, 31 Jul 2016 12:34:54 -0500 Subject: [PATCH 3/3] Python 2.7 compatibilit --- tests/compilation/test_compile_empty.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/compilation/test_compile_empty.py b/tests/compilation/test_compile_empty.py index 2282e76..679bae7 100644 --- a/tests/compilation/test_compile_empty.py +++ b/tests/compilation/test_compile_empty.py @@ -1,3 +1,4 @@ +import os import tempfile import pytest @@ -12,6 +13,9 @@ def test_compile_empty_folder(): """Execute compile on a folder without contracts.""" - with tempfile.TemporaryDirectory() as tmpdirname: + tmpdirname = tempfile.mkdtemp() + try: with pytest.raises(ContractsNotFound): compile_files(tmpdirname) + finally: + os.rmdir(tmpdirname)