Skip to content

Commit

Permalink
Fix reverse dependency logic, cleanup output and added support for a
Browse files Browse the repository at this point in the history
whitelist of packages not to uninstall.
This is a newline separated list of package names in a file
called /etc/deblint/whitelist
Added content to README.md
  • Loading branch information
frode-h committed Apr 15, 2024
1 parent c548c15 commit 512b5d4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
Binary file added __pycache__/deblint.cpython-311.pyc
Binary file not shown.
44 changes: 40 additions & 4 deletions deblint
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,44 @@
#!/usr/bin/python3

import os
import time
from datetime import datetime, timedelta
import sys

import apt
import re

whitelistfile = '/etc/deblint/whitelist'

def import_package_whitelist(file_path):
"""
Import a newline-separated list of package names from a file.
Args:
file_path (str): The path to the file.
Returns:
set: A set of package names.
"""
if not os.path.isfile(file_path):
print(f"Error: File {file_path} does not exist.")
return set()

with open(file_path, 'r') as file:
packages = file.read().splitlines()

# Define a regular expression pattern for valid package names
pattern = re.compile('^[a-z0-9.:_-]+$')

# Use the pattern to filter the packages
packages = {pkg for pkg in packages if pkg and pattern.match(pkg)}

if not packages:
print(f"Warning: No valid package names found in {file_path}.")

return packages

whitelist = import_package_whitelist(whitelistfile)

def get_package_files(package_name, cache):
"""
Expand Down Expand Up @@ -79,12 +114,13 @@ def filter_safe_to_remove_packages(inactive_packages, cache):
safe_to_remove = set(inactive_packages)

for package_name in inactive_packages:
if package_name in whitelist:
continue
reverse_deps = get_reverse_dependencies(package_name, cache)
for dep_pkg in reverse_deps:
if dep_pkg in safe_to_remove:
if dep_pkg not in safe_to_remove: # changed from safe_to_remove_packages to safe_to_remove
safe_to_remove.discard(package_name)
break

return list(safe_to_remove)


Expand All @@ -100,10 +136,10 @@ def main():
days_inactive = int(sys.argv[1]) if len(sys.argv) > 1 else 7
inactive_packages = check_inactive_packages(days_inactive, cache)
safe_to_remove_packages = filter_safe_to_remove_packages(inactive_packages, cache)

print("break")
if safe_to_remove_packages:
apt_command = "sudo apt remove " + ' '.join(safe_to_remove_packages)
print("Inactive packages that can be safely removed:", safe_to_remove_packages)
print("Inactive packages that can be safely removed have been identified.")
print("Command to remove these packages:", apt_command)

if input("Do you want to uninstall these packages now? (y/n) ").lower() == 'y':
Expand Down

0 comments on commit 512b5d4

Please sign in to comment.