Skip to content

Commit

Permalink
Improve error handling (fix #45, fix #62)
Browse files Browse the repository at this point in the history
  • Loading branch information
montyly committed Feb 19, 2020
1 parent 73bf397 commit 708443a
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 39 deletions.
6 changes: 4 additions & 2 deletions crytic_compile/platform/brownie.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ def compile(crytic_compile: "CryticCompile", target: str, **kwargs: Dict):

if not brownie_ignore_compile:
cmd = base_cmd + ["compile"]

process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=target)
try:
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=target)
except OSError as e:
raise InvalidCompilation(e)

stdout_bytes, stderr_bytes = process.communicate()
stdout, stderr = (
Expand Down
7 changes: 4 additions & 3 deletions crytic_compile/platform/dapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
convert_filename,
)


# Handle cycle
if TYPE_CHECKING:
from crytic_compile import CryticCompile


LOGGER = logging.getLogger("CryticCompile")


Expand Down Expand Up @@ -150,7 +148,10 @@ def _run_dapp(target: str):
"""
cmd = ["dapp", "build"]

process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=target)
try:
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=target)
except OSError as e:
raise InvalidCompilation(e)
_, _ = process.communicate()


Expand Down
20 changes: 13 additions & 7 deletions crytic_compile/platform/embark.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ def compile(crytic_compile: "CryticCompile", target: str, **kwargs: str):
embark_json["plugins"][plugin_name] = {"flags": ""}
write_embark_json = True
if write_embark_json:
process = subprocess.Popen(["npm", "install", plugin_name], cwd=target)
try:
process = subprocess.Popen(["npm", "install", plugin_name], cwd=target)
except OSError as e:
raise InvalidCompilation(e)
_, stderr = process.communicate()
with open(os.path.join(target, "embark.json"), "w", encoding="utf8") as outfile:
json.dump(embark_json, outfile, indent=2)
Expand All @@ -59,12 +62,15 @@ def compile(crytic_compile: "CryticCompile", target: str, **kwargs: str):
)

if not embark_ignore_compile:
process = subprocess.Popen(
["embark", "build", "--contracts"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=target,
)
try:
process = subprocess.Popen(
["embark", "build", "--contracts"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=target,
)
except OSError as e:
raise InvalidCompilation(e)
stdout, stderr = process.communicate()
LOGGER.info("%s\n", stdout.decode())
if stderr:
Expand Down
5 changes: 4 additions & 1 deletion crytic_compile/platform/etherlime.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ def compile(crytic_compile: "CryticCompile", target: str, **kwargs: str):
if compile_arguments:
cmd += compile_arguments.split(" ")

process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
try:
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError as e:
raise InvalidCompilation(e)

stdout_bytes, stderr_bytes = process.communicate()
stdout, stderr = (
Expand Down
28 changes: 17 additions & 11 deletions crytic_compile/platform/solc.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,15 @@ def get_version(solc: str) -> str:
:return:
"""
cmd = [solc, "--version"]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
try:
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError as e:
raise InvalidCompilation(e)
stdout_bytes, _ = process.communicate()
stdout = stdout_bytes.decode() # convert bytestrings to unicode strings
version = re.findall(r"\d+\.\d+\.\d+", stdout)
assert len(version) != 0
if len(version) == 0:
raise InvalidCompilation(f'Solidity version not found: {stdout}')
return version[0]


Expand Down Expand Up @@ -290,15 +294,17 @@ def _run_solc(
relative_filepath = relative_filepath[len(working_dir) + 1 :]

cmd += ["--allow-paths", ".", relative_filepath]

if env:
process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env, **additional_kwargs
)
else:
process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **additional_kwargs
)
try:
if env:
process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env, **additional_kwargs
)
else:
process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **additional_kwargs
)
except OSError as e:
raise InvalidCompilation(e)
stdout, stderr = process.communicate()
stdout, stderr = (stdout.decode(), stderr.decode()) # convert bytestrings to unicode strings

Expand Down
17 changes: 10 additions & 7 deletions crytic_compile/platform/solc_standard_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,16 @@ def _run_solc_standard_json(
cmd = [solc, "--standard-json", "--allow-paths", "."]
additional_kwargs = {"cwd": working_dir} if working_dir else {}

process = subprocess.Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
**additional_kwargs,
)
try:
process = subprocess.Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
**additional_kwargs,
)
except OSError as e:
raise InvalidCompilation(e)
stdout, stderr = process.communicate(json.dumps(solc_input).encode("utf-8"))
stdout, stderr = (stdout.decode(), stderr.decode()) # convert bytestrings to unicode strings

Expand Down
7 changes: 6 additions & 1 deletion crytic_compile/platform/truffle.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,14 @@ def _get_version_from_config(target: str) -> Optional[Tuple[str, str]]:

def _get_version(truffle_call, cwd):
cmd = truffle_call + ["version"]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd)
try:
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd)
except OSError as e:
raise InvalidCompilation(f"Truffle failed: {e}")
stdout, _ = process.communicate()
stdout = stdout.decode() # convert bytestrings to unicode strings
if not stdout:
raise InvalidCompilation(f"Truffle failed to run: 'truffle version'")
stdout = stdout.split("\n")
for line in stdout:
if "Solidity" in line:
Expand Down
4 changes: 2 additions & 2 deletions crytic_compile/platform/vyper.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ def _run_vyper(filename: str, vyper: str, env: Dict = None, working_dir: str = N
process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env, **additional_kwargs
)
except Exception as exception:
raise InvalidCompilation(exception)
except OSError as e:
raise InvalidCompilation(e)

stdout, stderr = process.communicate()

Expand Down
19 changes: 14 additions & 5 deletions crytic_compile/platform/waffle.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,12 @@ def compile(crytic_compile: "CryticCompile", target: str, **kwargs: str):
LOGGER.info("Temporary file created: %s", file_desc.name)
LOGGER.info("'%s running", " ".join(cmd))

process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=target
)
try:
process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=target
)
except OSError as e:
raise InvalidCompilation(e)

stdout, stderr = process.communicate()
stdout, stderr = (
Expand Down Expand Up @@ -224,7 +227,10 @@ def _get_version(compiler: str, cwd: str, config=None) -> str:

elif compiler == "native":
cmd = ["solc", "--version"]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd)
try:
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd)
except OSError as e:
raise InvalidCompilation(e)
stdout_bytes, _ = process.communicate()
stdout = stdout_bytes.decode() # convert bytestrings to unicode strings
stdout = stdout.split("\n")
Expand All @@ -234,7 +240,10 @@ def _get_version(compiler: str, cwd: str, config=None) -> str:

elif compiler == "solc-js":
cmd = ["solcjs", "--version"]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd)
try:
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd)
except OSError as e:
raise InvalidCompilation(e)
stdout_bytes, _ = process.communicate()
stdout = stdout_bytes.decode() # convert bytestrings to unicode strings
version = re.findall(r"\d+\.\d+\.\d+", stdout)[0]
Expand Down

0 comments on commit 708443a

Please sign in to comment.