configs/scripts/cleanup

91 lines
2.7 KiB
Text
Raw Permalink Normal View History

2024-11-01 01:27:54 +00:00
#!/usr/bin/env python
"""
Remove cache and other automatically created stuff that is always safe to remove when we need some more space in a pinch
"""
import argparse
from pathlib import Path
import subprocess
import shutil
from rich.progress import (
Progress,
SpinnerColumn,
FileSizeColumn,
TimeElapsedColumn,
TextColumn,
)
from rich import filesize
jetbrains_dir = Path().home() / "Library/Caches/JetBrains"
rubymine = []
for mine in jetbrains_dir.glob('RubyMine*'):
rubymine.append(mine / 'index')
rubymine.append(mine / 'cache')
pycharm = []
for charm in jetbrains_dir.glob('PyCharm*'):
pycharm.append(charm / 'index')
pycharm.append(charm / 'vcs-log')
pycharm.append(charm / 'caches')
paths = [
*rubymine,
*pycharm,
Path().home() / "Library/Caches/JetBrains/RubyMine*/caches",
Path().home() / "Library/Caches/JetBrains/PyCharm*/index",
Path().home() / "Library/Caches/JetBrains/PyCharm*/vcs-log",
Path().home() / "Library/Caches/JetBrains/PyCharm*/caches",
Path().home() / "Library/Caches/Yarn",
Path().home() / "Library/Caches/node-gyp",
Path().home() / "Library/Caches/pdm/http",
Path().home() / "Library/Caches/com.spotify.client/Default/Cache",
Path().home() / "Library/Caches/kdenlive",
Path().home() / "Library/Caches/pip/http-v2",
Path().home() / "Library/Caches/go-build",
]
paths = [Path(p) for p in paths]
def dir_size(path: Path) -> int:
return sum(file.stat().st_size for file in path.rglob('*'))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--check', action="store_true", help="just show sizes, no delete")
parser.add_argument('--keep-pycharm', action="store_true")
args = parser.parse_args()
if args.keep_pycharm:
paths = [p for p in paths if 'pycharm' not in str(p).lower()]
if args.check:
msg = "Checking clearable space"
else:
msg = "Clearing space"
total_size = 0
with Progress(
SpinnerColumn(),
*Progress.get_default_columns(),
TextColumn("{task.fields[size]}"),
transient=False
) as progress:
task = progress.add_task(f"[green] {msg}", total=len(paths), size="0")
for path in paths:
size = dir_size(path)
total_size += size
total_size_str = filesize.decimal(int(total_size))
file_msg = filesize.decimal(int(size))
if not args.check and path.exists() and size > 0:
try:
shutil.rmtree(path)
except Exception as e:
file_msg = f"Error removing: {e}"
progress.update(task, advance=1, size=total_size_str)
progress.log(file_msg, path)