Updated cleanup.py

This commit is contained in:
2Shirt 2018-11-23 18:09:31 -07:00
parent 7f7c220073
commit 1839edf84d
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

@ -6,7 +6,7 @@ def cleanup_adwcleaner():
"""Move AdwCleaner folders into the ClientDir.""" """Move AdwCleaner folders into the ClientDir."""
source_path = r'{SYSTEMDRIVE}\AdwCleaner'.format(**global_vars['Env']) source_path = r'{SYSTEMDRIVE}\AdwCleaner'.format(**global_vars['Env'])
source_quarantine = r'{}\Quarantine'.format(source_path) source_quarantine = r'{}\Quarantine'.format(source_path)
# Quarantine # Quarantine
if os.path.exists(source_quarantine): if os.path.exists(source_quarantine):
os.makedirs(global_vars['QuarantineDir'], exist_ok=True) os.makedirs(global_vars['QuarantineDir'], exist_ok=True)
@ -14,27 +14,24 @@ def cleanup_adwcleaner():
**global_vars) **global_vars)
dest_name = non_clobber_rename(dest_name) dest_name = non_clobber_rename(dest_name)
shutil.move(source_quarantine, dest_name) shutil.move(source_quarantine, dest_name)
# Delete source folder if empty # Delete source folder if empty
try: delete_empty_folders(source_path)
os.rmdir(source_path)
except OSError:
pass
# Main folder # Main folder
if os.path.exists(source_path): if os.path.exists(source_path):
os.makedirs(global_vars['ProgBackupDir'], exist_ok=True) os.makedirs(global_vars['LogDir'], exist_ok=True)
dest_name = r'{ProgBackupDir}\AdwCleaner_{Date-Time}'.format( dest_name = r'{LogDir}\Tools\AdwCleaner'.format(
**global_vars) **global_vars)
dest_name = non_clobber_rename(dest_name) dest_name = non_clobber_rename(dest_name)
shutil.move(source_path, dest_name) shutil.move(source_path, dest_name)
def cleanup_cbs(dest_folder): def cleanup_cbs(dest_folder):
"""Safely cleanup a known CBS archive bug under Windows 7. """Safely cleanup a known CBS archive bug under Windows 7.
If a CbsPersist file is larger than 2 Gb then the auto archive feature If a CbsPersist file is larger than 2 Gb then the auto archive feature
continually fails and will fill up the system drive with temp files. continually fails and will fill up the system drive with temp files.
This function moves the temp files and CbsPersist file to a temp folder, This function moves the temp files and CbsPersist file to a temp folder,
compresses the CbsPersist files with 7-Zip, and then opens the temp folder compresses the CbsPersist files with 7-Zip, and then opens the temp folder
for the user to manually save the backup files and delete the temp files. for the user to manually save the backup files and delete the temp files.
@ -43,7 +40,7 @@ def cleanup_cbs(dest_folder):
temp_folder = r'{backup_folder}\Temp'.format(backup_folder=backup_folder) temp_folder = r'{backup_folder}\Temp'.format(backup_folder=backup_folder)
os.makedirs(backup_folder, exist_ok=True) os.makedirs(backup_folder, exist_ok=True)
os.makedirs(temp_folder, exist_ok=True) os.makedirs(temp_folder, exist_ok=True)
# Move files into temp folder # Move files into temp folder
cbs_path = r'{SYSTEMROOT}\Logs\CBS'.format(**global_vars['Env']) cbs_path = r'{SYSTEMROOT}\Logs\CBS'.format(**global_vars['Env'])
for entry in os.scandir(cbs_path): for entry in os.scandir(cbs_path):
@ -59,7 +56,7 @@ def cleanup_cbs(dest_folder):
dest_name = r'{}\{}'.format(temp_folder, entry.name) dest_name = r'{}\{}'.format(temp_folder, entry.name)
dest_name = non_clobber_rename(dest_name) dest_name = non_clobber_rename(dest_name)
shutil.move(entry.path, dest_name) shutil.move(entry.path, dest_name)
# Compress CbsPersist files with 7-Zip # Compress CbsPersist files with 7-Zip
cmd = [ cmd = [
global_vars['Tools']['SevenZip'], global_vars['Tools']['SevenZip'],
@ -70,9 +67,9 @@ def cleanup_cbs(dest_folder):
def cleanup_desktop(): def cleanup_desktop():
"""Move known backup files and reports into the ClientDir.""" """Move known backup files and reports into the ClientDir."""
dest_folder = r'{ProgBackupDir}\Desktop_{Date-Time}'.format(**global_vars) dest_folder = r'{LogDir}\Tools'.format(**global_vars)
os.makedirs(dest_folder, exist_ok=True) os.makedirs(dest_folder, exist_ok=True)
desktop_path = r'{USERPROFILE}\Desktop'.format(**global_vars['Env']) desktop_path = r'{USERPROFILE}\Desktop'.format(**global_vars['Env'])
for entry in os.scandir(desktop_path): for entry in os.scandir(desktop_path):
# JRT, RKill, Shortcut cleaner # JRT, RKill, Shortcut cleaner
@ -80,12 +77,53 @@ def cleanup_desktop():
dest_name = r'{}\{}'.format(dest_folder, entry.name) dest_name = r'{}\{}'.format(dest_folder, entry.name)
dest_name = non_clobber_rename(dest_name) dest_name = non_clobber_rename(dest_name)
shutil.move(entry.path, dest_name) shutil.move(entry.path, dest_name)
# Remove dir if empty # Remove dir if empty
delete_empty_folders(dest_folder)
def delete_empty_folders(folder_path):
"""Delete all empty folders in path (depth first)."""
if not os.path.exists(folder_path) or not os.path.isdir(folder_path):
# Bail early (silently)
return
# Delete empty subfolders first
for item in os.scandir(folder_path):
if item.is_dir():
delete_empty_folders(item.path)
# Remove top folder
try: try:
os.rmdir(dest_folder) os.rmdir(folder_path)
except OSError: except OSError:
pass pass
def delete_registry_key(hive, key, recurse=False):
"""Delete a registry key and all it's subkeys."""
access = winreg.KEY_ALL_ACCESS
try:
if recurse:
# Delete all subkeys first
with winreg.OpenKeyEx(hive, key, 0, access) as k:
key_info = winreg.QueryInfoKey(k)
for x in range(key_info[0]):
subkey = r'{}\{}'.format(key, winreg.EnumKey(k, 0))
delete_registry_key(hive, subkey)
# Delete key
winreg.DeleteKey(hive, key)
except FileNotFoundError:
# Ignore
pass
def delete_registry_value(hive, key, value):
"""Delete a registry value."""
access = winreg.KEY_ALL_ACCESS
with winreg.OpenKeyEx(hive, key, 0, access) as k:
winreg.DeleteValue(k, value)
if __name__ == '__main__': if __name__ == '__main__':
print("This file is not meant to be called directly.") print("This file is not meant to be called directly.")
# vim: sts=4 sw=4 ts=4