Skip to content

Commit 27d227d

Browse files
committed
chore(utils): move render_command to utils.py
1 parent ccc6fab commit 27d227d

2 files changed

Lines changed: 36 additions & 34 deletions

File tree

codesectools/sasts/core/sast/__init__.py

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
USER_OUTPUT_DIR,
3636
MissingFile,
3737
NonZeroExit,
38+
render_command,
3839
run_command,
3940
)
4041

@@ -96,39 +97,6 @@ def __init__(self) -> None:
9697
self.status = self.requirements.get_status()
9798
self.missing = self.requirements.get_missing()
9899

99-
def render_command(self, command: list[str], map: dict[str, str]) -> list[str]:
100-
"""Render a command template by replacing placeholders with values.
101-
102-
Args:
103-
command: The command template as a list of strings.
104-
map: A dictionary of placeholders to their replacement values.
105-
106-
Returns:
107-
The rendered command as a list of strings.
108-
109-
"""
110-
_command = command.copy()
111-
for pattern, value in map.items():
112-
for i, arg in enumerate(_command):
113-
# Check if optional argument can be used
114-
if isinstance(arg, tuple):
115-
default_arg, optional_arg = arg
116-
if pattern in optional_arg:
117-
_command[i] = arg.replace(pattern, value)
118-
else:
119-
_command[i] = default_arg
120-
else:
121-
if pattern in arg:
122-
_command[i] = arg.replace(pattern, value)
123-
124-
# Remove not rendered part of the command:
125-
__command = []
126-
for part in _command:
127-
if not ("{" in part and "}" in part):
128-
__command.append(part)
129-
130-
return __command
131-
132100
def run_analysis(
133101
self, lang: str, project_dir: Path, output_dir: Path, **kwargs: Any
134102
) -> None:
@@ -165,7 +133,7 @@ def run_analysis(
165133
command_output = ""
166134
start = time.time()
167135
for command in self.commands:
168-
rendered_command = self.render_command(command, render_variables)
136+
rendered_command = render_command(command, render_variables)
169137
retcode, out = run_command(rendered_command, project_dir, self.environ)
170138
command_output += out
171139
if retcode not in self.valid_codes:

codesectools/utils.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,40 @@ def DEBUG() -> bool:
3939

4040

4141
# Subprocess wrapper
42+
def render_command(command: list[str], map: dict[str, str]) -> list[str]:
43+
"""Render a command template by replacing placeholders with values.
44+
45+
Args:
46+
command: The command template as a list of strings.
47+
map: A dictionary of placeholders to their replacement values.
48+
49+
Returns:
50+
The rendered command as a list of strings.
51+
52+
"""
53+
_command = command.copy()
54+
for pattern, value in map.items():
55+
for i, arg in enumerate(_command):
56+
# Check if optional argument can be used
57+
if isinstance(arg, tuple):
58+
default_arg, optional_arg = arg
59+
if pattern in optional_arg:
60+
_command[i] = arg.replace(pattern, value)
61+
else:
62+
_command[i] = default_arg
63+
else:
64+
if pattern in arg:
65+
_command[i] = arg.replace(pattern, value)
66+
67+
# Remove not rendered part of the command:
68+
__command = []
69+
for part in _command:
70+
if not ("{" in part and "}" in part):
71+
__command.append(part)
72+
73+
return __command
74+
75+
4276
def run_command(
4377
command: Sequence[str], cwd: Path, env: dict[str, str] | None = None
4478
) -> tuple[int | None, str]:

0 commit comments

Comments
 (0)