4
4
import json
5
5
import logging
6
6
import os
7
- import tempfile
7
+ import subprocess
8
+ import shutil
8
9
from pathlib import Path
9
10
from typing import TYPE_CHECKING , Dict , List , Optional
10
11
@@ -72,15 +73,8 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
72
73
self .add_source_files ([target ])
73
74
74
75
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 )
80
76
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 )
84
78
compilation_unit = CompilationUnit (crytic_compile , str (target ))
85
79
86
80
compiler_version = compilation_artifacts ["compiler" ].split ("-" )[1 ]
@@ -169,15 +163,15 @@ def _guessed_tests(self) -> List[str]:
169
163
170
164
171
165
def _run_vyper_standard_json (
172
- standard_input_path : str ,
166
+ standard_json_input : Dict ,
173
167
vyper : str ,
174
168
env : Optional [Dict ] = None ,
175
169
working_dir : Optional [str ] = None ,
176
170
) -> Dict :
177
171
"""Run vyper and write compilation output to a file
178
172
179
173
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
181
175
vyper (str): vyper binary
182
176
env (Optional[Dict], optional): Environment variables. Defaults to None.
183
177
working_dir (Optional[str], optional): Working directory. Defaults to None.
@@ -188,13 +182,29 @@ def _run_vyper_standard_json(
188
182
Returns:
189
183
Dict: Vyper json compilation artifact
190
184
"""
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
198
208
199
209
200
210
def _relative_to_short (relative : Path ) -> Path :
0 commit comments