Beispiel #1
0
def _get_approx_size(path: Path) -> int:
    count = 0
    if not path.is_symlink() and path.is_file():
        count += path.size()
    elif not path.is_symlink() and path.is_dir():
        for subdir in path.iterdir():
            count += _get_approx_size(subdir)
    return count
Beispiel #2
0
def _copy_file(source_path: Path, target_path: Path, overwrite: str,
               copy_permissions: bool, callback: Optional[CopyCallback],
               already_written: int, size: int) -> int:
    """Copy a file.

    Returns the number of bytes written.
    """
    target_path_exists = target_path.exists() or target_path.is_symlink()

    if not target_path_exists or overwrite == 'always':
        logger.debug('Copying file from %s to %s', source_path, target_path)

        if not target_path.is_symlink() and target_path.is_dir():
            target_path.rmdir(recursive=True)
        elif target_path.exists():
            target_path.unlink()
        target_path.touch()

        perms = dict()
        for permission in Permission:
            perms[permission] = target_path.has_permission(permission)

        try:
            target_path.chmod(0o600)
        except UnsupportedOperationError:
            pass

        target_path.streaming_write(
                _call_back(
                    callback, perf_counter() + 1.0, already_written, size,
                    source_path.streaming_read()))

        already_written += source_path.size()

        try:
            for permission in Permission:
                if copy_permissions:
                    target_path.set_permission(
                        permission, source_path.has_permission(permission))
                else:
                    target_path.set_permission(
                        permission,
                        perms[permission]
                        and source_path.has_permission(permission))
        except UnsupportedOperationError:
            pass

    elif overwrite == 'raise':
        raise FileExistsError('Target path exists, not overwriting')

    return already_written