22
33import logging
44import os
5+ import subprocess
56import threading
67import time
78from subprocess import Popen , TimeoutExpired
9+ import re
810
911from software .py_constants import *
1012from software .python_bindings import *
@@ -53,6 +55,21 @@ def __init__(
5355 self .log_level = log_level
5456 self .thread = threading .Thread (target = self .__restart__ , daemon = True )
5557
58+ def discover_supported_flags (self , path_to_binary : str ) -> set [str ]:
59+ """Discover what binary flags are supported by provided binary
60+
61+ :param path_to_binary path to specific binary
62+ :return a set of supported flags
63+ """
64+ try :
65+ result = subprocess .run (
66+ [path_to_binary , "--help" ], capture_output = True , text = True , timeout = 3
67+ )
68+ flags = re .findall (r"--(\w+)" , result .stdout )
69+ return set (flags )
70+ except (subprocess .TimeoutExpired , FileNotFoundError , PermissionError ):
71+ return set ()
72+
5673 def __enter__ (self ) -> FullSystem :
5774 """Enter the full_system context manager.
5875
@@ -69,13 +86,31 @@ def __enter__(self) -> FullSystem:
6986 except :
7087 pass
7188
72- self .full_system = "{} --runtime_dir={} {} {} --log_level={}" .format (
73- self .path_to_binary ,
74- self .full_system_runtime_dir ,
75- "--friendly_colour_yellow" if self .friendly_colour_yellow else "" ,
76- "--ci" if not self .running_in_realtime else "" ,
77- self .log_level .value ,
78- )
89+ supported_flags = self .discover_supported_flags (self .path_to_binary )
90+
91+ cmd_parts = [self .path_to_binary ]
92+ # runtime_dir is always required (core functionality)
93+ cmd_parts .append ("--runtime_dir={}" .format (self .full_system_runtime_dir ))
94+
95+ # Optional flags - only add if supported
96+ if self .friendly_colour_yellow and "friendly_colour_yellow" in supported_flags :
97+ cmd_parts .append ("--friendly_colour_yellow" )
98+ if not self .running_in_realtime and "ci" in supported_flags :
99+ cmd_parts .append ("--ci" )
100+ if "log_level" in supported_flags :
101+ cmd_parts .append ("--log_level={}" .format (self .log_level .value ))
102+
103+ # Log supported flags info based on importance level
104+ if supported_flags :
105+ logging .debug ("Binary support flags: {}" .format (supported_flags ))
106+ else :
107+ logging .warning (
108+ "Could not discover flags for path: '{}'. Continuing..." .format (
109+ self .path_to_binary
110+ )
111+ )
112+
113+ self .full_system = " " .join (cmd_parts )
79114
80115 if self .should_run_under_sudo :
81116 if not is_cmd_running (
0 commit comments