Added infinite proxy execution mode

This commit is contained in:
Kevin Veen-Birkenbach 2025-07-08 17:12:01 +02:00
parent 9cbd2d4f4b
commit 6b87a049d4
No known key found for this signature in database
GPG Key ID: 44D8F11FD62F878E

33
main.py
View File

@ -67,16 +67,12 @@ def failure_with_warning_loop():
from cli.sounds import Sound # ensure Sound imported from cli.sounds import Sound # ensure Sound imported
def _main():
# existing main block logic here
pass
if __name__ == "__main__": if __name__ == "__main__":
_main() # Parse special flags early and remove from args
# Parse --no-sound, --log and --git-clean early and remove from args
no_sound = False no_sound = False
log_enabled = False log_enabled = False
git_clean = False git_clean = False
infinite = False
if '--no-sound' in sys.argv: if '--no-sound' in sys.argv:
no_sound = True no_sound = True
sys.argv.remove('--no-sound') sys.argv.remove('--no-sound')
@ -86,6 +82,9 @@ if __name__ == "__main__":
if '--git-clean' in sys.argv: if '--git-clean' in sys.argv:
git_clean = True git_clean = True
sys.argv.remove('--git-clean') sys.argv.remove('--git-clean')
if '--infinite' in sys.argv:
infinite = True
sys.argv.remove('--infinite')
# Setup segfault handler to catch crashes # Setup segfault handler to catch crashes
def segv_handler(signum, frame): def segv_handler(signum, frame):
@ -118,11 +117,12 @@ if __name__ == "__main__":
# Handle help invocation # Handle help invocation
if len(sys.argv) == 1 or sys.argv[1] in ('-h', '--help'): if len(sys.argv) == 1 or sys.argv[1] in ('-h', '--help'):
print("CyMaIS CLI proxy to tools in ./cli/") print("CyMaIS CLI proxy to tools in ./cli/")
print("Usage: cymais [--no-sound] [--log] [--git-clean] <command> [options]") print("Usage: cymais [--no-sound] [--log] [--git-clean] [--infinite] <command> [options]")
print("Options:") print("Options:")
print(" --no-sound Suppress all sounds during execution") print(" --no-sound Suppress all sounds during execution")
print(" --log Log all proxied command output to logfile.log") print(" --log Log all proxied command output to logfile.log")
print(" --git-clean Remove all Git-ignored files before running") print(" --git-clean Remove all Git-ignored files before running")
print(" --infinite Run the proxied command in an infinite loop")
print(" -h, --help Show this help message and exit") print(" -h, --help Show this help message and exit")
print("Available commands:") print("Available commands:")
for cmd in available_cli_commands: for cmd in available_cli_commands:
@ -152,11 +152,12 @@ if __name__ == "__main__":
timestamp = datetime.now().strftime('%Y%m%dT%H%M%S') timestamp = datetime.now().strftime('%Y%m%dT%H%M%S')
log_file_path = os.path.join(log_dir, f'{timestamp}.log') log_file_path = os.path.join(log_dir, f'{timestamp}.log')
log_file = open(log_file_path, 'a', encoding='utf-8') log_file = open(log_file_path, 'a', encoding='utf-8')
# 📖 Tip: Check your logs at the path below
print(f"📖 Tip: Log file created at {log_file_path}")
def run_once():
try: try:
if log_enabled: if log_enabled:
# Use a pseudo-terminal to preserve color formatting
master_fd, slave_fd = pty.openpty() master_fd, slave_fd = pty.openpty()
proc = subprocess.Popen( proc = subprocess.Popen(
full_cmd, full_cmd,
@ -194,8 +195,20 @@ if __name__ == "__main__":
else: else:
if not no_sound: if not no_sound:
Sound.play_finished_successfully_sound() Sound.play_finished_successfully_sound()
sys.exit(0) return True
except Exception as e: except Exception as e:
print(f"Exception running command: {e}") print(f"Exception running command: {e}")
failure_with_warning_loop() failure_with_warning_loop()
sys.exit(1) sys.exit(1)
if infinite:
# ♾️ Infinite mode activated
print("♾️ Starting infinite execution mode...")
count = 1
while True:
print(f"🔄 Execution #{count}")
run_once()
count += 1
else:
run_once()
sys.exit(0)