From 1ce13d631dd7463de65cf8348aca43930afd3645 Mon Sep 17 00:00:00 2001 From: "theo@manjaro" Date: Fri, 22 Jul 2022 14:15:21 +0200 Subject: [PATCH] Detect venvs from /var/www/ and recursively --- .../0021_migrate_to_bullseye.py | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/yunohost/data_migrations/0021_migrate_to_bullseye.py b/src/yunohost/data_migrations/0021_migrate_to_bullseye.py index 0fb698c32..56044d48f 100644 --- a/src/yunohost/data_migrations/0021_migrate_to_bullseye.py +++ b/src/yunohost/data_migrations/0021_migrate_to_bullseye.py @@ -30,22 +30,30 @@ N_CURRENT_YUNOHOST = 4 N_NEXT_DEBAN = 11 N_NEXT_YUNOHOST = 11 -VENV_BACKUP_PREFIX= "BACKUP_VENV_" -VENV_REQUIREMENTS_SUFFIX= "_req.txt" +VENV_BACKUP_SUFFIX = "_BACKUP_VENV" +VENV_REQUIREMENTS_SUFFIX = "_req.txt" +VENV_IGNORE = "VENVNOREGEN" -def _get_all_venvs(): +def _get_all_venvs(dir,level=0,maxlevel=2): result = [] - exclude = glob.glob(f"/opt/{VENV_BACKUP_PREFIX}*") - for x in glob.glob('/opt/*'): - if x not in exclude and os.path.isdir(x) and os.path.isfile(f"{x}/bin/activate"): - content = read_file(f"{x}/bin/activate") - if "VIRTUAL_ENV" and "PYTHONHOME" in content: - result.append(x) + for file in os.listdir(dir): + path = os.path.join(dir,file) + if os.path.isdir(path): + if os.path.isfile(os.path.join(path,VENV_IGNORE)): + continue + activatepath = os.path.join(path,"bin","activate") + if os.path.isfile(activatepath): + content = read_file(activatepath) + if "VIRTUAL_ENV" and "PYTHONHOME" in content: + result.append(path) + continue + if level {venv}{VENV_REQUIREMENTS_SUFFIX} && deactivate'") @@ -53,15 +61,14 @@ def _generate_requirements(): def _rebuild_venvs(): - venvs = _get_all_venvs() + venvs = _get_all_venvs("/opt/")+_get_all_venvs("/var/www/") for venv in venvs: - venvdirname = venv.split("/")[-1] # Create a backup of the venv, in case there's a problem - if os.path.isdir(f"/opt/{VENV_BACKUP_PREFIX}{venvdirname}"): - rm(f"/opt/{VENV_BACKUP_PREFIX}{venvdirname}", recursive=True) + if os.path.isdir(venv+VENV_BACKUP_SUFFIX): + rm(venv+VENV_BACKUP_SUFFIX, recursive=True) backup = True try: - cp(venv, f"/opt/{VENV_BACKUP_PREFIX}{venvdirname}", recursive=True) + cp(venv, venv+VENV_BACKUP_SUFFIX, recursive=True) except: backup = False if backup and os.path.isfile(venv+VENV_REQUIREMENTS_SUFFIX):