Skip to content

Commit 02cc1df

Browse files
committed
auto-clean: clean up dapp and hardhat implementations with a helper
1 parent ab436a3 commit 02cc1df

File tree

3 files changed

+72
-28
lines changed

3 files changed

+72
-28
lines changed

crytic_compile/platform/dapp.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from crytic_compile.platform.abstract_platform import AbstractPlatform
2020
from crytic_compile.platform.types import Type
2121
from crytic_compile.utils.naming import convert_filename, extract_name
22+
from crytic_compile.utils.subprocess import run
2223

2324
# Handle cycle
2425
from crytic_compile.utils.natspec import Natspec
@@ -129,16 +130,7 @@ def clean(self, **kwargs: str) -> None:
129130
if dapp_ignore_compile:
130131
return
131132

132-
cmd = ["dapp", "clean"]
133-
LOGGER.info(
134-
"'%s' running",
135-
" ".join(cmd),
136-
)
137-
subprocess.run(
138-
cmd,
139-
cwd=self._target,
140-
executable=shutil.which(cmd[0]),
141-
)
133+
run(["dapp", "clean"], cwd=self._target)
142134

143135
@staticmethod
144136
def is_supported(target: str, **kwargs: str) -> bool:

crytic_compile/platform/hardhat.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from crytic_compile.platform.types import Type
1515
from crytic_compile.utils.naming import convert_filename, extract_name
1616
from crytic_compile.utils.natspec import Natspec
17+
from crytic_compile.utils.subprocess import run
1718
from .abstract_platform import AbstractPlatform
1819

1920
# Handle cycle
@@ -35,6 +36,17 @@ class Hardhat(AbstractPlatform):
3536
PROJECT_URL = "https://github.com/nomiclabs/hardhat"
3637
TYPE = Type.HARDHAT
3738

39+
def _settings(self, args):
40+
hardhat_ignore_compile = args.get("hardhat_ignore_compile", False) or args.get(
41+
"ignore_compile", False
42+
)
43+
44+
base_cmd = ["hardhat"]
45+
if not args.get("npx_disable", False):
46+
base_cmd = ["npx"] + base_cmd
47+
48+
return hardhat_ignore_compile, base_cmd
49+
3850
# pylint: disable=too-many-locals,too-many-statements
3951
def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
4052
"""Run the compilation
@@ -48,13 +60,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
4860
InvalidCompilation: If hardhat failed to run
4961
"""
5062

51-
hardhat_ignore_compile = kwargs.get("hardhat_ignore_compile", False) or kwargs.get(
52-
"ignore_compile", False
53-
)
54-
55-
base_cmd = ["hardhat"]
56-
if not kwargs.get("npx_disable", False):
57-
base_cmd = ["npx"] + base_cmd
63+
hardhat_ignore_compile, base_cmd = self._settings(kwargs)
5864

5965
detected_paths = self._get_hardhat_paths(base_cmd, kwargs)
6066

@@ -187,20 +193,13 @@ def clean(self, **kwargs: str) -> None:
187193
**kwargs: optional arguments.
188194
"""
189195

190-
if self._ignore_compile:
196+
hardhat_ignore_compile, base_cmd = self._settings(kwargs)
197+
198+
if hardhat_ignore_compile:
191199
return
192200

193201
for clean_cmd in [["clean"], ["clean", "--global"]]:
194-
cmd = self._base_cmd + clean_cmd
195-
LOGGER.info(
196-
"'%s' running",
197-
" ".join(cmd),
198-
)
199-
subprocess.run(
200-
cmd,
201-
cwd=self._target,
202-
executable=shutil.which(cmd[0]),
203-
)
202+
run(base_cmd + clean_cmd, cwd=self._target)
204203

205204
@staticmethod
206205
def is_supported(target: str, **kwargs: str) -> bool:

crytic_compile/utils/subprocess.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import logging
2+
import os
3+
from pathlib import Path
4+
import shutil
5+
import subprocess
6+
from typing import Dict, List
7+
8+
LOGGER = logging.getLogger("CryticCompile")
9+
10+
11+
def run(
12+
cmd: List[str],
13+
cwd: os.PathLike | None = None,
14+
extra_env: Dict[str, str] | None = None,
15+
**kwargs,
16+
) -> subprocess.CompletedProcess | None:
17+
"""
18+
Execute a command in a cross-platform compatible way.
19+
20+
Args:
21+
cmd (List[str]): Command to run
22+
cwd (PathLike): Working directory to run the command in
23+
extra_env (Dict[str, str]): extra environment variables to define for the execution
24+
**kwargs: optional arguments passed to `subprocess.run`
25+
26+
Returns:
27+
CompletedProcess: If the execution succeeded
28+
None: if there was a problem executing
29+
"""
30+
subprocess_cwd = Path(os.getcwd() if cwd is None else cwd).resolve()
31+
subprocess_env = None if extra_env is None else dict(os.environ, extra_env)
32+
subprocess_exe = shutil.which(cmd[0])
33+
34+
if subprocess_exe is None:
35+
LOGGER.error("Cannot execute `%s`, is it installed and in PATH?", cmd[0])
36+
return None
37+
38+
LOGGER.info(
39+
"'%s' running (wd: %s)",
40+
" ".join(cmd),
41+
subprocess_cwd,
42+
)
43+
44+
try:
45+
return subprocess.run(
46+
cmd, cwd=subprocess_cwd, executable=subprocess_exe, env=subprocess_env, **kwargs
47+
)
48+
except FileNotFoundError:
49+
LOGGER.error("Could not execute `%s`, is it installed and in PATH?", cmd[0])
50+
except OSError:
51+
LOGGER.error("OS error executing:", exc_info=1)
52+
53+
return None

0 commit comments

Comments
 (0)