From 4b6d6fdd2d704c1446e1235d9f126cb23e10e3a8 Mon Sep 17 00:00:00 2001 From: Papoteur Date: Thu, 5 May 2016 15:51:59 +0200 Subject: Add the count of renamed files and directories. --- mageiaSync/mageiaSyncExt.py | 35 ++++++++++++++++++++--------------- mageiaSync/mageiasync.py | 9 ++++----- 2 files changed, 24 insertions(+), 20 deletions(-) (limited to 'mageiaSync') diff --git a/mageiaSync/mageiaSyncExt.py b/mageiaSync/mageiaSyncExt.py index 5c79ec9..451f73e 100644 --- a/mageiaSync/mageiaSyncExt.py +++ b/mageiaSync/mageiaSyncExt.py @@ -200,21 +200,26 @@ class syncThread(QThread): self.list=[] self.quit() -def rename(directory,oldRelease,newRelease): - options=['d', 'f'] - for option in options: - commande=['find', directory, '-type' ,option, '-exec','rename',oldRelease,newRelease, '{}', '+'] - process = Popen(commande, shell=False, stdout=PIPE, stderr=PIPE) - process.poll() - while True : - item=process.stdout.readline().rstrip() - process.poll() - if process.returncode != None: - if process.returncode!=0: - item=process.stderr.readline().rstrip() - return 1, item - break - return 0 #"Success in renaming" +def rename(directory,oldRelease,newRelease, nbf, nbr): + # nbf: number of files renamed + # nbr: number of directories renamed + # the function is recursive + for entry in os.listdir(directory): + name = entry + entry=os.path.join(directory,entry) + print(entry, os.path.isfile(entry), os.path.isdir(entry), name.startswith(oldRelease)) + if os.path.isfile(entry): + if name.startswith(oldRelease): + newPath = os.path.join( os.path.dirname(entry) , name.replace(oldRelease, newRelease,1)) + os.rename(entry, newPath) + nbf = nbf + 1 + if os.path.isdir(entry): + nbf, nbr = rename(entry,oldRelease,newRelease, nbf, nbr) + if name.startswith(oldRelease) : + newPath = os.path.join( os.path.dirname(entry) , name.replace(oldRelease, newRelease,1)) + os.rename(entry, newPath) + nbr = nbr + 1 + return nbf, nbr class findIsos(QThread): endSignal=pyqtSignal(int) diff --git a/mageiaSync/mageiasync.py b/mageiaSync/mageiasync.py index 9dc4c7b..3582e2a 100644 --- a/mageiaSync/mageiasync.py +++ b/mageiaSync/mageiasync.py @@ -447,11 +447,10 @@ class IsosViewer(QMainWindow, mageiaSyncUI.Ui_mainWindow): self.rd.chooseDir.setText(self.destination) answer=self.rd.exec_() if answer: - returncode=mageiaSyncExt.rename(self.rd.chooseDir.text(),self.rd.oldRelease.text(),str(self.rd.newRelease.text())) - if returncode==1: - returnMsg="Error in renaming" - else: - returnMsg=self.tr("Success in renaming") + nbf, nbr = mageiaSyncExt.rename(self.rd.chooseDir.text(),\ + self.rd.oldRelease.text(),str(self.rd.newRelease.text()),\ + 0,0) + returnMsg=(self.tr("Renaming {0} files and {1} directories")).format(nbf, nbr) self.lvMessage(returnMsg) self.updateList() self.rd.close() -- cgit v1.2.1