Example #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
Example #2
0
def _copy_dir(source_path: Path, target_path: Path, overwrite: str,
              copy_permissions: bool, context: Optional[Path],
              callback: Optional[CopyCallback], already_written: int, size: int
              ) -> int:
    """Copy a directory recursively.
    """
    target_path_exists = target_path.exists() or target_path.is_symlink()

    if target_path_exists:
        if overwrite == 'always':
            if not target_path.is_dir():
                target_path.unlink()
        elif overwrite == 'raise':
            raise FileExistsError('Target path exists, not overwriting')
        elif overwrite == 'never':
            return already_written

    if not target_path.exists():
        logging.debug('Making new dir {}'.format(target_path))
        target_path.mkdir()

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

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

    for entry in source_path.iterdir():
        logging.debug('Recursively copying entry {}'.format(entry))
        already_written = _copy(entry, target_path / entry.name, overwrite,
                                copy_permissions, context, callback,
                                already_written, 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

    return already_written