diff --git a/personal/cron_scripts/system/update_system.py b/personal/cron_scripts/system/update_system.py index 7a95d33..baac2d4 100644 --- a/personal/cron_scripts/system/update_system.py +++ b/personal/cron_scripts/system/update_system.py @@ -1,15 +1,45 @@ #!/usr/bin/python3 import subprocess import os +from datetime import datetime, timezone +from typing import Optional + +def generate_timestamp(fmt: Optional[str] = None, utc: bool = False) -> str: + """Return a timestamp string. + + - If `fmt` is provided, return `datetime.strftime(fmt)`. + - If `fmt` is None, return an ISO 8601 timestamp. When `utc` is True + the timestamp ends with 'Z' (UTC). When `utc` is False it returns + the local time ISO format. + + Examples: + - `generate_timestamp()` -> '2025-11-20T12:34:56Z' + - `generate_timestamp("%Y-%m-%d %H:%M:%S")` -> '2025-11-20 12:34:56' + """ + if fmt: + dt = datetime.now(timezone.utc) if utc else datetime.now() + return dt.strftime(fmt) + + dt = datetime.now(timezone.utc) if utc else datetime.now() + if utc: + # force trailing Z for UTC instead of +00:00 + return dt.replace(tzinfo=timezone.utc).isoformat().replace('+00:00', 'Z') + return dt.isoformat() + +print(f"Starting system update at {generate_timestamp()}") # Update package lists +print(f"{generate_timestamp()}: Updating package lists...") subprocess.run(['sudo', 'apt', 'update'], check=True) # Upgrade packages +print(f"{generate_timestamp()}: Upgrading packages...") subprocess.run(['sudo', 'apt', 'upgrade', '-y'], check=True) # Check if reboot is required if os.path.exists('/var/run/reboot-required'): - print("Reboot required. Rebooting now...") + print(f"{generate_timestamp()}: Reboot required. Rebooting now...") subprocess.run(['sudo', 'reboot']) else: - print("No reboot required.") + print(f"{generate_timestamp()}: No reboot required.") + +