コード例 #1
0
    def copyfile(self, src, dst, *, follow_symlinks=True):
        """Copy data from src to dst.

        If follow_symlinks is not set and src is a symbolic link, a new
        symlink will be created instead of copying the file it points to.

        """
        if shutil._samefile(src, dst):
            raise shutil.SameFileError("{!r} and {!r} are the same file".format(src, dst))

        for fn in [src, dst]:
            try:
                st = os.stat(fn)
            except OSError:
                # File most likely does not exist
                pass
            else:
                # XXX What about other special files? (sockets, devices...)
                if shutil.stat.S_ISFIFO(st.st_mode):
                    raise shutil.SpecialFileError("`%s` is a named pipe" % fn)

        if not follow_symlinks and os.path.islink(src):
            os.symlink(os.readlink(src), dst)
        else:
            size = os.stat(src).st_size
            with open(src, 'rb') as fsrc:
                with open(dst, 'wb') as fdst:
                    self.copyfileobj(fsrc, fdst, callback=self.draw_copy_progress, total=size)
        return dst
コード例 #2
0
    def copyfile(src, dst, follow_symlinks=True):
        """Copy data from src to dst.

    If follow_symlinks is not set and src is a symbolic link, a new
    symlink will be created instead of copying the file it points to.

    """
        if shutil._samefile(src, dst):
            raise shutil.SameFileError(
                "{!r} and {!r} are the same file".format(src, dst))

        for fn in [src, dst]:
            try:
                st = os.stat(fn)
            except OSError:
                # File most likely does not exist
                pass
            else:
                # XXX What about other special files? (sockets, devices...)
                if stat.S_ISFIFO(st.st_mode):
                    raise shutil.SpecialFileError("`%s` is a named pipe" % fn)

        if not follow_symlinks and os.path.islink(src):
            os.symlink(os.readlink(src), dst)
        else:
            with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
                # Try to use sendfile if available for performance
                if not _copyfile_sendfile(fsrc, fdst):
                    # sendfile is not available or failed, fallback to copyfileobj
                    shutil.copyfileobj(fsrc, fdst)
        return dst
コード例 #3
0
def copyfile(src, dst, *, follow_symlinks=True):
    """Copy data from src to dst.

    If follow_symlinks is not set and src is a symbolic link, a new
    symlink will be created instead of copying the file it points to.

    """
    if shutil._samefile(src, dst):
        raise shutil.SameFileError("{!r} and {!r} are the same file".format(
            src, dst))

    for fn in [src, dst]:
        try:
            st = os.stat(fn)
        except OSError:
            # File most likely does not exist
            pass
        else:
            # XXX What about other special files? (sockets, devices...)
            if stat.S_ISFIFO(st.st_mode):
                raise shutil.SpecialFileError("`%s` is a named pipe" % fn)

    if not follow_symlinks and os.path.islink(src):
        os.symlink(os.readlink(src), dst)
    else:
        if _sys == 'Linux':
            subprocess.check_call(['cp', str(src), str(dst)])
        else:
            with open(src, 'rb') as fsrc:
                with open(dst, 'wb') as fdst:
                    shutil.copyfileobj(fsrc, fdst, length=16 * 1024 * 1024)
    return dst
コード例 #4
0
ファイル: utils.py プロジェクト: facebookresearch/FBPCS
 def copy_file(source: str, destination: str) -> None:
     """
     Copys folder from source to destination path
     """
     try:
         shutil.copy2(source, destination)
     except shutil.SameFileError as err:
         raise shutil.SameFileError(
             f"{source} and {destination} represents same file)"
         ) from err
     except PermissionError as err:
         raise PermissionError("Permission denied") from err
コード例 #5
0
ファイル: files.py プロジェクト: shahinism/cinemabit
def copy_file(src, dst, mk_dst=True):
    # mk_dst: make directory if doesn't exists
    if shutil._samefile(src, dst):
        msg = "{!r} and {!r} are the same file".format(src, dst)
        raise shutil.SameFileError(msg)
    else:
        if mk_dst:
            paths.mkdir(os.path.dirname(dst))
        source_size = os.stat(src).st_size
        with open(src, 'rb') as fsrc:
            with open(dst, 'wb') as fdst:
                copyfileobj(fsrc, fdst, source_size)

    return dst
コード例 #6
0
def copy_pro(src, dst, *, follow_symlinks=True,
             force=False, progress_min_size=1e7):
    """
    Copy data from src to dst.
    show progress bar in terminal for large files (> 10 MB by default)
    If follow_symlinks is not set and src is a symbolic link, a new
    symlink will be created instead of copying the file it points to.
    """
    src = Path(src)
    dst = Path(dst)
    
    # file/dir -> dir 
    if dst.is_dir():
        dst = dst / src.name

    # same file error?
    if shutil._samefile(src, dst):
        raise shutil.SameFileError("{!r} and {!r} are the same file"
                                   .format(src, dst))

    # destination exists error?
    if dst.exists():
        if force:
            dst.unlink()
        else:    
            raise IOError(f"copy failed, destination already exists: {dst}, use force=True to overwrite")

    size = os.stat(src).st_size

    # symlink copy?
    if not follow_symlinks and os.path.islink(src):
        os.symlink(os.readlink(src), dst)

    else:
        # file copy
        callback = print_copy_progress if size > progress_min_size else None
        with src.open(mode='rb') as fsrc:
            with dst.open(mode='wb+') as fdst:  # open or create
                __copyfileobj(fsrc, fdst, callback=callback, total=size)

    # copy permissions
    shutil.copymode(src, dst)

    # check success by size
    if not cmp(src, dst, shallow=True):
        raise OSError(f'Error, copy failed {src}: {size} b \
         -> {dst}: {os.stat(dst).st_size} b')

    return dst
コード例 #7
0
def copyfile(src, dst, *, follow_symlinks=True):
    if shutil._samefile(src, dst):
        raise shutil.SameFileError("{!r} and {!r} are the same file".format(
            src, dst))

    for fn in [src, dst]:
        try:
            st = os.stat(fn)
        except OSError:
            pass
        else:

            if shutil.stat.S_ISFIFO(st.st_mode):
                raise shutil.SpecialFileError("`%s` is a named pipe" % fn)

    if not follow_symlinks and os.path.islink(src):
        os.symlink(os.readlink(src), dst)
    else:
        size = os.stat(src).st_size
        with open(src, 'rb') as fsrc:
            with open(dst, 'wb') as fdst:
                copyfileobj(fsrc, fdst, callback=copy_progress, total=size)
    return dst
コード例 #8
0
ファイル: homework_4.py プロジェクト: Djet78/python_homework
def my_copy(src, dst):
    """
    Copy file or directory

    :param src: Path to File or Dir for copy
    :param dst: Destination path
    :return: None
    """
    logger = create_logger(level=20, filename="logs.txt")
    if not os.path.exists(src):
        logger.info("Wrong path given")
        raise TypeError("One of pathways is not exist")
    if os.path.isfile(src):
        try:
            shutil.copy2(src, dst)
        except shutil.SameFileError as why:
            logger.info("Create file with same name attempt")
            raise shutil.SameFileError(why)
    elif os.path.isdir(src):
        try:
            shutil.copytree(src, dst)
        except FileExistsError as why:
            logger.info("Create directory with same name attempt")
            raise FileExistsError(why)
コード例 #9
0
    def copyfile(src, dst, follow_symlinks=True):
        """Copy data from src to dst.
        If follow_symlinks is not set and src is a symbolic link, a new
        symlink will be created instead of copying the file it points to.
        """
        if shutil._samefile(src, dst):
            raise shutil.SameFileError(
                "{!r} and {!r} are the same file".format(src, dst))

        for fn in [src, dst]:
            try:
                st = os.stat(fn)
            except OSError as e:
                # File most likely does not exist
                debug(">>> {} doesn't exists [ {} ]".format(fn, e))
            else:
                # XXX What about other special files? (sockets, devices...)
                if stat.S_ISFIFO(st.st_mode):
                    raise shutil.SpecialFileError("`%s` is a named pipe" % fn)

        if not follow_symlinks and os.path.islink(src):
            debug(">>> creating symlink ...")
            os.symlink(os.readlink(src), dst)
        else:
            fs_src_type = FilesystemInfo().filesystem(src.encode('utf-8'))
            fs_dst_type = FilesystemInfo().filesystem(
                os.path.dirname(dst.encode('utf-8')))
            supported_fs = ['CIFS', 'SMB2']
            debug(">>> Source FS: {}".format(fs_src_type))
            debug(">>> Destination FS: {}".format(fs_dst_type))
            if fs_src_type in supported_fs and fs_dst_type in supported_fs:
                fsrc = os.open(src, os.O_RDONLY)
                fdst = os.open(dst, os.O_WRONLY | os.O_CREAT)

                CIFS_IOCTL_MAGIC = 0xCF
                CIFS_IOC_COPYCHUNK_FILE = IOW(CIFS_IOCTL_MAGIC, 3, c_int)

                # try copy file with COW support on Linux. If fail, fallback
                # to sendfile and if this is not available too, fallback
                # copyfileobj.
                ret = ioctl(fdst, CIFS_IOC_COPYCHUNK_FILE, fsrc)
                os.close(fsrc)
                os.close(fdst)
                if ret != 0:
                    debug("!!! failed {}".format(ret))
                    os.close(fsrc)
                    os.close(fdst)
                    # Try to use sendfile if available for performance
                    with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
                        if not _copyfile_sendfile(fsrc, fdst):
                            debug("!!! failed sendfile")
                            # sendfile is not available or failed, fallback
                            # to copyfileobj
                            shutil.copyfileobj(fsrc, fdst)
            else:
                with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
                    if not _copyfile_sendfile(fsrc, fdst):
                        # sendfile is not available or failed, fallback
                        # to copyfileobj
                        shutil.copyfileobj(src, dst)

        return dst
コード例 #10
0
ファイル: __init__.py プロジェクト: adevress/yo
def _copyfile(src, dst):
    if _samefile(src, dst):
        raise shutil.SameFileError("{!r} and {!r} are the same file".format(
            src, dst))
    else:
        copyfile(src, dst)