|
6 | 6 | from errors import CommandError |
7 | 7 | from pathlib import Path |
8 | 8 |
|
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( |
10 | 79 | cmd: str, |
11 | 80 | oss_fuzz_dir: Path, |
12 | 81 | project: str = "", |
13 | 82 | allowed_exit_codes: Maybe[list[int]] = Maybe.empty, |
14 | 83 | skip_yes: bool = False |
15 | 84 | ) -> int: |
16 | | - """Execute a command and return the exit code (no stdout/stderr capture)""" |
| 85 | + """build_fuzz.py 中使用的 run_command,简化版,抛异常""" |
17 | 86 | allowed_codes = allowed_exit_codes.value_or([0]) |
18 | 87 | 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: |
32 | 90 | error_msg = f"Command failed (exit code: {exit_code})" |
33 | 91 | if project: |
34 | 92 | error_msg += f" for project: {project}" |
35 | 93 | 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 |
36 | 114 |
|
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 |
|
0 commit comments