Files
port-ui/main.py

84 lines
2.3 KiB
Python
Raw Normal View History

2025-03-20 00:23:35 +01:00
#!/usr/bin/env python3
"""
2025-07-08 17:16:57 +02:00
main.py - Proxy to Makefile targets for managing the Portfolio CMS Docker application.
Automatically generates CLI commands based on the Makefile definitions.
2025-03-20 00:23:35 +01:00
"""
import argparse
import subprocess
import sys
import os
2025-07-08 17:16:57 +02:00
import re
2025-07-05 10:12:49 +02:00
from pathlib import Path
2025-07-08 17:16:57 +02:00
MAKEFILE_PATH = Path(__file__).resolve().parent / "Makefile"
2025-07-05 10:12:49 +02:00
2025-07-08 17:16:57 +02:00
def load_targets(makefile_path):
"""
Parse the Makefile to extract targets and their help comments.
Assumes each target is defined as 'name:' and the following line that starts
with '\t#' provides its help text.
"""
targets = []
pattern = re.compile(r"^([A-Za-z0-9_\-]+):")
with open(makefile_path, 'r') as f:
lines = f.readlines()
for idx, line in enumerate(lines):
m = pattern.match(line)
if m:
name = m.group(1)
help_text = ''
# look for next non-empty line
if idx + 1 < len(lines) and lines[idx+1].lstrip().startswith('#'):
help_text = lines[idx+1].lstrip('# ').strip()
targets.append((name, help_text))
return targets
2025-03-20 00:23:35 +01:00
2025-07-08 17:16:57 +02:00
def run_command(command, dry_run=False):
"""Utility to run shell commands."""
2025-03-20 00:23:35 +01:00
print(f"Executing: {' '.join(command)}")
if dry_run:
print("Dry run enabled: command not executed.")
return
try:
2025-07-08 17:16:57 +02:00
subprocess.check_call(command)
2025-03-20 00:23:35 +01:00
except subprocess.CalledProcessError as e:
print(f"Error: Command failed with exit code {e.returncode}")
sys.exit(e.returncode)
def main():
parser = argparse.ArgumentParser(
2025-07-08 17:16:57 +02:00
description="CLI proxy to Makefile targets for Portfolio CMS Docker app"
2025-03-20 00:23:35 +01:00
)
parser.add_argument(
"--dry-run",
action="store_true",
2025-07-08 17:16:57 +02:00
help="Print the generated Make command without executing it."
2025-03-21 18:36:00 +01:00
)
2025-07-08 17:16:57 +02:00
2025-03-20 00:23:35 +01:00
subparsers = parser.add_subparsers(
2025-07-08 17:16:57 +02:00
title="Available commands",
dest="command",
required=True
2025-03-20 00:23:35 +01:00
)
2025-03-21 18:36:00 +01:00
2025-07-08 17:16:57 +02:00
targets = load_targets(MAKEFILE_PATH)
for name, help_text in targets:
sp = subparsers.add_parser(name, help=help_text)
sp.set_defaults(target=name)
2025-03-21 18:36:00 +01:00
2025-03-20 00:23:35 +01:00
args = parser.parse_args()
2025-07-08 17:16:57 +02:00
if not args.command:
2025-03-20 00:23:35 +01:00
parser.print_help()
sys.exit(1)
2025-03-21 18:36:00 +01:00
2025-07-08 17:16:57 +02:00
cmd = ["make", args.target]
run_command(cmd, dry_run=args.dry_run)
2025-03-20 00:23:35 +01:00
if __name__ == "__main__":
2025-07-08 17:16:57 +02:00
from pathlib import Path
main()