|
25 | 25 | from pathlib import Path |
26 | 26 | from multiprocessing import Pool, cpu_count |
27 | 27 | from returns.maybe import Maybe, Nothing, Some |
28 | | - |
29 | | - |
30 | | - |
31 | | - |
32 | | - |
33 | | -def run_command( |
34 | | - cmd: str, |
35 | | - log_msg: str, |
36 | | - logger: logging.Logger, |
37 | | - allowed_exit_codes: Maybe[list[int]] = Nothing, |
38 | | - timeout: int = 3600 # Default 1-hour timeout |
39 | | -) -> bool: |
40 | | - """Execute commands with real-time logging and precise error handling""" |
41 | | - allowed_codes = allowed_exit_codes.value_or([]) |
42 | | - logger.info(f"▶️ {log_msg}...") |
43 | | - logger.debug(f" $ {cmd}") |
44 | | - |
45 | | - process = None |
46 | | - try: |
47 | | - process = subprocess.Popen( |
48 | | - cmd, |
49 | | - shell=True, |
50 | | - stdout=subprocess.PIPE, |
51 | | - stderr=subprocess.STDOUT, |
52 | | - text=True, |
53 | | - encoding="utf-8", |
54 | | - errors="replace" |
55 | | - ) |
56 | | - |
57 | | - start_time = time.time() |
58 | | - while process.poll() is None: |
59 | | - if time.time() - start_time > timeout: |
60 | | - logger.error(f"⌛ Command timed out after {timeout} seconds") |
61 | | - process.terminate() |
62 | | - try: |
63 | | - process.wait(timeout=5) |
64 | | - except subprocess.TimeoutExpired: |
65 | | - process.kill() |
66 | | - return False |
67 | | - |
68 | | - if process.stdout: |
69 | | - line = process.stdout.readline() |
70 | | - if line: |
71 | | - logger.debug(line.strip()) |
72 | | - else: |
73 | | - time.sleep(0.1) |
74 | | - |
75 | | - exit_code = process.returncode |
76 | | - if exit_code not in [0, *allowed_codes]: |
77 | | - logger.error(f"❌ Command execution failed, exit code: {exit_code}") |
78 | | - return False |
79 | | - return True |
80 | | - |
81 | | - except FileNotFoundError: |
82 | | - logger.error(f"🔍 Command not found: {cmd.split()[0]}") |
83 | | - return False |
84 | | - except PermissionError: |
85 | | - logger.error(f"🔒 Insufficient permissions to execute command: {cmd}") |
86 | | - return False |
87 | | - except subprocess.SubprocessError as e: |
88 | | - logger.exception(f"💥 Subprocess error: {e}") |
89 | | - return False |
90 | | - except OSError as e: |
91 | | - logger.exception(f"💥 Operating system error during command execution: {e}") |
92 | | - return False |
93 | | - finally: |
94 | | - if process and process.poll() is None: |
95 | | - try: |
96 | | - process.terminate() |
97 | | - process.wait(timeout=5) |
98 | | - except Exception: |
99 | | - pass |
| 28 | +from command_util import run_command_fuzz_all_targets as run_command |
| 29 | + |
| 30 | +# def run_command( |
| 31 | +# cmd: str, |
| 32 | +# log_msg: str, |
| 33 | +# logger: logging.Logger, |
| 34 | +# allowed_exit_codes: Maybe[list[int]] = Nothing, |
| 35 | +# timeout: int = 3600 # Default 1-hour timeout |
| 36 | +# ) -> bool: |
| 37 | +# """Execute commands with real-time logging and precise error handling""" |
| 38 | +# allowed_codes = allowed_exit_codes.value_or([]) |
| 39 | +# logger.info(f"▶️ {log_msg}...") |
| 40 | +# logger.debug(f" $ {cmd}") |
| 41 | + |
| 42 | +# process = None |
| 43 | +# try: |
| 44 | +# process = subprocess.Popen( |
| 45 | +# cmd, |
| 46 | +# shell=True, |
| 47 | +# stdout=subprocess.PIPE, |
| 48 | +# stderr=subprocess.STDOUT, |
| 49 | +# text=True, |
| 50 | +# encoding="utf-8", |
| 51 | +# errors="replace" |
| 52 | +# ) |
| 53 | + |
| 54 | +# start_time = time.time() |
| 55 | +# while process.poll() is None: |
| 56 | +# if time.time() - start_time > timeout: |
| 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 False |
| 64 | + |
| 65 | +# if process.stdout: |
| 66 | +# line = process.stdout.readline() |
| 67 | +# if line: |
| 68 | +# logger.debug(line.strip()) |
| 69 | +# else: |
| 70 | +# time.sleep(0.1) |
| 71 | + |
| 72 | +# exit_code = process.returncode |
| 73 | +# if exit_code not in [0, *allowed_codes]: |
| 74 | +# logger.error(f"❌ Command execution failed, exit code: {exit_code}") |
| 75 | +# return False |
| 76 | +# return True |
| 77 | + |
| 78 | +# except FileNotFoundError: |
| 79 | +# logger.error(f"🔍 Command not found: {cmd.split()[0]}") |
| 80 | +# return False |
| 81 | +# except PermissionError: |
| 82 | +# logger.error(f"🔒 Insufficient permissions to execute command: {cmd}") |
| 83 | +# return False |
| 84 | +# except subprocess.SubprocessError as e: |
| 85 | +# logger.exception(f"💥 Subprocess error: {e}") |
| 86 | +# return False |
| 87 | +# except OSError as e: |
| 88 | +# logger.exception(f"💥 Operating system error during command execution: {e}") |
| 89 | +# return False |
| 90 | +# finally: |
| 91 | +# if process and process.poll() is None: |
| 92 | +# try: |
| 93 | +# process.terminate() |
| 94 | +# process.wait(timeout=5) |
| 95 | +# except Exception: |
| 96 | +# pass |
100 | 97 |
|
101 | 98 |
|
102 | 99 | def discover_targets(project_name: str, oss_fuzz_dir: Path, logger: logging.Logger) -> list[str]: |
|
0 commit comments