<commit_before>import os

from moulinette import m18n
from moulinette.utils.log import getActionLogger
from moulinette.utils.process import call_async_output

from yunohost.tools import Migration, tools_migrations_state
from moulinette.utils.filesystem import rm


logger = getActionLogger("yunohost.migration")

VENV_REQUIREMENTS_SUFFIX = ".requirements_backup_for_bullseye_upgrade.txt"


def extract_app_from_venv_path(venv_path):

    venv_path = venv_path.replace("/var/www/", "")
    venv_path = venv_path.replace("/opt/yunohost/", "")
    venv_path = venv_path.replace("/opt/", "")
    return venv_path.split("/")[0]


def _get_all_venvs(dir, level=0, maxlevel=3):
    """
    Returns the list of all python virtual env directories recursively

    Arguments:
        dir - the directory to scan in
        maxlevel - the depth of the recursion
        level - do not edit this, used as an iterator
    """
    if not os.path.exists(dir):
        return []

    # Using os functions instead of glob, because glob doesn't support hidden
    # folders, and we need recursion with a fixed depth
    result = []
    for file in os.listdir(dir):
        path = os.path.join(dir, file)
        if os.path.isdir(path):
            activatepath = os.path.join(path, "bin", "activate")
            if os.path.isfile(activatepath) and os.path.isfile(
                path + VENV_REQUIREMENTS_SUFFIX
            ):
                result.append(path)
                continue
            if level < maxlevel:
                result += _get_all_venvs(path, level=level + 1)
    return result


class MyMigration(Migration):
    """
    After the update, recreate a python virtual env based on the previously
    generated requirements file
    """

    ignored_python_apps = [
        "calibreweb",
        "django-for-runners",
        "ffsync",
        "jupiterlab",
        "librephotos",
        "mautrix",
        "mediadrop",
        "mopidy",
        "pgadmin",
        "tracim",
        "synapse",
        "weblate",
    ]

    dependencies = ["migrate_to_bullseye"]
    state = None

    def is_pending(self):
        if not self.state:
            self.state = tools_migrations_state()["migrations"].get(
                "0024_rebuild_python_venv", "pending"
            )
        return self.state == "pending"

    @property
    def mode(self):
        if not self.is_pending():<commit_msg>venv rebuild: synapse's folder is named matrix-synapse<commit_after>import os

from moulinette import m18n
from moulinette.utils.log import getActionLogger
from moulinette.utils.process import call_async_output

from yunohost.tools import Migration, tools_migrations_state
from moulinette.utils.filesystem import rm


logger = getActionLogger("yunohost.migration")

VENV_REQUIREMENTS_SUFFIX = ".requirements_backup_for_bullseye_upgrade.txt"


def extract_app_from_venv_path(venv_path):

    venv_path = venv_path.replace("/var/www/", "")
    venv_path = venv_path.replace("/opt/yunohost/", "")
    venv_path = venv_path.replace("/opt/", "")
    return venv_path.split("/")[0]


def _get_all_venvs(dir, level=0, maxlevel=3):
    """
    Returns the list of all python virtual env directories recursively

    Arguments:
        dir - the directory to scan in
        maxlevel - the depth of the recursion
        level - do not edit this, used as an iterator
    """
    if not os.path.exists(dir):
        return []

    # Using os functions instead of glob, because glob doesn't support hidden
    # folders, and we need recursion with a fixed depth
    result = []
    for file in os.listdir(dir):
        path = os.path.join(dir, file)
        if os.path.isdir(path):
            activatepath = os.path.join(path, "bin", "activate")
            if os.path.isfile(activatepath) and os.path.isfile(
                path + VENV_REQUIREMENTS_SUFFIX
            ):
                result.append(path)
                continue
            if level < maxlevel:
                result += _get_all_venvs(path, level=level + 1)
    return result


class MyMigration(Migration):
    """
    After the update, recreate a python virtual env based on the previously
    generated requirements file
    """

    ignored_python_apps = [
        "calibreweb",
        "django-for-runners",
        "ffsync",
        "jupiterlab",
        "librephotos",
        "mautrix",
        "mediadrop",
        "mopidy",
        "pgadmin",
        "tracim",
        "synapse",
        "matrix-synapse",
        "weblate",
    ]

    dependencies = ["migrate_to_bullseye"]
    state = None

    def is_pending(self):
        if not self.state:
            self.state = tools_migrations_state()["migrations"].get(
                "0024_rebuild_python_venv", "pending"
            )
        return self.state == "pending"

    @property
    def mode(self):
        if not self.is_pending():
{'fraction_non_alphanumeric': 0.10022638402963573, 'fraction_numerical': 0.004116073266104137, 'mean_word_length': 2.4322033898305087, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1357945', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1483, 'n_words': 366}