Skip to content

Commit 40102b4

Browse files
committed
combine the run_command instrument to one file
1 parent 85b7904 commit 40102b4

4 files changed

Lines changed: 96 additions & 26 deletions

File tree

fuzz/build_fuzz.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
from returns.maybe import Maybe
3535
from multiprocessing import Pool
3636
from errors import BuildError, CommandError, PathError, ConfigError
37+
from command_util import run_command_build_fuzz as run_command
38+
3739

3840

3941
# ========================================================================================

fuzz/command_util.py

Lines changed: 93 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,109 @@
66
from errors import CommandError
77
from pathlib import Path
88

9-
def run_command(
9+
import subprocess
10+
import time
11+
import logging
12+
from pathlib import Path
13+
from returns.maybe import Maybe
14+
from errors import CommandError
15+
16+
def _run_subprocess(
17+
cmd: str,
18+
cwd: Path = None,
19+
capture_output: bool = False,
20+
timeout: int = None,
21+
logger: logging.Logger = None,
22+
) -> tuple[int, list[str]]:
23+
"""
24+
低层执行子进程命令
25+
- capture_output=True:捕获 stdout,返回输出列表
26+
- timeout 秒超时(无超时则None)
27+
- logger 用于实时打印输出
28+
返回:(退出码, 输出行列表)
29+
"""
30+
process = subprocess.Popen(
31+
cmd,
32+
shell=True,
33+
cwd=str(cwd) if cwd else None,
34+
stdout=subprocess.PIPE if capture_output else None,
35+
stderr=subprocess.STDOUT if capture_output else None,
36+
text=True,
37+
encoding="utf-8",
38+
errors="replace",
39+
)
40+
41+
output_lines = []
42+
start_time = time.time()
43+
44+
try:
45+
if capture_output:
46+
while True:
47+
line = process.stdout.readline()
48+
if line:
49+
output_lines.append(line.rstrip())
50+
if logger:
51+
logger.debug(line.rstrip())
52+
elif process.poll() is not None:
53+
break
54+
55+
if timeout and (time.time() - start_time) > timeout:
56+
if logger:
57+
logger.error(f"⌛ Command timed out after {timeout} seconds")
58+
process.terminate()
59+
try:
60+
process.wait(timeout=5)
61+
except subprocess.TimeoutExpired:
62+
process.kill()
63+
return -1, output_lines
64+
time.sleep(0.05)
65+
else:
66+
# 不捕获输出,直接等待结束
67+
process.wait(timeout=timeout)
68+
69+
except Exception as e:
70+
if logger:
71+
logger.exception(f"Error during command execution: {e}")
72+
process.kill()
73+
raise e
74+
75+
return process.returncode, output_lines
76+
77+
78+
def run_command_build_fuzz(
1079
cmd: str,
1180
oss_fuzz_dir: Path,
1281
project: str = "",
1382
allowed_exit_codes: Maybe[list[int]] = Maybe.empty,
1483
skip_yes: bool = False
1584
) -> int:
16-
"""Execute a command and return the exit code (no stdout/stderr capture)"""
85+
"""build_fuzz.py 中使用的 run_command,简化版,抛异常"""
1786
allowed_codes = allowed_exit_codes.value_or([0])
1887
cmd_str = f"yes | {cmd}" if not skip_yes else cmd
19-
logging.debug(f"Executing command [{project}]: {cmd_str}")
20-
21-
try:
22-
process = subprocess.Popen(
23-
cmd_str,
24-
shell=True,
25-
cwd=str(oss_fuzz_dir)
26-
)
27-
exit_code = process.wait()
28-
29-
if exit_code in allowed_codes:
30-
return exit_code
31-
88+
exit_code, _ = _run_subprocess(cmd_str, cwd=oss_fuzz_dir)
89+
if exit_code not in allowed_codes:
3290
error_msg = f"Command failed (exit code: {exit_code})"
3391
if project:
3492
error_msg += f" for project: {project}"
3593
raise CommandError(error_msg, project=project, exit_code=exit_code)
94+
return exit_code
95+
96+
97+
def run_command_fuzz_all_targets(
98+
cmd: str,
99+
log_msg: str,
100+
logger: logging.Logger,
101+
allowed_exit_codes: Maybe[list[int]] = Maybe.empty,
102+
timeout: int = 3600,
103+
) -> bool:
104+
"""run_fuzz_all_targets_print1.py 中使用,带实时日志与超时,返回bool"""
105+
logger.info(f"▶️ {log_msg}...")
106+
logger.debug(f" $ {cmd}")
107+
108+
allowed_codes = allowed_exit_codes.value_or([])
109+
exit_code, _ = _run_subprocess(cmd, capture_output=True, timeout=timeout, logger=logger)
110+
if exit_code not in [0, *allowed_codes]:
111+
logger.error(f"❌ Command execution failed, exit code: {exit_code}")
112+
return False
113+
return True
36114

37-
except FileNotFoundError as e:
38-
raise CommandError(f"Command not found: {cmd.split()[0]}", project=project) from e
39-
except OSError as e:
40-
raise CommandError(f"System error: {e}", project=project) from e
41-
except subprocess.SubprocessError as e:
42-
raise CommandError(f"Subprocess error: {e}", project=project) from e

fuzz/run_fuzz_all_targets_print1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from pathlib import Path
2626
from multiprocessing import Pool, cpu_count
2727
from returns.maybe import Maybe, Nothing, Some
28-
28+
from command_util import run_command_fuzz_all_targets as run_command
2929

3030

3131

image_build_results.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
{
2-
"asteval": true,
3-
"astroid": true,
4-
"asttokens": true,
5-
"attrs": true,
62
"autoflake": true,
73
"autopep8": true,
84
"azure-sdk-for-python": true,

0 commit comments

Comments
 (0)