gcp: reorganize __copyNextFile()

This commit is contained in:
Matteo Cypriani 2018-04-23 23:05:40 +02:00
rodič c4db1808da
revize cbd96480c5
1 změnil soubory, kde provedl 45 přidání a 38 odebrání

83
gcp
Zobrazit soubor

@ -310,49 +310,56 @@ class GCP():
self.__appendToList(abspath, dest_path, options)
def __copyNextFile(self):
"""Take the last file in the list, and launch the copy using glib io_watch event
@return: True a file was added, False else"""
if self.copy_list:
source_file, dest_path, options = self.copy_list.pop()
self.journal.startFile(source_file)
try:
source_fd = open(source_file, 'rb')
except:
self.journal.copyFailed()
self.journal.error("can't open source")
self.journal.closeFile()
return True
filename = os.path.basename(source_file)
assert(filename)
dest_file = self.__filename_fix(options.dest_file,options) if options.dest_file else self.__filename_fix(os.path.join(dest_path,filename),options)
if os.path.exists(dest_file) and not options.force:
warning (_("File [%s] already exists, skipping it!") % dest_file)
self.journal.copyFailed()
self.journal.error("already exists")
self.journal.closeFile()
source_fd.close()
return True
try:
dest_fd = open(dest_file, 'wb')
except:
self.journal.copyFailed()
self.journal.error("can't open dest")
self.journal.closeFile()
source_fd.close()
return True
GObject.io_add_watch(source_fd, GObject.IO_IN,self._copyFile,
(dest_fd, options), priority=GObject.PRIORITY_DEFAULT)
if not self.progress:
info(_("COPYING %(source)s ==> %(dest)s") % {"source":source_file, "dest":dest_file})
return True
else:
#Nothing left to copy, we quit
"""Takes the last file in the list and launches the copy using glib
io_watch event."""
if not self.copy_list:
# Nothing left to copy, we quit
if self.progress:
self.__pbar_finish()
self.journal.showErrors()
self.loop.quit()
source_file, dest_path, options = self.copy_list.pop()
self.journal.startFile(source_file)
try:
source_fd = open(source_file, 'rb')
except:
self.journal.copyFailed()
self.journal.error("can't open source")
self.journal.closeFile()
return
filename = os.path.basename(source_file)
assert(filename)
if options.dest_file:
dest_file = self.__filename_fix(options.dest_file, options)
else:
dest_file = self.__filename_fix(os.path.join(dest_path, filename),
options)
if os.path.exists(dest_file) and not options.force:
warning (_("File [%s] already exists, skipping it!") % dest_file)
self.journal.copyFailed()
self.journal.error("already exists")
self.journal.closeFile()
source_fd.close()
return
try:
dest_fd = open(dest_file, 'wb')
except:
self.journal.copyFailed()
self.journal.error("can't open dest")
self.journal.closeFile()
source_fd.close()
return
GObject.io_add_watch(source_fd, GObject.IO_IN,self._copyFile,
(dest_fd, options),
priority=GObject.PRIORITY_DEFAULT)
if not self.progress:
info(_("COPYING %(source)s ==> %(dest)s")
% {"source":source_file, "dest":dest_file})
def __copyFailed(self, reason, source_fd, dest_fd):
"""Write the failure in the journal and close files descriptors"""
self.journal.copyFailed()