-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathclean.py
556 lines (482 loc) · 18.8 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
from PyQt5.QtCore import Qt, pyqtSignal, QThread
from PyQt5.QtGui import QPixmap, QPainter, QColor
from PyQt5.QtWidgets import QWidget
from qfluentwidgets import (
FluentIcon as FIF,
InfoBar,
InfoBarPosition,
InfoBarIcon,
TeachingTip,
TeachingTipTailPosition,
Dialog,
)
from mian_ui_ui import Ui_Form
import json
import os
import shutil
import psutil
import subprocess
import ctypes
try:
from plyer import notification
except Exception as e:
print(f"发生错误:{e}")
from datetime import datetime
import time
import os
# 导入日志模块
from logger import get_logger
# 获取日志记录器实例
logger = get_logger()
current_file = "清理内存和临时文件,减少电脑卡顿"
# 获取当前脚本的目录
script_directory = os.path.dirname(os.path.abspath(__file__))
settings_path = f"{script_directory}\WCMain\settings.json"
with open(settings_path, "r") as f:
settings_data = json.load(f)
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def get_drive_info(drive_letter):
partitions = psutil.disk_partitions()
for part in partitions:
if part.device.startswith(drive_letter):
try:
# 检查挂载点是否存在
if os.path.exists(part.mountpoint):
usage = psutil.disk_usage(part.mountpoint)
return usage.total, usage.used, usage.free, usage.percent
else:
print(f"挂载点 {part.mountpoint} 不存在")
logger.error(f"挂载点 {part.mountpoint} 不存在")
except PermissionError:
print(f"权限错误: 无法访问挂载点 {part.mountpoint}")
logger.error(f"权限错误: 无法访问挂载点 {part.mountpoint}")
except Exception as e:
print(f"获取磁盘使用情况时发生错误: {e}")
logger.error(f"获取磁盘使用情况时发生错误: {e}")
def WeatherLate(date_str):
now = datetime.now()
try:
specified_date = datetime.strptime(date_str, "%Y-%m-%d")
date_difference = now - specified_date
days_difference = date_difference.days
return days_difference
except ValueError as e:
print(f"日期格式错误: {e}")
logger.error(f"日期格式错误: {e}")
return None
def get_v():
# 获取C盘的使用情况
usage = psutil.disk_usage("C:\\") # 请注意在Windows上使用双反斜杠来表示路径
# 获取已用空间(以MB为单位)
used_space_mb = round(usage.used / (1024**2), 2) # 转换为MB
print(f"已用空间: {used_space_mb} MB")
return used_space_mb
def boost_main():
boost_prefetch("C:\\Windows\\Prefetch")
clean_temp_folder()
clean_system_logs()
clean_browser_cache()
memreduct()
def clean_main():
global settings_data
try:
boost_prefetch("C:\\Windows\\SoftwareDistribution\\Download")
except Exception as e:
print("软件分发缓存清理失败")
logger.error("软件分发缓存清理失败.是我在捣鬼>_<")
try:
boost_prefetch("C:\\Windows\\Prefetch")
except Exception as e:
print("预取文件清理失败")
logger.error("预取文件清理失败.是我在捣鬼>_>")
try:
boost_prefetch("C:\\Windows\\Temp")
except Exception as e:
print("临时文件清理失败")
logger.error("临时文件清理失败.是我在捣鬼<_<")
'''
TEST
try:
boost_prefetch("C:\\Windows\\System32\\Winevt\\Logs")
except Exception as e:
print("系统日志清理失败")
logger.error("系统日志清理失败.是我在捣鬼>_<")
'''
try:
boost_prefetch("C:\\Windows\\System32\\LogFiles")
except Exception as e:
print("系统日志清理失败")
logger.error("系统日志清理失败.是我在捣鬼>_<")
try:
boost_prefetch("C:\\Windows\\System32\\DriverStore\\FileRepository")
except Exception as e:
print("驱动程序缓存清理失败")
logger.error("驱动程序缓存清理失败.是我在捣鬼>_<")
try:
clean_temp_folder()
except Exception as e:
print("临时文件清理失败")
logger.error("临时文件清理失败.是我在捣鬼>_<")
try:
clean_system_logs()
except Exception as e:
print("系统日志清理失败")
logger.error("系统日志清理失败.是我在捣鬼>_<")
try:
#clean_application_cache()
print("应用程序缓存清理功能存在问题,已禁用")
except Exception as e:
print("应用程序缓存清理失败")
logger.error("应用程序缓存清理失败.是我在捣鬼>_<")
try:
clean_browser_cache()
except Exception as e:
print("浏览器缓存清理失败")
logger.error("浏览器缓存清理失败")
try:
delete_restore_points()
except Exception as e:
print("还原点清理失败")
logger.error("还原点清理失败.是我在捣鬼>_<")
try:
clean_tmp_files()
except Exception as e:
print("临时文件清理失败")
logger.error("临时文件清理失败_2.是我在捣鬼>_<")
try:
user_list = settings_data["includePath"]
for path in user_list:
boost_prefetch(path)
except Exception as e:
print("用户自定义路径清理失败")
logger.error("用户自定义路径清理失败.是我在捣鬼>_<")
def clean_application_cache():
# 获取当前用户的用户名
username = os.getlogin()
# 构建应用程序缓存目录的路径
cache_dir = f"C:\\Users\\{username}\\AppData\\Local\\Packages"
# 遍历缓存目录下的所有文件夹
for root, dirs, files in os.walk(cache_dir):
for dir in dirs:
# 构建每个应用程序缓存文件夹的路径
app_cache_dir = os.path.join(root, dir)
# 如果文件夹名称以"Cache"或"cache"结尾,则删除该文件夹
if dir.lower().endswith("cache"):
try:
shutil.rmtree(app_cache_dir)
print(f"Deleted {app_cache_dir}")
logger.info(f"Deleted {app_cache_dir}")
except Exception as e:
print(f"Failed to delete {app_cache_dir}. Reason: {e}")
logger.error(f"Failed to delete {app_cache_dir}. Reason: {e}")
def clean_tmp_files():
global current_file
# 定义要遍历的文件夹路径
folder_path = "C:\\" # C盘根目录
# 获取管理员权限
if not is_admin():
raise PermissionError(
"You don't have permission to delete files in this folder."
)
# 遍历文件夹及其子文件夹
for root, dirs, files in os.walk(folder_path):
for file in files:
# 检查文件是否为.tmp文件, .cache文件, 或 .msp文件
if file.endswith(".tmp") or file.endswith(".cache") or file.endswith(".msp"):
# 获取文件的完整路径
file_path = os.path.join(root, file)
# 删除文件
try:
current_file = file_path
os.remove(file_path)
print(f"已删除:{file_path}")
except Exception as e:
print(f"Failed to delete: {file_path}, Error: {e}")
def boost_prefetch(folder_path):
global current_file
if os.path.exists(folder_path):
print(f"The path {folder_path} exists.")
else:
print(f"The path {folder_path} does not exist.")
return
# 获取管理员权限
if not is_admin():
raise PermissionError(
"You don't have permission to delete files in this folder."
)
# 遍历文件夹并删除文件
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
try:
if os.path.isfile(file_path):
current_file = file_path
os.remove(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
print(f"Deleted: {file_path}")
except Exception as e:
print(f"Failed to delete: {file_path}, Error: {e}")
def clean_temp_folder():
temp_folder = os.environ.get("TEMP")
boost_prefetch(temp_folder)
def clean_browser_cache():
browser_cache_folders = {
"Chrome": os.path.join(
os.getenv("LOCALAPPDATA"), "Google\\Chrome\\User Data\\Default\\Cache"
),
"Edge": os.path.join(
os.getenv("LOCALAPPDATA"), "Microsoft\\Edge\\User Data\\Default\\Cache"
),
}
print(browser_cache_folders)
for browser, folder in browser_cache_folders.items():
print(f"清理 {browser} 浏览器缓存:{folder}")
logger.info(f"清理 {browser} 浏览器缓存:{folder}")
boost_prefetch(folder)
def clean_system_logs():
log_folder = os.path.join(os.getenv("SystemRoot"), "Logs")
print(f"清理系统日志文件夹:{log_folder}")
logger.info(f"清理系统日志文件夹:{log_folder}")
boost_prefetch(log_folder)
# 删除所有还原点的函数
def delete_restore_points():
try:
print("清理旧的系统还原点...")
logger.info("清理旧的系统还原点...")
subprocess.run("vssadmin Delete Shadows /all /quiet", shell=True, check=True)
print("成功清理旧的系统还原点")
logger.info("成功清理旧的系统还原点")
except subprocess.CalledProcessError as e:
print("error:", e.returncode)
logger.error("error:", e.returncode)
def memreduct(threshold=100,exclude_processes=["System", "Idle", "svchost.exe"]):
os.system(r"WCMain\memreduct.exe /clean /silent")
def is_desktop_application(process):
try:
return process.name() != "Python" and process.name() != "mian.exe" and process.as_dict(attrs=['name', 'cmdline'])['cmdline'] is not None
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
return False
class CleanThread(QThread):
operationCompleted = pyqtSignal()
operationFailed_permissionError = pyqtSignal()
def run(self):
try:
clean_main()
self.operationCompleted.emit()
except PermissionError as e:
self.operationFailed_permissionError.emit()
class BoostThread(QThread):
operationCompleted = pyqtSignal()
operationFailed_permissionError = pyqtSignal()
def run(self):
try:
boost_main()
self.operationCompleted.emit()
except PermissionError as e:
self.operationFailed_permissionError.emit()
class FlashThread(QThread):
operationCompleted = pyqtSignal(int)
operationFailed_permissionError = pyqtSignal()
def run(self):
while True:
memory_info = psutil.virtual_memory()
memory = int(memory_info.percent)
self.operationCompleted.emit(memory)
time.sleep(2)
class clean_page(QWidget, Ui_Form):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setupUi(self)
# 设置流畅图标
self.pushButton.setIcon(FIF.BROOM)
# 设置进度环取值范围和当前值
self.progressBar.setRange(0, 100)
self.progressBar.setValue(30)
self.progressBar.setStrokeWidth(13)
# 显示进度环内文本
self.progressBar.setTextVisible(True)
self.pushButton.clicked.connect(self.boost)
self.widget_2.clicked.connect(self.clean)
self.widget_3.clicked.connect(self.RunSpaceSniffer)
self.widget_4.clicked.connect(self.RunAppDataCleaner)
self.show_work()
self.run_flash()
def RunSpaceSniffer(self):
try:
exe_path = "WCMain/SpaceSniffer.exe"
subprocess.Popen([exe_path])
except Exception as e:
self.RunAPIError_bar()
def RunAppDataCleaner(self):
try:
exe_path = "WCMain/AppDataCleaner.exe"
subprocess.Popen([exe_path])
except Exception as e:
self.RunAPIError_bar()
def on_operation_completed(self):
self.info_bar()
print("完成")
def on_operation_failed_PermissionError(self):
self.PermissionError_bar()
self.pushButton.setEnabled(True)
self.widget_2.setEnabled(True)
self.pushButton.setText("立即加速")
def run_flash(self):
self.thread = FlashThread()
self.thread.operationCompleted.connect(self.update_ui)
self.thread.start()
def update_ui(self, index):
global current_file
self.progressBar.setValue(index)
file_name = os.path.basename(current_file)
self.label_3.setText(f"{file_name}")
def boost(self):
print("优化加速")
self.v0 = get_v()
self.pushButton.setEnabled(False)
self.widget_2.setEnabled(False)
self.pushButton.setText("优化加速中...")
self.thread = BoostThread()
self.thread.operationCompleted.connect(self.after_clean)
self.thread.operationFailed_permissionError.connect(
self.on_operation_failed_PermissionError
)
self.thread.start()
def clean(self):
print("深度清理")
self.v0 = get_v()
self.pushButton.setEnabled(False)
self.widget_2.setEnabled(False)
self.pushButton.setText("深度清理中...")
self.thread = CleanThread()
self.thread.operationCompleted.connect(self.deep_after_clean)
self.thread.operationFailed_permissionError.connect(
self.on_operation_failed_PermissionError
)
self.thread.start()
def show_work(self):
drive_letter = "C:"
info = get_drive_info(drive_letter)
if info:
total, used, free, percent = info
# print(f"C 盘总空间: {total / (1024 ** 3):.2f} GB")
# print(f"C 盘已用空间: {used / (1024 ** 3):.2f} GB")
self.label_9.setText(
f"{free / (1024 ** 3):.2f}GB可用,共{total / (1024 ** 3):.2f}GB"
)
# print(f"C 盘使用百分比: {percent}%")
else:
print(f"找不到驱动器 {drive_letter}")
logger.error(f"找不到驱动器 {drive_letter}")
def clear_failed(self):
self.pushButton.setEnabled(True)
self.widget_2.setEnabled(True)
# self.showTeachingTip(content="清理失败,请重试!")
def after_clean(self):
self.pushButton.setText("立即加速")
self.info_bar()
global current_file
current_file = "清理内存和临时文件,减少电脑卡顿"
self.v1 = get_v()
if int(self.v0 - self.v1) > 1024:
message = (
f"加速完成!\n清理出{format((self.v0 - self.v1)/1024, '.2f')}GB空间"
)
logger.info(message)
else:
message = f"加速完成!\n清理出{format(self.v0 - self.v1, '.2f')}MB空间"
logger.info(message)
self.pushButton.setEnabled(True)
self.widget_2.setEnabled(True)
self.showTeachingTip(content=message)
self.show_work()
def deep_after_clean(self):
self.pushButton.setText("立即加速")
self.info_bar()
global current_file
current_file = "清理内存和临时文件,减少电脑卡顿"
self.v1 = get_v()
if int(self.v0 - self.v1) > 1024:
message = (
f"加速完成!\n清理出{format((self.v0 - self.v1)/1024, '.2f')}GB空间"
)
logger.info(message)
else:
message = f"加速完成!\n清理出{format(self.v0 - self.v1, '.2f')}MB空间"
logger.info(message)
self.pushButton.setEnabled(True)
self.widget_2.setEnabled(True)
self.showTeachingTip(content=message)
self.show_work()
w = Dialog("是否继续清理?", "是否使用'磁盘清理'工具继续清理?", self)
w.yesButton.setText(f"是(Y)")
w.cancelButton.setText(f"否(C)")
if w.exec():
try:
os.popen("cleanmgr")
except Exception as e:
print(f"error{e}")
else:
print("Cancel")
w = Dialog(
"是否继续清理?",
f"是否从'存储'删除临时文件?\n注意:此操作可能并不适用于所有Windows版本。",
self,
)
w.yesButton.setText(f"是(Y)")
w.cancelButton.setText(f"否(C)")
if w.exec():
try:
os.popen("start ms-settings:storagesense")
except Exception as e:
print(f"error{e}")
else:
print("Cancel")
def showTeachingTip(self, content="设置成功!重启软件后生效。"):
TeachingTip.create(
target=self.widget,
icon=InfoBarIcon.SUCCESS,
title="Success",
content=content,
isClosable=True,
tailPosition=TeachingTipTailPosition.TOP,
duration=2000,
parent=self,
)
def info_bar(self):
InfoBar.success(
title="success",
content="清理中,请稍后...",
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP,
duration=2000,
parent=self,
)
def PermissionError_bar(self):
InfoBar.warning(
title="没有权限删除此文件",
content="请以管理员身份运行",
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP,
duration=5000,
parent=self,
)
def RunAPIError_bar(self):
InfoBar.warning(
title="出错了!😭",
content="未知错误,请联系开发者",
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP,
duration=5000,
parent=self,
)
if __name__ == "__main__":
logger.error("请运行 main.py ,而不是 clean.py")
raise RuntimeError("请运行 main.py ,而不是 clean.py")