-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathrmshit.py
More file actions
executable file
·146 lines (121 loc) · 3.56 KB
/
rmshit.py
File metadata and controls
executable file
·146 lines (121 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#! /usr/bin/env python3
import os
import shutil
from pathlib import Path
import yaml
DEFAULT_CONFIG = """
- ~/.adobe # Flash crap
- ~/.macromedia # Flash crap
- ~/.recently-used
- ~/.local/share/recently-used.xbel
- ~/.thumbnails
- ~/.gconfd
- ~/.gconf
- ~/.local/share/gegl-0.2
- ~/.FRD/log/app.log # FRD
- ~/.FRD/links.txt # FRD
- ~/.objectdb # FRD
- ~/.gstreamer-0.10
- ~/.pulse
- ~/.esd_auth
- ~/.config/enchant
- ~/.spicec # contains only log file; unconfigurable
- ~/.dropbox-dist
- ~/.parallel
- ~/.dbus
- ~/ca2 # WTF?
- ~/ca2~ # WTF?
- ~/.distlib/ # contains another empty dir, don't know which software creates it
- ~/.bazaar/ # bzr insists on creating files holding default values
- ~/.bzr.log
- ~/.nv/
- ~/.viminfo # configured to be moved to ~/.cache/vim/viminfo, but it is still sometimes created...
- ~/.npm/ # npm cache
- ~/.java/
- ~/.swt/
- ~/.oracle_jre_usage/
- ~/.openjfx/
- ~/.org.jabref.gui.JabRefMain/
- ~/.org.jabref.gui.MainApplication/
- ~/.jssc/
- ~/.tox/ # cache directory for tox
- ~/.pylint.d/
- ~/.qute_test/
- ~/.QtWebEngineProcess/
- ~/.qutebrowser/ # created empty, only with webengine backend
- ~/.asy/
- ~/.cmake/
- ~/.gnome/
- ~/unison.log
- ~/.texlive/
- ~/.w3m/
- ~/.subversion/
- ~/nvvp_workspace/ # created empty even when the path is set differently in nvvp
- ~/.ansible/
- ~/.fltk/
- ~/.vnc/
- ~/.local/share/Trash/ # VSCode puts deleted files here
"""
def get_size(path):
if Path(path).is_dir():
return sum(p.stat().st_size for p in Path(path).rglob("*"))
return Path(path).stat().st_size
def read_config():
"""
Reads the list of shitty files from a YAML config.
"""
config_dir = os.getenv("XDG_CONFIG_HOME", os.path.expanduser("~/.config/"))
config_path = Path(config_dir) / "rmshit.yaml"
# write default config if it does not exist
if not config_path.exists():
with open(config_path, "w") as f:
print(DEFAULT_CONFIG.strip(), file=f)
with open(config_path, "r") as f:
return yaml.safe_load(f)
def yesno(question, default="n"):
"""
Asks the user for YES or NO, always case insensitive.
Returns True for YES and False for NO.
"""
prompt = f"{question} (y/[n]) "
ans = input(prompt).strip().lower()
if not ans:
ans = default
if ans == "y":
return True
return False
def format_size(size_in_bytes):
"""Format file size in bytes to a human-readable string."""
if size_in_bytes <= 0:
return "0 bytes"
units = ["bytes", "KiB", "MiB", "GiB"]
size = float(size_in_bytes)
unit_index = min(int((size_in_bytes.bit_length() - 1) // 10), len(units) - 1)
size /= 1024**unit_index
return f"{size:.4g} {units[unit_index]}"
def rmshit():
shittyfiles = read_config()
print("Found shittyfiles:")
found = []
total_size = 0
for f in shittyfiles:
absf = os.path.expanduser(f)
if os.path.exists(absf):
found.append(absf)
size = get_size(absf)
total_size += size
print(f" {f} ({format_size(size)})")
if len(found) == 0:
print("No shitty files found :)")
return
if yesno("Remove all?", default="n"):
for f in found:
if os.path.isfile(f):
os.remove(f)
else:
shutil.rmtree(f)
print(f"All cleaned, {format_size(total_size)} freed.")
else:
print("No file removed")
if __name__ == "__main__":
rmshit()