-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiskAnalyzer.py
134 lines (110 loc) · 9.18 KB
/
DiskAnalyzer.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
import os
import sys
import base64
import urllib.parse
from pathlib import Path
from PyQt5 import QtWidgets, QtGui, QtCore
# Base64-encoded icon (replace this with your actual Base64 string)
icon_base64 = '''
iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAACXBIWXMAAB2HAAAdhwGP5fFlAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAC2lJREFUeJzNm3l01NUVxz/vN0tmEhIICUlgCIEQBCFshiSggFjrAlXUI6BCtVYqWitWjlqrQosboNZ6Kp7TU612USoapR4B9SCKYFkFSYAAArJIJmabrJNklt/8Xv8ICfPLMktmQvieM3/Mfffd+96d97u/++69I7hAyPrkeIxsNC+SUi4AkS0g7kLpBpDQCPKQgHdEH+8bJ2aNcAOIC6H8kvfP2lS0DQImXAh9wSCh0Ihyw7F56fYeN0DWJ8djNKd518Wy+VZIKFT6eCYrPa6owXzfxbZ5AAETpNN0b8QnYOQVL8QbfXKGQIxDMhYYKCBJglVI6nypiVkyqW+8TIzHNzAJbVAymI1R2EJUsLNbBsjJWR7rMljnKkLcJZHTAFPIkw0KWnoqvlFDUEcNAYOhO0voElcNtPJcTn8k8NTearaWNQdid4ZlgJFXvBBvVsUSiVwC9ItkoQBYY/BOHIGaMypqp+J/N9hIs7YY9ccmH9M22gPyh6x1TN6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Gjx4JjA3EJ6DSoCq3NTUddgbiCwUXzwk4h1iDWCglHwdgKRFCu76ysqAsAE/IuGicYDuI/mlz5iDEIpCTACOSk8A61ShW15cUVAcTECr+DxdfNp4rVF8PAAAAAElFTkSuQmCC
'''
def get_folder_size(folder):
"""Returns the size of the folder in bytes."""
total_size = 0
for dirpath, dirnames, filenames in os.walk(folder):
for f in filenames:
fp = os.path.join(dirpath, f)
if os.path.exists(fp): # Only count the size if the file exists
total_size += os.path.getsize(fp)
return total_size
def find_largest_folders(drive, top_n=10):
"""Finds the largest folders in the specified directory."""
folder_sizes = []
for folder in Path(drive).iterdir():
if folder.is_dir():
try:
folder_size = get_folder_size(folder)
folder_sizes.append((folder, folder_size))
except PermissionError:
pass
folder_sizes.sort(key=lambda x: x[1], reverse=True)
return folder_sizes[:top_n]
class Worker(QtCore.QThread):
result_ready = QtCore.pyqtSignal(list) # Signal to communicate when the result is ready
def __init__(self, folder):
super().__init__()
self.folder = folder
def run(self):
# Analyze the largest folders in the specified directory
largest_folders = find_largest_folders(self.folder, top_n=10)
self.result_ready.emit(largest_folders) # Emit the result
class DriveAnalyzerApp(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Drive Analyzer')
self.setGeometry(300, 300, 600, 400)
# Set window icon using Base64
icon_data = base64.b64decode(icon_base64)
pixmap = QtGui.QPixmap()
pixmap.loadFromData(icon_data)
icon = QtGui.QIcon(pixmap)
self.setWindowIcon(icon)
# Create layout
layout = QtWidgets.QVBoxLayout()
layout.setContentsMargins(10, 10, 10, 10)
# Create a label for the drive input
drive_label = QtWidgets.QLabel("Enter Drive (e.g., C:\\):", self)
layout.addWidget(drive_label)
# Create a line edit for the user to input the drive
self.drive_input = QtWidgets.QLineEdit(self)
self.drive_input.setPlaceholderText("Enter drive letter (e.g., C:\\)")
layout.addWidget(self.drive_input)
# Create a button to start the scan
self.scan_button = QtWidgets.QPushButton("Analyze Drive", self)
self.scan_button.clicked.connect(self.start_analysis)
self.scan_button.setStyleSheet("background-color: #4CAF50; color: white; font-weight: bold;")
layout.addWidget(self.scan_button)
# Create a text area to display the results and make it interactive
self.result_area = QtWidgets.QTextBrowser(self)
self.result_area.setReadOnly(True)
self.result_area.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse | QtCore.Qt.LinksAccessibleByMouse)
self.result_area.setStyleSheet("background-color: #f2f2f2; border: 1px solid #ccc; padding: 10px;")
self.result_area.anchorClicked.connect(self.analyze_folder) # Handle anchor clicks
layout.addWidget(self.result_area)
# Set the layout
self.setLayout(layout)
def start_analysis(self):
"""Start analyzing the drive or folder."""
folder = self.drive_input.text().strip() # Get the drive from the input field
if not folder or not isinstance(folder, str): # Ensure it's a valid folder path
folder = "C:\\" # Default to C: if input is invalid
self.result_area.append(f"Analyzing {folder} ...")
self.worker = Worker(folder) # Pass the folder path to the worker
self.worker.result_ready.connect(self.display_results)
self.worker.start()
def display_results(self, largest_folders):
self.result_area.clear() # Clear previous results
self.result_area.append("Top largest folders:\n")
for folder, size in largest_folders:
folder_info = f"<a href='{folder}'>{folder}</a>: {size / (1024 * 1024):.2f} MB"
self.result_area.append(folder_info)
def analyze_folder(self, url):
folder_path = url.toString() # Get the URL string
if folder_path.startswith("file:///"):
folder_path = folder_path.replace("file:///", "", 1) # Remove file:/// prefix
# Decode any URL-encoded characters
folder_path = urllib.parse.unquote(folder_path) # Decode the URL
folder_path = folder_path.replace('/', '\\') # Correct path format for Windows
# Ensure folder_path is a valid path and exists
if folder_path and Path(folder_path).exists():
self.start_analysis(folder_path)
else:
QtWidgets.QMessageBox.warning(self, "Invalid Path", "The selected folder does not exist or is invalid.")
# Main entry point
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
analyzer = DriveAnalyzerApp()
analyzer.show()
sys.exit(app.exec_())