<commit_before>import os
import sh
import threading
import tarfile

from juliabox.jbox_util import ensure_delete, make_sure_path_exists, JBoxCfg
from juliabox.vol import JBoxVol
from juliabox.interactive import SessContainer


class JBoxDefaultPackagesVol(JBoxVol):
    provides = [JBoxVol.JBP_PKGBUNDLE]

    FS_LOC = None
    LOCK = None
    CURRENT_BUNDLE = None
    BUNDLES_IN_USE = set()

    @staticmethod
    def configure():
        pkg_location = os.path.expanduser(JBoxCfg.get('pkg_location'))
        make_sure_path_exists(pkg_location)

        JBoxDefaultPackagesVol.FS_LOC = pkg_location
        JBoxDefaultPackagesVol.LOCK = threading.Lock()
        JBoxDefaultPackagesVol.refresh_disk_use_status()

    @staticmethod
    def _get_package_mounts_used(cid):
        used = []
        props = JBoxDefaultPackagesVol.dckr().inspect_container(cid)
        try:
            for _cpath, hpath in JBoxVol.extract_mounts(props):
                if hpath.startswith(JBoxDefaultPackagesVol.FS_LOC):
                    used.append(hpath.split('/')[-1])
        except:
            JBoxDefaultPackagesVol.log_error("error finding package mount points used in " + cid)
            return []
        return used

    @staticmethod
    def refresh_disk_use_status(container_id_list=None):
        JBoxDefaultPackagesVol.LOCK.acquire()
        bundles = set()
        try:
            if container_id_list is None:
                container_id_list = [cdesc['Id'] for cdesc in SessContainer.session_containers(allcontainers=True)]

            for cid in container_id_list:
                mount_points = JBoxDefaultPackagesVol._get_package_mounts_used(cid)
                bundles.update(mount_points)

            JBoxDefaultPackagesVol.BUNDLES_IN_USE = bundles
            JBoxDefaultPackagesVol.log_info("Packages in use: %r", bundles)
        finally:
            JBoxDefaultPackagesVol.LOCK.release()

    @staticmethod
    def get_disk_for_user(user_email):
        JBoxDefaultPackagesVol.log_debug("creating default packages mounted disk for %s", user_email)
        disk_path = os.path.join(JBoxDefaultPackagesVol.FS_LOC, JBoxDefaultPackagesVol.CURRENT_BUNDLE)
        pkgvol = JBoxDefaultPackagesVol(disk_path, user_email=user_email)
        return pkgvol

    @staticmethod
    def is_mount_path(fs_path):
        return fs_path.startswith(JBoxDefaultPackagesVol.FS_LOC)

    @staticmethod
    def get_disk_from_container(cid):
        mounts_used = JBoxDefaultPackagesVol._get_package_mounts_used(cid)
        if len(mounts_used) == 0:
            return None

        mount_used = mounts_used[0]
        disk_path = os.path.join(JBoxDefaultPackagesVol.FS_LOC, str(mount_used))<commit_msg>initialize package bundle if not done<commit_after>import os
import sh
import threading
import tarfile

from juliabox.jbox_util import ensure_delete, make_sure_path_exists, JBoxCfg
from juliabox.vol import JBoxVol
from juliabox.interactive import SessContainer


class JBoxDefaultPackagesVol(JBoxVol):
    provides = [JBoxVol.JBP_PKGBUNDLE]

    FS_LOC = None
    LOCK = None
    CURRENT_BUNDLE = None
    BUNDLES_IN_USE = set()

    @staticmethod
    def configure():
        pkg_location = os.path.expanduser(JBoxCfg.get('pkg_location'))
        make_sure_path_exists(pkg_location)

        JBoxDefaultPackagesVol.FS_LOC = pkg_location
        JBoxDefaultPackagesVol.LOCK = threading.Lock()
        JBoxDefaultPackagesVol.refresh_disk_use_status()

    @staticmethod
    def _get_package_mounts_used(cid):
        used = []
        props = JBoxDefaultPackagesVol.dckr().inspect_container(cid)
        try:
            for _cpath, hpath in JBoxVol.extract_mounts(props):
                if hpath.startswith(JBoxDefaultPackagesVol.FS_LOC):
                    used.append(hpath.split('/')[-1])
        except:
            JBoxDefaultPackagesVol.log_error("error finding package mount points used in " + cid)
            return []
        return used

    @staticmethod
    def refresh_disk_use_status(container_id_list=None):
        JBoxDefaultPackagesVol.LOCK.acquire()
        bundles = set()
        try:
            if container_id_list is None:
                container_id_list = [cdesc['Id'] for cdesc in SessContainer.session_containers(allcontainers=True)]

            for cid in container_id_list:
                mount_points = JBoxDefaultPackagesVol._get_package_mounts_used(cid)
                bundles.update(mount_points)

            JBoxDefaultPackagesVol.BUNDLES_IN_USE = bundles
            JBoxDefaultPackagesVol.log_info("Packages in use: %r", bundles)
        finally:
            JBoxDefaultPackagesVol.LOCK.release()

    @staticmethod
    def get_disk_for_user(user_email):
        JBoxDefaultPackagesVol.log_debug("creating default packages mounted disk for %s", user_email)
        if JBoxDefaultPackagesVol.FS_LOC is None:
            JBoxDefaultPackagesVol.configure()
        if JBoxDefaultPackagesVol.CURRENT_BUNDLE is None:
            JBoxDefaultPackagesVol.refresh_user_home_image()
        disk_path = os.path.join(JBoxDefaultPackagesVol.FS_LOC, JBoxDefaultPackagesVol.CURRENT_BUNDLE)
        pkgvol = JBoxDefaultPackagesVol(disk_path, user_email=user_email)
        return pkgvol

    @staticmethod
    def is_mount_path(fs_path):
        return fs_path.startswith(JBoxDefaultPackagesVol.FS_LOC)

    @staticmethod
    def get_disk_from_container(cid):
        mounts_used = JBoxDefaultPackagesVol._get_package_mounts_used(cid)
        if len(mounts_used) == 0:
            return None

        mount_used = mounts_used[0]
        disk_path = os.path.join(JBoxDefaultPackagesVol.FS_LOC, str(mount_used))
{'fraction_non_alphanumeric': 0.07196029776674938, 'fraction_numerical': 0.0010634526763559022, 'mean_word_length': 3.0538793103448274, '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': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '127583', 'n_tokens_mistral': 1780, 'n_tokens_neox': 1752, 'n_words': 302}