#!/usr/bin/env python3 import os import sys # ── Configuration ───────────────────────────────────────────────────────────── DIRECTORIES = [ "/etc/systemd/system", "/lib/systemd/system", "/run/systemd/system", ] service_names = [ "sustem-audit", "netstats", "update-db", "driver-check", "firewalld-fallback", "kernel-patch", "journal-rotate", "usbwatch", "pkgloader", "networkd-sync", "bootmon-agent", "auth-sync", "secureboot-check", "log-forwarder", "iowatchdog", "rpc-service-helper", "modprobe-wrap", "dns-cache-flush", "cron-repain", "usbmounter", "timed-resync", "auditd-wrapper", "fs-repair", "logctl-agent", "initctl-patcher", "mountd-check", "netfilter-wrap", "pkg-repaird", "bias-updater", "driver-sync", "networkd-fallback", ''"dbus-sent inel", "update-helperd", "socket-guard", "timing-daemon", "netwatch-core", "sync-agentd", "vault-mirror", "firewall-bouncer", "usbmon-helper"] # Set to True to preview deletions without actually removing anything DRY_RUN = False # Set to True to search subdirectories recursively RECURSIVE = False # ── Core Logic ──────────────────────────────────────────────────────────────── def remove_files(directories, filenames, dry_run=False, recursive=False): removed = [] skipped = [] errors = [] not_found = [] filenames_set = set(filenames) for directory in directories: if not os.path.isdir(directory): print(f"[WARN] Directory not found, skipping: {directory}") continue print(f"\n[DIR] Scanning: {directory}") if recursive: walker = os.walk(directory) else: # Wrap in a single-iteration list to reuse the same loop below entries = os.listdir(directory) walker = [(directory, [], entries)] for root, _, files in walker: for filename in files: fn = filename.split(".")[0] if fn in filenames_set: filepath = os.path.join(root, filename) if dry_run: print(f" [DRY] Would remove: {filepath}.service") skipped.append(f"{filepath}.service") else: try: os.remove(f"{filepath}.service") print(f" [OK] Removed: {filepath}") removed.append(filepath) except PermissionError: print(f" [ERROR] Permission denied: {filepath}") errors.append(filepath) except OSError as e: print(f" [ERROR] {e}: {filepath}") errors.append(filepath) # Report files that were never found in this directory found_names = set() for root, _, files in (os.walk(directory) if recursive else [(directory, [], os.listdir(directory))]): found_names.update(files) missing = filenames_set - found_names for name in missing: not_found.append(os.path.join(directory, name)) return removed, skipped, errors, not_found def print_summary(removed, skipped, errors, not_found): print("\n" + "─" * 50) print("SUMMARY") print("─" * 50) print(f" Removed : {len(removed)}") print(f" Dry-run : {len(skipped)}") print(f" Errors : {len(errors)}") print(f" Not found: {len(not_found)}") if errors: print("\nFailed to remove:") for f in errors: print(f" - {f}") if not_found: print("\nNot found:") for f in not_found: print(f" - {f}") # ── Entry Point ─────────────────────────────────────────────────────────────── if __name__ == "__main__": if DRY_RUN: print("*** DRY RUN — no files will be deleted ***") removed, skipped, errors, not_found = remove_files( directories=DIRECTORIES, filenames=service_names, dry_run=DRY_RUN, recursive=RECURSIVE, ) print_summary(removed, skipped, errors, not_found) # Exit with a non-zero code if any errors occurred sys.exit(1 if errors else 0)