-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyCopy.pyw
210 lines (167 loc) · 7.09 KB
/
PyCopy.pyw
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
import sys
import os
import shutil
import threading
from PySide6 import QtWidgets, QtCore, QtGui
class FileCopier(QtCore.QObject):
progressChanged = QtCore.Signal(int)
completed = QtCore.Signal()
errorOccurred = QtCore.Signal(str)
def __init__(self, source_path, destination_path, overwrite=False, refresh=False, parent=None):
super().__init__(parent)
self.source_path = source_path
self.destination_path = destination_path
self.overwrite = overwrite
self.refresh = refresh
self.total_files = self._count_total_files()
self.copied_files = 0
self.thread = threading.Thread(target=self._copy_files_and_dirs)
self.copied_folders = 0
self.log = []
def _count_total_files(self):
total_files = 0
for _, _, files in os.walk(self.source_path):
total_files += len(files)
return total_files
def _copy_files_and_dirs(self):
try:
for root, _, files in os.walk(self.source_path):
destination_root = os.path.join(self.destination_path, os.path.relpath(root, self.source_path))
if not os.path.exists(destination_root):
os.makedirs(destination_root)
self.copied_folders += 1
for file in files:
source_file = os.path.join(root, file)
destination_file = os.path.join(destination_root, file)
if os.path.exists(destination_file) and not self.overwrite:
if self.refresh and os.path.getmtime(source_file) <= os.path.getmtime(destination_file):
continue
shutil.copy2(source_file, destination_file)
self.copied_files += 1
self.progressChanged.emit(self.copied_files)
self.completed.emit()
except Exception as e:
self.errorOccurred.emit(str(e))
self.log.append(f"Error: {str(e)}")
def start(self):
self.thread.start()
def isRunning(self):
return self.thread.is_alive()
class CopyWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PyCopy")
self.resize(500, 330)
self.source_label = QtWidgets.QLabel("Source Path:")
self.source_input = QtWidgets.QLineEdit()
self.source_browse = QtWidgets.QPushButton("Browse")
self.source_browse.clicked.connect(self.browse_source)
self.destination_label = QtWidgets.QLabel("Destination Path:")
self.destination_input = QtWidgets.QLineEdit()
self.destination_browse = QtWidgets.QPushButton("Browse")
self.destination_browse.clicked.connect(self.browse_destination)
self.overwrite_checkbox = QtWidgets.QCheckBox("Overwrite existing files")
self.refresh_checkbox = QtWidgets.QCheckBox("Copy only new/modified files")
self.start_button = QtWidgets.QPushButton("Start Copying")
self.start_button.clicked.connect(self.start_copying)
self.progress_bar = QtWidgets.QProgressBar()
self.info_label = QtWidgets.QLabel("File and folder permissions are copied as well.")
self.info_label.setObjectName("infoLabel")
source_layout = QtWidgets.QHBoxLayout()
source_layout.addWidget(self.source_input)
source_layout.addWidget(self.source_browse)
destination_layout = QtWidgets.QHBoxLayout()
destination_layout.addWidget(self.destination_input)
destination_layout.addWidget(self.destination_browse)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.source_label)
layout.addLayout(source_layout)
layout.addWidget(self.destination_label)
layout.addLayout(destination_layout)
layout.addWidget(self.overwrite_checkbox)
layout.addWidget(self.refresh_checkbox)
layout.addWidget(self.start_button)
layout.addWidget(self.progress_bar)
font = QtGui.QFont()
font.setItalic(True)
font.setPointSize(7)
self.info_label.setFont(font)
layout.addWidget(self.info_label, alignment=QtCore.Qt.AlignCenter)
container = QtWidgets.QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
layout.addStretch(1)
self.apply_styles()
self.log = []
def apply_styles(self):
styles = """
QWidget {
font-size: 16px;
}
#infoLabel {
color: darkgray;
}
QPushButton {
background-color: #2196F3;
color: white;
border: none;
padding: 10px 20px;
text-align: center;
border-radius: 4px;
}
QPushButton:hover {
background-color: #1976D2;
}
QProgressBar {
border: 2px solid #2196F3;
border-radius: 5px;
text-align: center;
}
QProgressBar::chunk {
background-color: #2196F3;
}
"""
self.setStyleSheet(styles)
def start_copying(self):
self.start_button.setStyleSheet("background-color: palevioletred;")
self.start_button.setText("Copying...")
source_path = self.source_input.text()
destination_path = self.destination_input.text()
overwrite = self.overwrite_checkbox.isChecked()
refresh = self.refresh_checkbox.isChecked()
self.file_copier = FileCopier(source_path, destination_path, overwrite, refresh)
self.file_copier.progressChanged.connect(self.update_progress_bar)
self.file_copier.completed.connect(self.on_completed)
self.file_copier.errorOccurred.connect(self.on_error)
self.file_copier.log = self.log # Pass log reference
self.file_copier.start()
def update_progress_bar(self, value):
percent_complete = (value / self.file_copier.total_files) * 100
self.progress_bar.setValue(percent_complete)
def on_completed(self):
self.log.append("Copying completed!")
self.show_log()
self.reset_button()
def on_error(self, error_message):
self.log.append(f"Error: {error_message}")
self.show_log()
self.reset_button()
def reset_button(self):
self.start_button.setStyleSheet("background-color: #2196F3;")
self.start_button.setText("Start Copying")
def show_log(self):
with open("copy_log.txt", "w") as f:
f.write("\n".join(self.log))
def browse_source(self):
path = QtWidgets.QFileDialog.getExistingDirectory(self, "Select Source Directory")
if path:
self.source_input.setText(path)
def browse_destination(self):
path = QtWidgets.QFileDialog.getExistingDirectory(self, "Select Destination Directory")
if path:
self.destination_input.setText(path)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = CopyWindow()
window.show()
sys.exit(app.exec())