mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-07-18 06:24:25 +02:00
Added infinite proxy execution mode
This commit is contained in:
parent
9cbd2d4f4b
commit
6b87a049d4
113
main.py
113
main.py
@ -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,50 +152,63 @@ 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:
|
||||||
|
if log_enabled:
|
||||||
|
master_fd, slave_fd = pty.openpty()
|
||||||
|
proc = subprocess.Popen(
|
||||||
|
full_cmd,
|
||||||
|
stdin=slave_fd,
|
||||||
|
stdout=slave_fd,
|
||||||
|
stderr=slave_fd,
|
||||||
|
text=True
|
||||||
|
)
|
||||||
|
os.close(slave_fd)
|
||||||
|
import errno
|
||||||
|
with os.fdopen(master_fd) as master:
|
||||||
|
try:
|
||||||
|
for line in master:
|
||||||
|
ts = datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
|
||||||
|
log_file.write(f"{ts} {line}")
|
||||||
|
log_file.flush()
|
||||||
|
print(line, end='')
|
||||||
|
except OSError as e:
|
||||||
|
if e.errno != errno.EIO:
|
||||||
|
raise
|
||||||
|
proc.wait()
|
||||||
|
rc = proc.returncode
|
||||||
|
else:
|
||||||
|
proc = subprocess.Popen(full_cmd)
|
||||||
|
proc.wait()
|
||||||
|
rc = proc.returncode
|
||||||
|
|
||||||
try:
|
if log_file:
|
||||||
if log_enabled:
|
log_file.close()
|
||||||
# Use a pseudo-terminal to preserve color formatting
|
|
||||||
master_fd, slave_fd = pty.openpty()
|
|
||||||
proc = subprocess.Popen(
|
|
||||||
full_cmd,
|
|
||||||
stdin=slave_fd,
|
|
||||||
stdout=slave_fd,
|
|
||||||
stderr=slave_fd,
|
|
||||||
text=True
|
|
||||||
)
|
|
||||||
os.close(slave_fd)
|
|
||||||
import errno
|
|
||||||
with os.fdopen(master_fd) as master:
|
|
||||||
try:
|
|
||||||
for line in master:
|
|
||||||
ts = datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
|
|
||||||
log_file.write(f"{ts} {line}")
|
|
||||||
log_file.flush()
|
|
||||||
print(line, end='')
|
|
||||||
except OSError as e:
|
|
||||||
if e.errno != errno.EIO:
|
|
||||||
raise
|
|
||||||
proc.wait()
|
|
||||||
rc = proc.returncode
|
|
||||||
else:
|
|
||||||
proc = subprocess.Popen(full_cmd)
|
|
||||||
proc.wait()
|
|
||||||
rc = proc.returncode
|
|
||||||
|
|
||||||
if log_file:
|
if rc != 0:
|
||||||
log_file.close()
|
print(f"Command '{args.cli_command}' failed with exit code {rc}.")
|
||||||
|
failure_with_warning_loop()
|
||||||
if rc != 0:
|
sys.exit(rc)
|
||||||
print(f"Command '{args.cli_command}' failed with exit code {rc}.")
|
else:
|
||||||
|
if not no_sound:
|
||||||
|
Sound.play_finished_successfully_sound()
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Exception running command: {e}")
|
||||||
failure_with_warning_loop()
|
failure_with_warning_loop()
|
||||||
sys.exit(rc)
|
sys.exit(1)
|
||||||
else:
|
|
||||||
if not no_sound:
|
if infinite:
|
||||||
Sound.play_finished_successfully_sound()
|
# ♾️ Infinite mode activated
|
||||||
sys.exit(0)
|
print("♾️ Starting infinite execution mode...")
|
||||||
except Exception as e:
|
count = 1
|
||||||
print(f"Exception running command: {e}")
|
while True:
|
||||||
failure_with_warning_loop()
|
print(f"🔄 Execution #{count}")
|
||||||
sys.exit(1)
|
run_once()
|
||||||
|
count += 1
|
||||||
|
else:
|
||||||
|
run_once()
|
||||||
|
sys.exit(0)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user