Skip to content

Commit 1396844

Browse files
committed
use stdin/stdout
1 parent 852d59a commit 1396844

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

crytic_compile/platform/vyper.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import json
55
import logging
66
import os
7-
import tempfile
7+
import subprocess
8+
import shutil
89
from pathlib import Path
910
from typing import TYPE_CHECKING, Dict, List, Optional
1011

@@ -72,15 +73,8 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
7273
self.add_source_files([target])
7374

7475
vyper_bin = kwargs.get("vyper", "vyper")
75-
compilation_artifacts = None
76-
with tempfile.NamedTemporaryFile(mode="a+") as f:
77-
json.dump(self.standard_json_input, f)
78-
f.flush()
79-
compilation_artifacts = _run_vyper_standard_json(f.name, vyper_bin)
8076

81-
if "errors" in compilation_artifacts:
82-
# TODO format errors
83-
raise InvalidCompilation(compilation_artifacts["errors"])
77+
compilation_artifacts = _run_vyper_standard_json(self.standard_json_input, vyper_bin)
8478
compilation_unit = CompilationUnit(crytic_compile, str(target))
8579

8680
compiler_version = compilation_artifacts["compiler"].split("-")[1]
@@ -169,15 +163,15 @@ def _guessed_tests(self) -> List[str]:
169163

170164

171165
def _run_vyper_standard_json(
172-
standard_input_path: str,
166+
standard_json_input: Dict,
173167
vyper: str,
174168
env: Optional[Dict] = None,
175169
working_dir: Optional[str] = None,
176170
) -> Dict:
177171
"""Run vyper and write compilation output to a file
178172
179173
Args:
180-
standard_input_path (str): path to the standard input json file
174+
standard_json_input (Dict): Dict containing the vyper standard json input
181175
vyper (str): vyper binary
182176
env (Optional[Dict], optional): Environment variables. Defaults to None.
183177
working_dir (Optional[str], optional): Working directory. Defaults to None.
@@ -188,13 +182,29 @@ def _run_vyper_standard_json(
188182
Returns:
189183
Dict: Vyper json compilation artifact
190184
"""
191-
with tempfile.NamedTemporaryFile(mode="a+") as f:
192-
cmd = [vyper, standard_input_path, "--standard-json", "-o", f.name]
193-
success = run(cmd, cwd=working_dir, extra_env=env)
194-
if success is None:
195-
raise InvalidCompilation("Vyper compilation failed")
196-
f.seek(0)
197-
return json.loads(f.read())
185+
cmd = [vyper, "--standard-json"]
186+
187+
with subprocess.Popen(
188+
cmd,
189+
stdin=subprocess.PIPE,
190+
stdout=subprocess.PIPE,
191+
stderr=subprocess.PIPE,
192+
env=env,
193+
executable=shutil.which(cmd[0]),
194+
) as process:
195+
196+
stdout_b, stderr_b = process.communicate(json.dumps(standard_json_input).encode("utf-8"))
197+
stdout, stderr = (
198+
stdout_b.decode(),
199+
stderr_b.decode(errors="backslashreplace"),
200+
) # convert bytestrings to unicode strings
201+
202+
vyper_standard_output = json.loads(stdout)
203+
if "errors" in vyper_standard_output:
204+
# TODO format errors
205+
raise InvalidCompilation(vyper_standard_output["errors"])
206+
207+
return vyper_standard_output
198208

199209

200210
def _relative_to_short(relative: Path) -> Path:

0 commit comments

Comments
 (0)