feat: cleanup empty dirs

This commit is contained in:
relikd
2025-09-10 12:54:01 +02:00
parent 364f154ec2
commit 9fbc733e0e

17
brew.py
View File

@@ -455,6 +455,7 @@ def cli_unlink(args: ArgParams) -> None:
# perform unlink # perform unlink
pkg.unlink(unlinkOpt=not args.bin, unlinkBin=True, dryRun=args.dry) pkg.unlink(unlinkOpt=not args.bin, unlinkBin=True, dryRun=args.dry)
File.cleanupEmptyDirs(Cellar.SHARE)
Log.main('==> Unlinked', 'binaries from' if args.bin else '', prev) Log.main('==> Unlinked', 'binaries from' if args.bin else '', prev)
@@ -505,6 +506,7 @@ def cli_toggle(args: ArgParams) -> None:
for prev in allVersions: for prev in allVersions:
prev.unlink(unlinkOpt=False, unlinkBin=True) prev.unlink(unlinkOpt=False, unlinkBin=True)
File.cleanupEmptyDirs(Cellar.SHARE)
if isActive: if isActive:
Log.info('==> disabled', pkg.name) Log.info('==> disabled', pkg.name)
@@ -575,6 +577,7 @@ def cli_cleanup(args: ArgParams) -> None:
total_savings += File.remove(link.path, dryRun=args.dry) total_savings += File.remove(link.path, dryRun=args.dry)
Log.main(Txt.freedDiskSpace(total_savings, dryRun=args.dry)) Log.main(Txt.freedDiskSpace(total_savings, dryRun=args.dry))
File.cleanupEmptyDirs(Cellar.SHARE)
def cli_export(args: ArgParams) -> None: def cli_export(args: ArgParams) -> None:
@@ -2332,6 +2335,8 @@ class UninstallQueue:
print() print()
print('The following packages will NOT be removed:') print('The following packages will NOT be removed:')
Utils.printInColumns(sorted(self.skips)) Utils.printInColumns(sorted(self.skips))
else:
File.cleanupEmptyDirs(Cellar.SHARE)
# ----------------------------------- # -----------------------------------
@@ -2736,6 +2741,18 @@ class File:
if file != '.DS_Store': if file != '.DS_Store':
yield os.path.join(base, file) yield os.path.join(base, file)
@staticmethod
def cleanupEmptyDirs(path: str) -> None:
''' Delete all empty directories (bottom-up) '''
for base, dirs, files in os.walk(path, topdown=False):
if files == ['.DS_Store']: # rm only if it is the last remaining
os.remove(os.path.join(base, '.DS_Store'))
for d in dirs:
try:
os.rmdir(os.path.join(base, d))
except OSError:
pass
@staticmethod @staticmethod
def folderSize(path: str) -> tuple[int, int]: def folderSize(path: str) -> tuple[int, int]:
'''Calculate total size of folder and all it's content (recursively)''' '''Calculate total size of folder and all it's content (recursively)'''