<commit_before>from kubernetes import client

from django.conf import settings

from libs.paths.data_paths import validate_persistence_data
from libs.paths.exceptions import VolumeNotFoundError
from libs.paths.outputs_paths import validate_persistence_outputs
from scheduler.spawners.templates import constants


def get_volume_mount(volume, volume_mount=None, read_only=False):
    return client.V1VolumeMount(name=volume, mount_path=volume_mount, read_only=read_only)


def get_volume(volume, claim_name=None, host_path=None):
    if claim_name:
        pv_claim = client.V1PersistentVolumeClaimVolumeSource(claim_name=claim_name)
        return client.V1Volume(name=volume, persistent_volume_claim=pv_claim)

    if host_path:
        return client.V1Volume(
            name=volume,
            host_path=client.V1HostPathVolumeSource(path=host_path))

    empty_dir = client.V1EmptyDirVolumeSource()
    return client.V1Volume(name=volume, empty_dir=empty_dir)


def get_volume_from_definition(volume_name, volume_settings):
    if volume_name not in volume_settings:
        raise VolumeNotFoundError('Volume with name `{}` was defined in specification, '
                                  'but was not found'.format(volume_name))
    volumes = []
    volume_mounts = []
    definition = volume_settings[volume_name]
    mount_path = definition.get('mountPath')
    claim_name = definition.get('existingClaim')
    host_path = definition.get('hostPath')
    read_only = definition.get('readOnly', False)
    if mount_path:
        volumes.append(get_volume(volume=volume_name,
                                  claim_name=claim_name,
                                  host_path=host_path))
        volume_mounts.append(get_volume_mount(volume=volume_name,
                                              volume_mount=mount_path,
                                              read_only=read_only))

    return volumes, volume_mounts


def get_pod_data_volume(persistence_data):
    persistence_data = validate_persistence_data(persistence_data=persistence_data)
    volumes = []
    volume_mounts = []
    for persistence_name in persistence_data:
        persistence_volumes, persistence_volume_mounts = get_volume_from_definition(
            volume_name=persistence_name,
            volume_settings=settings.PERSISTENCE_DATA)
        volumes += persistence_volumes
        volume_mounts += persistence_volume_mounts
    return volumes, volume_mounts


def get_pod_outputs_volume(persistence_outputs):
    persistence_outputs = validate_persistence_outputs(persistence_outputs=persistence_outputs)<commit_msg>Add read only to claim in pod volumes<commit_after>from kubernetes import client

from django.conf import settings

from libs.paths.data_paths import validate_persistence_data
from libs.paths.exceptions import VolumeNotFoundError
from libs.paths.outputs_paths import validate_persistence_outputs
from scheduler.spawners.templates import constants


def get_volume_mount(volume, volume_mount=None, read_only=False):
    return client.V1VolumeMount(name=volume, mount_path=volume_mount, read_only=read_only)


def get_volume(volume, claim_name=None, host_path=None, read_only=None):
    if claim_name:
        pv_claim = client.V1PersistentVolumeClaimVolumeSource(claim_name=claim_name,
                                                              read_only=read_only)
        return client.V1Volume(name=volume, persistent_volume_claim=pv_claim)

    if host_path:
        return client.V1Volume(
            name=volume,
            host_path=client.V1HostPathVolumeSource(path=host_path))

    empty_dir = client.V1EmptyDirVolumeSource()
    return client.V1Volume(name=volume, empty_dir=empty_dir)


def get_volume_from_definition(volume_name, volume_settings):
    if volume_name not in volume_settings:
        raise VolumeNotFoundError('Volume with name `{}` was defined in specification, '
                                  'but was not found'.format(volume_name))
    volumes = []
    volume_mounts = []
    definition = volume_settings[volume_name]
    mount_path = definition.get('mountPath')
    claim_name = definition.get('existingClaim')
    host_path = definition.get('hostPath')
    read_only = definition.get('readOnly', False)
    if mount_path:
        volumes.append(get_volume(volume=volume_name,
                                  claim_name=claim_name,
                                  host_path=host_path,
                                  read_only=read_only))
        volume_mounts.append(get_volume_mount(volume=volume_name,
                                              volume_mount=mount_path,
                                              read_only=read_only))

    return volumes, volume_mounts


def get_pod_data_volume(persistence_data):
    persistence_data = validate_persistence_data(persistence_data=persistence_data)
    volumes = []
    volume_mounts = []
    for persistence_name in persistence_data:
        persistence_volumes, persistence_volume_mounts = get_volume_from_definition(
            volume_name=persistence_name,
            volume_settings=settings.PERSISTENCE_DATA)
        volumes += persistence_volumes
        volume_mounts += persistence_volume_mounts
    return volumes, volume_mounts


def get_pod_outputs_volume(persistence_outputs):
    persistence_outputs = validate_persistence_outputs(persistence_outputs=persistence_outputs)
{'fraction_non_alphanumeric': 0.06619822485207101, 'fraction_numerical': 0.0025887573964497044, 'mean_word_length': 3.2557041699449254, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '407822', 'n_tokens_mistral': 1467, 'n_tokens_neox': 1458, 'n_words': 257}