Advertisement
Smokahontas

PIP UNINSTALL BY SPECIFIC DATE

Jul 13th, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1. import os
  2. import pkg_resources
  3. from datetime import datetime
  4.  
  5. def get_package_installation_dates(target_date):
  6.     installed_packages = pkg_resources.working_set
  7.  
  8.     packages_installed_on_date = []
  9.  
  10.     target_date = datetime.strptime(target_date, '%Y-%m-%d').date()
  11.  
  12.     for package in installed_packages:
  13.         package_name = package.project_name
  14.         package_location = package.location
  15.         mod_time = datetime.fromtimestamp(os.path.getmtime(package_location)).date()
  16.  
  17.         if mod_time == target_date:
  18.             packages_installed_on_date.append(package_name)
  19.  
  20.     return packages_installed_on_date
  21.  
  22. def uninstall_packages(packages):
  23.     if not packages:
  24.         print("No packages to uninstall.")
  25.         return
  26.    
  27.     print(f"The following packages will be uninstalled:")
  28.     for package in packages:
  29.         print(package)
  30.  
  31.     confirmation = input("Do you want to proceed with uninstallation? (yes/no): ").strip().lower()
  32.     if confirmation == 'yes':
  33.         for package in packages:
  34.             os.system(f"pip uninstall -y {package}")
  35.         print("Packages uninstalled successfully.")
  36.     else:
  37.         print("Uninstallation canceled.")
  38.  
  39. # Example usage:
  40. if __name__ == "__main__":
  41.     target_date = '2024-07-13'  # Replace with the date you want to check
  42.     packages = get_package_installation_dates(target_date)
  43.  
  44.     if packages:
  45.         print(f"Packages installed on {target_date}:")
  46.         for package in packages:
  47.             print(package)
  48.        
  49.         uninstall_prompt = input("Do you want to uninstall these packages? (yes/no): ").strip().lower()
  50.         if uninstall_prompt == 'yes':
  51.             uninstall_packages(packages)
  52.         else:
  53.             print("Uninstallation canceled.")
  54.     else:
  55.         print(f"No packages found installed on {target_date}")
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement