-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean.py
162 lines (137 loc) · 5.05 KB
/
clean.py
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import re
import os
import plistlib
import prettytable
import subprocess
from colorama import Fore
import argparse
def red(s):
return Fore.LIGHTRED_EX + s + Fore.RESET
def green(s):
return Fore.LIGHTGREEN_EX + s + Fore.RESET
def yellow(s):
return Fore.LIGHTYELLOW_EX + s + Fore.RESET
def white(s):
return Fore.LIGHTWHITE_EX + s + Fore.RESET
def output(out):
tb = prettytable.PrettyTable()
tb.field_names = ['ID', 'Folder/File', 'Size']
for o in out:
tb.add_row(o[1:])
print(tb)
def parse_plist(path):
print(path)
if not os.path.isfile(path):
return None
with open(path, 'rb') as f:
pl = plistlib.load(f)
return [pl['CFBundleExecutable'], pl['CFBundleIdentifier'].rsplit('.')[1]]
def get_size(path):
total = 0
if os.path.isfile(path):
total += os.path.getsize(path)
else:
for entry in os.scandir(path):
if entry.is_file():
total += entry.stat().st_size
elif entry.is_dir():
total += get_size(entry.path)
return total
def size_human(num):
base = 1024
for x in ['B ', 'KB', 'MB', 'GB']:
if num < base and num > -base:
return "%3.0f%s" % (num, x)
num /= base
return "%3.0f %s" % (num, 'TB')
def get_clean_folders():
folders = [
'~/Library/Application Support',
'~/Library/Preferences/',
'~/Library/Caches/',
'~/Library/Logs/',
'~/Library/Application Support/CrashReporter/',
'~/Library/Saved Application State/',
'~/Library/LaunchAgents/',
'/Library/Caches',
'/Library/Logs',
'/Library/Preferences/',
'/Library/Application Support',
'/Applications',
'/private/var/db/receipts',
'/Library/StagedExtensions/Library/Extensions',
'/Library/StagedExtensions/Library/Application Support',
'/Library/Extensions/',
'~',
'~/.config/'
]
for folder1 in os.listdir('/var/folders/'):
folder1_path = os.path.join('/var/folders/', folder1)
if os.path.isdir(folder1_path) and folder1 != 'zz':
for folder2 in os.listdir(folder1_path):
folder2_path = os.path.join(folder1_path, folder2)
if os.path.isdir(folder2_path):
folders.append(folder2_path)
return map(os.path.expanduser, folders)
def find_folders(name):
real_clean_folders = []
id_ = 0
for folder in get_clean_folders():
try:
for f in os.listdir(folder):
if f.startswith('com.apple'):
continue
f_word = ''.join(re.findall('[a-zA-Z0-9.]', f)).lower()
if len([x for x in name if ''.join(re.findall('[a-zA-Z0-9.]', x)).lower() in f_word]):
path = os.path.join(folder, f)
size = size_human(int(get_size(path)))
real_clean_folders.append([path, red(str(id_)), yellow(path), green(size)])
id_ += 1
except FileNotFoundError:
pass
return real_clean_folders
def rm_rf(path):
p = subprocess.Popen('rm -rf "{path}"'.format(path=path), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate()
return stderr.decode('utf-8')
def main():
parser = argparse.ArgumentParser(description='A tool for uninstalling macos third-party software.')
parser.add_argument('p', metavar='xxx.app', help='Software,e.g.: Docker.app、\'"PlistEdit pro.app"\'. Or keyword, e.g.: Docker')
args = parser.parse_args()
path = '/Applications/{app}/Contents/Info.plist'.format(app=args.p)
name = []
deleted_folders = []
flag = parse_plist(path)
if flag:
print(green('[+]Searches [{}] Info.plist successfully'.format(args.p)))
name = flag
else:
print(yellow('[*]Searches app Info.plist failed, native input will be used'))
name.append(args.p)
folders = find_folders(name)
if len(folders):
print(green('[+]Found the following results:'))
output(folders)
read = input('[+]Please confirm the deleted id (e.g.: 0 1 3 or all):')
print(green('[+]Your choices: {choice}'.format(choice=read)))
if read == 'all':
deleted_folders = [x[0] for x in folders]
else:
read = [x for x in re.split('\s+', read) if len(x)]
read = [int(read[i]) for i in range(len(read)) if int(read[i]) < len(folders)]
deleted_folders = [folders[x][0] for x in read]
if len(deleted_folders) == 0:
print(red('[-]Wrong input'))
exit()
count = 0
for f in deleted_folders:
stderr = rm_rf(f)
if stderr:
print(red('[-]Failed to delete: {reason}'.format(reason=stderr)))
else:
print(green('[+]Successfully deleted: {f}'.format(f=f)))
count += 1
print(yellow('[*]{count} files cleaned'.format(count=count)))
else:
print(red('[-]No residual files found'))
main()