Guest

ver

Apr 18th, 2026
10
0
Never
Not a member of GistPad yet? Sign Up, it unlocks many cool features!
None 11.93 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import os
  4. import sys
  5.  
  6. # ── Configuration ─────────────────────────────────────────────────────────────
  7.  
  8. DIRECTORIES = [
  9. "/etc/systemd/system",
  10. "/lib/systemd/system",
  11. "/run/systemd/system",
  12. ]
  13.  
  14. service_names = [
  15. "sustem-audit",
  16. "netstats",
  17. "update-db",
  18. "driver-check",
  19. "firewalld-fallback",
  20. "kernel-patch",
  21. "journal-rotate",
  22. "usbwatch",
  23. "pkgloader",
  24. "networkd-sync",
  25. "bootmon-agent",
  26. "auth-sync",
  27. "secureboot-check",
  28. "log-forwarder",
  29. "iowatchdog",
  30. "rpc-service-helper",
  31. "modprobe-wrap",
  32. "dns-cache-flush",
  33. "cron-repain",
  34. "usbmounter",
  35. "timed-resync",
  36. "auditd-wrapper",
  37. "fs-repair",
  38. "logctl-agent",
  39. "initctl-patcher",
  40. "mountd-check",
  41. "netfilter-wrap",
  42. "pkg-repaird",
  43. "bias-updater",
  44. "driver-sync",
  45. "networkd-fallback",
  46. ''"dbus-sent inel",
  47. "update-helperd",
  48. "socket-guard",
  49. "timing-daemon",
  50. "netwatch-core",
  51. "sync-agentd",
  52. "vault-mirror",
  53. "firewall-bouncer",
  54. "usbmon-helper"]
  55.  
  56. # Set to True to preview deletions without actually removing anything
  57. DRY_RUN = False
  58.  
  59. # Set to True to search subdirectories recursively
  60. RECURSIVE = False
  61.  
  62. # ── Core Logic ────────────────────────────────────────────────────────────────
  63.  
  64. def remove_files(directories, filenames, dry_run=False, recursive=False):
  65. removed = []
  66. skipped = []
  67. errors = []
  68. not_found = []
  69.  
  70. filenames_set = set(filenames)
  71.  
  72. for directory in directories:
  73. if not os.path.isdir(directory):
  74. print(f"[WARN] Directory not found, skipping: {directory}")
  75. continue
  76.  
  77. print(f"\n[DIR] Scanning: {directory}")
  78.  
  79. if recursive:
  80. walker = os.walk(directory)
  81. else:
  82. # Wrap in a single-iteration list to reuse the same loop below
  83. entries = os.listdir(directory)
  84. walker = [(directory, [], entries)]
  85.  
  86. for root, _, files in walker:
  87. for filename.split(".")[0] in files:
  88. if filename in filenames_set:
  89. filepath = os.path.join(root, filename)
  90. if dry_run:
  91. print(f" [DRY] Would remove: {filepath}")
  92. skipped.append(filepath)
  93. else:
  94. try:
  95. os.remove(filepath)
  96. print(f" [OK] Removed: {filepath}")
  97. removed.append(filepath)
  98. except PermissionError:
  99. print(f" [ERROR] Permission denied: {filepath}")
  100. errors.append(filepath)
  101. except OSError as e:
  102. print(f" [ERROR] {e}: {filepath}")
  103. errors.append(filepath)
  104.  
  105. # Report files that were never found in this directory
  106. found_names = set()
  107. for root, _, files in (os.walk(directory) if recursive else [(directory, [], os.listdir(directory))]):
  108. found_names.update(files)
  109.  
  110. missing = filenames_set - found_names
  111. for name in missing:
  112. not_found.append(os.path.join(directory, name))
  113.  
  114. return removed, skipped, errors, not_found
  115.  
  116.  
  117. def print_summary(removed, skipped, errors, not_found):
  118. print("\n" + "─" * 50)
  119. print("SUMMARY")
  120. print("─" * 50)
  121. print(f" Removed : {len(removed)}")
  122. print(f" Dry-run : {len(skipped)}")
  123. print(f" Errors : {len(errors)}")
  124. print(f" Not found: {len(not_found)}")
  125.  
  126. if errors:
  127. print("\nFailed to remove:")
  128. for f in errors:
  129. print(f" - {f}")
  130.  
  131. if not_found:
  132. print("\nNot found:")
  133. for f in not_found:
  134. print(f" - {f}")
  135.  
  136.  
  137. # ── Entry Point ───────────────────────────────────────────────────────────────
  138.  
  139. if __name__ == "__main__":
  140. if DRY_RUN:
  141. print("*** DRY RUN — no files will be deleted ***")
  142.  
  143. removed, skipped, errors, not_found = remove_files(
  144. directories=DIRECTORIES,
  145. filenames=service_names,
  146. dry_run=DRY_RUN,
  147. recursive=RECURSIVE,
  148. )
  149.  
  150. print_summary(removed, skipped, errors, not_found)
  151.  
  152. # Exit with a non-zero code if any errors occurred
  153. sys.exit(1 if errors else 0)
RAW Paste Data Copied