Skip to content

Commit bc7c024

Browse files
committed
Fix pylint
Remove working_dir usage to reduce complexity
1 parent cdadc24 commit bc7c024

File tree

2 files changed

+47
-25
lines changed

2 files changed

+47
-25
lines changed

crytic_compile/utils/naming.py

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,48 @@ def combine_filename_name(filename: str, name: str) -> str:
5959
return filename + ":" + name
6060

6161

62+
def _verify_filename_existence(filename: Path, cwd: Path) -> Path:
63+
"""
64+
Check if the filename exist. If it does not, try multiple heuristics to find the right filename:
65+
- Look for contracts/FILENAME
66+
- Look for node_modules/FILENAME
67+
- Look for node_modules/FILENAME in all the parents directories
68+
69+
70+
Args:
71+
filename (Path): filename to check
72+
cwd (Path): directory
73+
74+
Raises:
75+
InvalidCompilation: if the filename is not found
76+
77+
Returns:
78+
Path: the filename
79+
"""
80+
81+
if filename.exists():
82+
return filename
83+
84+
if cwd.joinpath(Path("contracts"), filename).exists():
85+
filename = cwd.joinpath("contracts", filename)
86+
elif cwd.joinpath(filename).exists():
87+
filename = cwd.joinpath(filename)
88+
# how node.js loads dependencies from node_modules:
89+
# https://nodejs.org/api/modules.html#loading-from-node_modules-folders
90+
elif cwd.joinpath(Path("node_modules"), filename).exists():
91+
filename = cwd.joinpath("node_modules", filename)
92+
else:
93+
for parent in cwd.parents:
94+
if parent.joinpath(Path("node_modules"), filename).exists():
95+
filename = parent.joinpath(Path("node_modules"), filename)
96+
break
97+
98+
if not filename.exists():
99+
raise InvalidCompilation(f"Unknown file: {filename}")
100+
101+
return filename
102+
103+
62104
# pylint: disable=too-many-branches
63105
def convert_filename(
64106
used_filename: Union[str, Path],
@@ -75,9 +117,6 @@ def convert_filename(
75117
crytic_compile (CryticCompile): Associated CryticCompile object
76118
working_dir (Optional[Union[str, Path]], optional): Working directory. Defaults to None.
77119
78-
Raises:
79-
InvalidCompilation: [description]
80-
81120
Returns:
82121
Filename: Filename converted
83122
"""
@@ -91,9 +130,9 @@ def convert_filename(
91130
else:
92131
filename = Path(filename_txt)
93132

133+
# cwd points to the directory to be used
94134
if working_dir is None:
95135
cwd = Path.cwd()
96-
working_dir = cwd
97136
else:
98137
working_dir = Path(working_dir)
99138
if working_dir.is_absolute():
@@ -107,23 +146,7 @@ def convert_filename(
107146
except ValueError:
108147
pass
109148

110-
if not filename.exists():
111-
if cwd.joinpath(Path("contracts"), filename).exists():
112-
filename = cwd.joinpath("contracts", filename)
113-
elif working_dir.joinpath(filename).exists():
114-
filename = working_dir.joinpath(filename)
115-
# how node.js loads dependencies from node_modules:
116-
# https://nodejs.org/api/modules.html#loading-from-node_modules-folders
117-
elif cwd.joinpath(Path("node_modules"), filename).exists():
118-
filename = cwd.joinpath("node_modules", filename)
119-
else:
120-
for parent in cwd.parents:
121-
if parent.joinpath(Path("node_modules"), filename).exists():
122-
filename = parent.joinpath(Path("node_modules"), filename)
123-
break
124-
125-
if not filename.exists():
126-
raise InvalidCompilation(f"Unknown file: {filename}")
149+
filename = _verify_filename_existence(filename, cwd)
127150

128151
if not filename.is_absolute():
129152
filename = cwd.joinpath(filename)
@@ -133,10 +156,10 @@ def convert_filename(
133156

134157
# Build the short path
135158
try:
136-
if working_dir.is_absolute():
137-
short = absolute.relative_to(working_dir)
159+
if cwd.is_absolute():
160+
short = absolute.relative_to(cwd)
138161
else:
139-
short = relative.relative_to(working_dir)
162+
short = relative.relative_to(cwd)
140163
except ValueError:
141164
short = relative
142165
except RuntimeError:

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ invalid-name,
1111
fixme,
1212
too-many-return-statements,
1313
too-many-ancestors,
14-
too-many-statements,
1514
logging-fstring-interpolation,
1615
logging-not-lazy,
1716
duplicate-code,

0 commit comments

Comments
 (0)