mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
Detect venvs from /var/www/ and recursively
This commit is contained in:
parent
2d223c9158
commit
1ce13d631d
1 changed files with 22 additions and 15 deletions
|
@ -30,22 +30,30 @@ N_CURRENT_YUNOHOST = 4
|
||||||
N_NEXT_DEBAN = 11
|
N_NEXT_DEBAN = 11
|
||||||
N_NEXT_YUNOHOST = 11
|
N_NEXT_YUNOHOST = 11
|
||||||
|
|
||||||
VENV_BACKUP_PREFIX= "BACKUP_VENV_"
|
VENV_BACKUP_SUFFIX = "_BACKUP_VENV"
|
||||||
VENV_REQUIREMENTS_SUFFIX= "_req.txt"
|
VENV_REQUIREMENTS_SUFFIX = "_req.txt"
|
||||||
|
VENV_IGNORE = "VENVNOREGEN"
|
||||||
|
|
||||||
def _get_all_venvs():
|
def _get_all_venvs(dir,level=0,maxlevel=2):
|
||||||
result = []
|
result = []
|
||||||
exclude = glob.glob(f"/opt/{VENV_BACKUP_PREFIX}*")
|
for file in os.listdir(dir):
|
||||||
for x in glob.glob('/opt/*'):
|
path = os.path.join(dir,file)
|
||||||
if x not in exclude and os.path.isdir(x) and os.path.isfile(f"{x}/bin/activate"):
|
if os.path.isdir(path):
|
||||||
content = read_file(f"{x}/bin/activate")
|
if os.path.isfile(os.path.join(path,VENV_IGNORE)):
|
||||||
if "VIRTUAL_ENV" and "PYTHONHOME" in content:
|
continue
|
||||||
result.append(x)
|
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<maxlevel:
|
||||||
|
result+=_get_all_venvs(path,level=level+1)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def _generate_requirements():
|
def _generate_requirements():
|
||||||
|
|
||||||
venvs = _get_all_venvs()
|
venvs = _get_all_venvs("/opt/")+_get_all_venvs("/var/www/")
|
||||||
for venv in venvs:
|
for venv in venvs:
|
||||||
# Generate a requirements file from venv
|
# Generate a requirements file from venv
|
||||||
os.system(f"bash -c 'source {venv}/bin/activate && pip freeze > {venv}{VENV_REQUIREMENTS_SUFFIX} && deactivate'")
|
os.system(f"bash -c 'source {venv}/bin/activate && pip freeze > {venv}{VENV_REQUIREMENTS_SUFFIX} && deactivate'")
|
||||||
|
@ -53,15 +61,14 @@ def _generate_requirements():
|
||||||
|
|
||||||
def _rebuild_venvs():
|
def _rebuild_venvs():
|
||||||
|
|
||||||
venvs = _get_all_venvs()
|
venvs = _get_all_venvs("/opt/")+_get_all_venvs("/var/www/")
|
||||||
for venv in venvs:
|
for venv in venvs:
|
||||||
venvdirname = venv.split("/")[-1]
|
|
||||||
# Create a backup of the venv, in case there's a problem
|
# Create a backup of the venv, in case there's a problem
|
||||||
if os.path.isdir(f"/opt/{VENV_BACKUP_PREFIX}{venvdirname}"):
|
if os.path.isdir(venv+VENV_BACKUP_SUFFIX):
|
||||||
rm(f"/opt/{VENV_BACKUP_PREFIX}{venvdirname}", recursive=True)
|
rm(venv+VENV_BACKUP_SUFFIX, recursive=True)
|
||||||
backup = True
|
backup = True
|
||||||
try:
|
try:
|
||||||
cp(venv, f"/opt/{VENV_BACKUP_PREFIX}{venvdirname}", recursive=True)
|
cp(venv, venv+VENV_BACKUP_SUFFIX, recursive=True)
|
||||||
except:
|
except:
|
||||||
backup = False
|
backup = False
|
||||||
if backup and os.path.isfile(venv+VENV_REQUIREMENTS_SUFFIX):
|
if backup and os.path.isfile(venv+VENV_REQUIREMENTS_SUFFIX):
|
||||||
|
|
Loading…
Add table
Reference in a new issue