def transfer_file(src, dest, callback): """Transfer src to dest, calling callback(xferd) while doing so, where xferd is the size of the buffer successfully transferred src and dest must be two valid file paths. If pycp.options.move is True, remove src when done. """ if samefile(src, dest): raise TransferError("%s and %s are the same file!" % (src, dest)) if os.path.islink(src): target = os.readlink(src) #remove existing stuff if os.path.lexists(dest): os.remove(dest) os.symlink(target, dest) callback(0) return try: src_file = open(src, "rb") except IOError: raise TransferError("Could not open %s for reading" % src) try: dest_file = open(dest, "wb") except IOError: raise TransferError("Could not open %s for writing" % dest) # FIXME: a buffer size this small consumes a lot of CPU ... buff_size = 100 * 1024 xferd = 0 try: while True: data = src_file.read(buff_size) if not data: callback(0) break xferd = len(data) callback(xferd) dest_file.write(data) except IOError as err: mess = "Problem when transferring %s to %s\n" % (src, dest) mess += "Error was: %s" % err raise TransferError(mess) finally: src_file.close() dest_file.close() try: post_transfer(src, dest) except OSError as err: print("Warning: failed to finalize transfer of %s: %s" % (dest, err)) if pycp.options.move: try: debug("removing %s" % src) os.remove(src) except OSError: print("Warning: could not remove %s" % src)
def _parse_file(self, source, destination): """Parse a new source file """ debug(":: file %s -> %s" % (source, destination)) if os.path.isdir(destination): destination = os.path.join(destination, os.path.basename(source)) self.add(source, destination)
def _parse_file(self, source, destination): """Parse a new source file """ debug(":: file %s -> %s" % (source, destination)) if os.path.isdir(destination): destination = os.path.join( destination, os.path.basename(os.path.normpath(source))) self.add(source, destination)
def _parse_dir(self, source, destination): """Parse a new source directory """ debug(":: dir %s -> %s" % (source, destination)) if os.path.isdir(destination): destination = os.path.join(destination, os.path.basename(source)) debug(":: making dir %s" % destination) if not os.path.exists(destination): os.mkdir(destination) file_names = sorted(os.listdir(source)) if not pycp.options.all: file_names = [f for f in file_names if not f.startswith(".")] file_names = [os.path.join(source, f) for f in file_names] self.parse(file_names, destination) self.to_remove.append(source)
def _parse_dir(self, source, destination): """Parse a new source directory """ debug(":: dir %s -> %s" % (source, destination)) if os.path.isdir(destination): destination = os.path.join( destination, os.path.basename(os.path.normpath(source))) debug(":: making dir %s" % destination) if not os.path.exists(destination): os.mkdir(destination) file_names = sorted(os.listdir(source)) if not pycp.options.all: file_names = [f for f in file_names if not f.startswith(".")] file_names = [os.path.join(source, f) for f in file_names] self.parse(file_names, destination) self.to_remove.append(source)
except IOError, err: mess = "Problem when transferring %s to %s\n" % (src, dest) mess += "Error was: %s" % err raise TransferError(mess) finally: src_file.close() dest_file.close() try: post_transfer(src, dest) except OSError, err: print "Warning: failed to finalize transfer of %s: %s" % (dest, err) if pycp.options.move: try: debug("removing %s" % src) os.remove(src) except OSError: print "Warning: could not remove %s" % src def post_transfer(src, dest): """Handle stat of transferred file By default, preserve only permissions. If "preserve" option was given, preserve also utime and flags. """ src_st = os.stat(src) if hasattr(os, 'chmod'):