File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # command_util.py
2+
3+ import subprocess
4+ import logging
5+ from returns .maybe import Maybe
6+ from errors import CommandError
7+ from pathlib import Path
8+
9+ def run_command (
10+ cmd : str ,
11+ oss_fuzz_dir : Path ,
12+ project : str = "" ,
13+ allowed_exit_codes : Maybe [list [int ]] = Maybe .empty ,
14+ skip_yes : bool = False
15+ ) -> int :
16+ """Execute a command and return the exit code (no stdout/stderr capture)"""
17+ allowed_codes = allowed_exit_codes .value_or ([0 ])
18+ 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+
32+ error_msg = f"Command failed (exit code: { exit_code } )"
33+ if project :
34+ error_msg += f" for project: { project } "
35+ raise CommandError (error_msg , project = project , exit_code = exit_code )
36+
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
You can’t perform that action at this time.
0 commit comments