Mercurial > gcp
comparison gcp @ 6:5f53dc5beec9
vfat names incompatibility fix
- bug fixed in getFsType(bad fs returned in most cases)
- removing gettext (_("")) in a debug fonction with filename, as the filename can be with invalid utf-8, and then cause an exception. A better solution can probably be found later.
- No more progress bar when the files have a null sum
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 27 Sep 2010 16:44:25 +0800 |
parents | 7edbb0e0d9dd |
children | a110d31482f7 |
comparison
equal
deleted
inserted
replaced
5:7edbb0e0d9dd | 6:5f53dc5beec9 |
---|---|
128 self.dbus_name = dbus.service.BusName(const_DBUS_INTERFACE, session_bus) | 128 self.dbus_name = dbus.service.BusName(const_DBUS_INTERFACE, session_bus) |
129 self.dbus_object = DbusObject(self, session_bus, const_DBUS_PATH) | 129 self.dbus_object = DbusObject(self, session_bus, const_DBUS_PATH) |
130 | 130 |
131 self.copy_list = [] | 131 self.copy_list = [] |
132 self.mounts = self.__getMountPoints() | 132 self.mounts = self.__getMountPoints() |
133 self.files_left = 0 | |
134 self.bytes_total = 0 | 133 self.bytes_total = 0 |
135 self.bytes_copied = 0 | 134 self.bytes_copied = 0 |
136 | 135 |
137 def getFsType(self, path): | 136 def getFsType(self, path): |
138 fs='' | 137 fs = '' |
138 last_mount_point = '' | |
139 for mount in self.mounts: | 139 for mount in self.mounts: |
140 if path.startswith(mount) and len(self.mounts[mount])>=len(fs): | 140 if path.startswith(mount) and len(mount)>=len(last_mount_point): |
141 fs = self.mounts[mount] | 141 fs = self.mounts[mount] |
142 last_mount_point = mount | |
142 return fs | 143 return fs |
143 | 144 |
144 def __getMountPoints(self): | 145 def __getMountPoints(self): |
145 """Parse /proc/mounts to get currently mounted devices""" | 146 """Parse /proc/mounts to get currently mounted devices""" |
146 #TODO: reparse when a new device is added/a device is removed | 147 #TODO: reparse when a new device is added/a device is removed |
157 | 158 |
158 def __appendToList(self, path, dest_path, options): | 159 def __appendToList(self, path, dest_path, options): |
159 """Add a file to the copy list | 160 """Add a file to the copy list |
160 @param path: absolute path of file | 161 @param path: absolute path of file |
161 @param options: options as return by optparse""" | 162 @param options: options as return by optparse""" |
162 debug (_("Adding to copy list: %(path)s ==> %(dest_path)s (%(fs_type)s)") % {"path":path, | 163 debug ("Adding to copy list: %(path)s ==> %(dest_path)s (%(fs_type)s)" % {"path":path, |
163 "dest_path":dest_path, | 164 "dest_path":dest_path, |
164 "fs_type":self.getFsType(dest_path)} ) | 165 "fs_type":self.getFsType(dest_path)} ) |
165 try: | 166 try: |
166 self.bytes_total+=os.path.getsize(path) | 167 self.bytes_total+=os.path.getsize(path) |
167 self.files_left+=1 | |
168 self.copy_list.insert(0,(path, dest_path, options)) | 168 self.copy_list.insert(0,(path, dest_path, options)) |
169 except OSError,e: | 169 except OSError,e: |
170 error(_("Can't copy %(path)s: %(exception)s") % {'path':path, 'exception':e.strerror}) | 170 error(_("Can't copy %(path)s: %(exception)s") % {'path':path, 'exception':e.strerror}) |
171 | 171 |
172 | 172 |
173 def __appendDirToList(self, dirpath, dest_path, options): | 173 def __appendDirToList(self, dirpath, dest_path, options): |
174 """Add recursively directory to the copy list | 174 """Add recursively directory to the copy list |
175 @param path: absolute path of dir | 175 @param path: absolute path of dir |
176 @param options: options as return by optparse""" | 176 @param options: options as return by optparse""" |
177 #We first check that the dest path exists, and create it if needed | 177 #We first check that the dest path exists, and create it if needed |
178 dest_path = self.__filename_fix(dest_path, options) | |
178 if not os.path.exists(dest_path): | 179 if not os.path.exists(dest_path): |
179 debug (_("Creating directory %s") % dest_path) | 180 debug (_("Creating directory %s") % dest_path) |
180 os.makedirs(dest_path) #TODO: check permissions | 181 os.makedirs(dest_path) #TODO: check permissions |
181 #TODO: check that dest_path is an accessible dir, | 182 #TODO: check that dest_path is an accessible dir, |
182 # and skip file/write error in log if needed | 183 # and skip file/write error in log if needed |
219 if self.copy_list: | 220 if self.copy_list: |
220 source_file, dest_path, options = self.copy_list.pop() | 221 source_file, dest_path, options = self.copy_list.pop() |
221 source_fd = open(source_file, 'rb') | 222 source_fd = open(source_file, 'rb') |
222 filename = os.path.basename(source_file) | 223 filename = os.path.basename(source_file) |
223 assert(filename) | 224 assert(filename) |
224 dest_file = os.path.join(dest_path,filename) | 225 dest_file = self.__filename_fix(os.path.join(dest_path,filename),options) |
225 if os.path.exists(dest_file) and not options.force: | 226 if os.path.exists(dest_file) and not options.force: |
226 warning (_("File [%s] already exists, skipping it !") % dest_file) | 227 warning (_("File [%s] already exists, skipping it !") % dest_file) |
227 return True | 228 return True |
228 dest_fd = open(dest_file, 'wb') | 229 dest_fd = open(dest_file, 'wb') |
229 | 230 |
252 | 253 |
253 if len(buff) != self.buffer_size: | 254 if len(buff) != self.buffer_size: |
254 source_fd.close() | 255 source_fd.close() |
255 dest_fd.close() | 256 dest_fd.close() |
256 self.__post_copy(source_fd.name, dest_fd.name, options) | 257 self.__post_copy(source_fd.name, dest_fd.name, options) |
257 self.files_left -= 1 | |
258 assert (self.files_left >= 0) | |
259 return False | 258 return False |
260 return True | 259 return True |
260 | |
261 def __filename_fix(self, filename, options): | |
262 if self.getFsType(filename) == 'vfat' and options.fs_fix: | |
263 filename = filename.replace('\\','_')\ | |
264 .replace(':',';')\ | |
265 .replace('*','+')\ | |
266 .replace('?','')\ | |
267 .replace('"','\'')\ | |
268 .replace('<','[')\ | |
269 .replace('>',']')\ | |
270 .replace('|','!') | |
271 return filename | |
261 | 272 |
262 def __post_copy(self, source_file, dest_file, options): | 273 def __post_copy(self, source_file, dest_file, options): |
263 """Do post copy traitement (mainly managing --preserve option)""" | 274 """Do post copy traitement (mainly managing --preserve option)""" |
264 st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, st_size, st_atime, st_mtime, st_ctime = os.stat(source_file) | 275 st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, st_size, st_atime, st_mtime, st_ctime = os.stat(source_file) |
265 #TODO: complete log in case of errors | 276 #TODO: complete log in case of errors |
279 assert(self.progress) | 290 assert(self.progress) |
280 try: | 291 try: |
281 if self.pbar.maxval != self.bytes_total: | 292 if self.pbar.maxval != self.bytes_total: |
282 self.pbar.maxval = self.bytes_total | 293 self.pbar.maxval = self.bytes_total |
283 except AttributeError: | 294 except AttributeError: |
295 if not self.bytes_total: | |
296 #No progress bar if the files have a null size | |
297 return | |
284 self.pbar = ProgressBar(self.bytes_total,[_("Progress: "),Percentage()," ",Bar()," ",FileTransferSpeed()," ",ETA()]) | 298 self.pbar = ProgressBar(self.bytes_total,[_("Progress: "),Percentage()," ",Bar()," ",FileTransferSpeed()," ",ETA()]) |
285 self.pbar.start() | 299 self.pbar.start() |
286 self.pbar.update(self.bytes_copied) | 300 self.pbar.update(self.bytes_copied) |
287 | 301 |
288 def __pbar_finish(self): | 302 def __pbar_finish(self): |
318 help=_("force overwriting of existing files")) | 332 help=_("force overwriting of existing files")) |
319 | 333 |
320 parser.add_option("--preserve", action="store", default='mode,ownership,timestamps', | 334 parser.add_option("--preserve", action="store", default='mode,ownership,timestamps', |
321 help=_("preserve the specified attributes")) | 335 help=_("preserve the specified attributes")) |
322 | 336 |
323 parser.add_option("--no-unicode-fix", action="store_true", default=False, | 337 parser.add_option("--no-unicode-fix", action="store_false", dest='unicode_fix', default=True, |
324 help=_("don't fixe name encoding errors")) #TODO | 338 help=_("don't fixe name encoding errors")) #TODO |
325 | 339 |
340 parser.add_option("--no-fs-fix", action="store_false", dest='fs_fix', default=True, | |
341 help=_("don't fixe filesystem name incompatibily")) #TODO | |
342 | |
326 parser.add_option("--no-progress", action="store_false", dest="progress", default=True, | 343 parser.add_option("--no-progress", action="store_false", dest="progress", default=True, |
327 help=_("deactivate progress bar")) | 344 help=_("deactivate progress bar")) |
328 | 345 |
329 parser.add_option("-v", "--verbose", action="store_true", default=False, | 346 parser.add_option("-v", "--verbose", action="store_true", default=False, |
330 help=_("Show what is currently done")) | 347 help=_("Show what is currently done")) |