mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-06-25 03:38:59 +02:00
Added --git-clean function to cleanup before run
This commit is contained in:
parent
c700ff3ee7
commit
7dd8fd4a5f
29
main.py
29
main.py
@ -48,6 +48,10 @@ def extract_description_via_help(cli_script_path):
|
|||||||
except Exception:
|
except Exception:
|
||||||
return "-"
|
return "-"
|
||||||
|
|
||||||
|
def git_clean_repo():
|
||||||
|
"""Remove all Git-ignored files and directories in the current repository."""
|
||||||
|
subprocess.run(['git', 'clean', '-Xfd'], check=True)
|
||||||
|
|
||||||
def play_start_intro():
|
def play_start_intro():
|
||||||
Sound.play_start_sound()
|
Sound.play_start_sound()
|
||||||
Sound.play_cymais_intro_sound()
|
Sound.play_cymais_intro_sound()
|
||||||
@ -61,16 +65,27 @@ def failure_with_warning_loop():
|
|||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("Warnings stopped by user.")
|
print("Warnings stopped by user.")
|
||||||
|
|
||||||
|
from cli.sounds import Sound # ensure Sound imported
|
||||||
|
|
||||||
|
def _main():
|
||||||
|
# existing main block logic here
|
||||||
|
pass
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# Parse --no-sound and --log early and remove from args
|
_main()
|
||||||
|
# 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
|
||||||
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')
|
||||||
if '--log' in sys.argv:
|
if '--log' in sys.argv:
|
||||||
log_enabled = True
|
log_enabled = True
|
||||||
sys.argv.remove('--log')
|
sys.argv.remove('--log')
|
||||||
|
if '--git-clean' in sys.argv:
|
||||||
|
git_clean = True
|
||||||
|
sys.argv.remove('--git-clean')
|
||||||
|
|
||||||
# Setup segfault handler to catch crashes
|
# Setup segfault handler to catch crashes
|
||||||
def segv_handler(signum, frame):
|
def segv_handler(signum, frame):
|
||||||
@ -94,15 +109,20 @@ if __name__ == "__main__":
|
|||||||
cli_dir = os.path.join(script_dir, "cli")
|
cli_dir = os.path.join(script_dir, "cli")
|
||||||
os.chdir(script_dir)
|
os.chdir(script_dir)
|
||||||
|
|
||||||
|
# If requested, clean git-ignored files
|
||||||
|
if git_clean:
|
||||||
|
git_clean_repo()
|
||||||
|
|
||||||
available_cli_commands = list_cli_commands(cli_dir)
|
available_cli_commands = list_cli_commands(cli_dir)
|
||||||
|
|
||||||
# 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] <command> [options]")
|
print("Usage: cymais [--no-sound] [--log] [--git-clean] <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(" -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:
|
||||||
@ -142,12 +162,11 @@ if __name__ == "__main__":
|
|||||||
text=True
|
text=True
|
||||||
)
|
)
|
||||||
os.close(slave_fd)
|
os.close(slave_fd)
|
||||||
with os.fdopen(master_fd) as m:
|
with os.fdopen(master_fd) as master:
|
||||||
for line in m:
|
for line in master:
|
||||||
ts = datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
|
ts = datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
|
||||||
log_file.write(f"{ts} {line}")
|
log_file.write(f"{ts} {line}")
|
||||||
log_file.flush()
|
log_file.flush()
|
||||||
# Print raw line (with ANSI escapes) to stdout
|
|
||||||
print(line, end='')
|
print(line, end='')
|
||||||
proc.wait()
|
proc.wait()
|
||||||
rc = proc.returncode
|
rc = proc.returncode
|
||||||
|
@ -5,6 +5,7 @@ import sys
|
|||||||
import stat
|
import stat
|
||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
# Insert project root into import path so we can import main.py
|
# Insert project root into import path so we can import main.py
|
||||||
sys.path.insert(
|
sys.path.insert(
|
||||||
@ -39,6 +40,11 @@ class TestMainHelpers(unittest.TestCase):
|
|||||||
# Only 'one' and 'two' should be returned, in sorted order
|
# Only 'one' and 'two' should be returned, in sorted order
|
||||||
commands = main.list_cli_commands(tmpdir)
|
commands = main.list_cli_commands(tmpdir)
|
||||||
self.assertEqual(commands, ["one", "two"])
|
self.assertEqual(commands, ["one", "two"])
|
||||||
|
|
||||||
|
def test_git_clean_repo_invokes_git_clean(self):
|
||||||
|
with mock.patch('main.subprocess.run') as mock_run:
|
||||||
|
main.git_clean_repo()
|
||||||
|
mock_run.assert_called_once_with(['git', 'clean', '-Xfd'], check=True)
|
||||||
|
|
||||||
def test_extract_description_via_help_with_description(self):
|
def test_extract_description_via_help_with_description(self):
|
||||||
# Create a dummy script that prints a help description
|
# Create a dummy script that prints a help description
|
||||||
|
Loading…
x
Reference in New Issue
Block a user