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
|
import sys
from PySide6.QtWidgets import Qdonlication, QLabel, QWidget, QPushButton, \
QHBoxLayout,QVBoxLayout, QProgressBar, QMessageBox, QLineEdit, QMenuBar
from PySide6.QtGui import QAction
from PySide6.QtCore import QDirIterator, QDir, QProcess, Signal, Qt, QCoredonlication
from don.services.tools import change_path
class copy_data(QWidget):
copyFinished = Signal(object)
def __init__(self, don):
super().__init__()
self.setWindowTitle("Tree Copy")
self.resize(500,400)
self.don = don
self.data = {
"nbDir": 0,
"nbFiles": 0,
"size": 0
}
self.script_xcopy = "xcopy '" + change_path(self.don['origin']) +"\*' " + change_path(self.don["working"]) + " /s"
print(self.don['origin'])
print(self.don['working'])
print(self.script_xcopy)
#-----------
self.copiedFiles = 0
self.init_ui()
def init_ui(self):
self.initialiseData()
self.repLabel = QLabel(f'Dir to copy: {self.data["nbDir"]}')
self.fileLabel = QLabel(f'File to copy: {self.data["nbFiles"]}')
self.sizeLabel = QLabel()
if self.data['size'] >= 10**9 :
self.sizeLabel.setText(f'Taille de la copie: {self.data["size"]/1024/1024/1024:.2f} Go')
else:
self.sizeLabel.setText(f'Taille de la copie: {self.data["size"]/1024/1024:.2f} Mo')
self.copiedFilesLEdit = QLabel()
self.pb = QProgressBar()
self.confirm = QPushButton('Confirm')
self.annul = QPushButton('Stop')
self.annul.setEnabled(0)
hb = QHBoxLayout()
hb.addStretch(1)
hb.addWidget(self.confirm)
hb.addWidget(self.annul)
vb = QVBoxLayout()
vb.addWidget(self.repLabel)
vb.addWidget(self.fileLabel)
vb.addWidget(self.sizeLabel)
vb.addStretch()
vb.addWidget(self.pb)
vb.addWidget(self.copiedFilesLEdit)
vb.addLayout(hb)
self.setLayout(vb)
self.confirm.clicked.connect(self.ps_process)
self.setWindowFlags(Qt.FramelessWindowHint)
def initialiseData(self):
rep = QDirIterator(self.don['origin'], QDir.NoDotAndDotDot | QDir.AllDirs, QDirIterator.Subdirectories )
files = QDirIterator(self.don['origin'], QDir.NoDotAndDotDot | QDir.Files, QDirIterator.Subdirectories )
while files.hasNext():
files.next()
info = files.fileInfo()
nom = info.fileName()
chemin = info.filePath()
self.data["size"] += info.size()
self.data["nbFiles"] += 1
while rep.hasNext():
rep.next()
self.data["nbDir"] += 1
def ps_process(self):
self.confirm.setEnabled(0)
self.annul.setEnabled(1)
self.powershell_process = QProcess()
self.powershell_process.readyReadStandardOutput.connect(self.powershell_process_stdout)
self.powershell_process.readyReadStandardError.connect(self.powershell_process_stderr)
self.powershell_process.finished.connect(self.cleanup_powershell_process)
self.powershell_process.start("powershell.exe", [self.script_xcopy])
self.powershell_process.waitForFinished(-1)
def powershell_process_stderr(self):
err = bytes(self.powershell_process.readAllStandardError()).decode("cp858")
pass
def powershell_process_stdout(self):
self.result = bytes(self.powershell_process.readAllStandardOutput()).decode("cp858")
print(self.result)
countFiles = self.result.split("\r\n")[:-1]
for countFile in countFiles:
self.copiedFiles +=1
self.pb.setValue(self.copiedFiles/self.data["nbFiles"]*100)
self.copiedFilesLEdit.setText(f'{self.copiedFiles} for {self.data["nbFiles"]}')
def cleanup_powershell_process(self):
self.powershell_process = None
self.copyFinished.emit(self.data)
self.close() |
Partager