from pathlib import Path


def get_and_mark_bundle_cache_version(bundle_base: str, *,
                                      previously_bundled: bool) -> int:
    """
    Check and return the bundle cache version.

    The marker filename is `.bundle_cache_version`.

    :param str bundle_base: The bundle directory
    :param bool previously_bundled: true if the user has previously used
    this workspace to build a bundle
    :returns: the cache layout version to use
    """
    marker_path = Path(bundle_base) / '.bundle_cache_version'
    bundle_cache_version = 2
    if previously_bundled:
        bundle_cache_version = 1
        if marker_path.is_file():
            bundle_cache_version = \
                int(marker_path.read_text().rstrip())
    marker_path.write_text(str(bundle_cache_version) + '\n')
    return bundle_cache_version