From 51506c6d6f61a2c5f017f181584138660a26314f Mon Sep 17 00:00:00 2001 From: Place Jean-Marie Date: Sun, 14 Nov 2021 10:33:37 +0100 Subject: [PATCH] migration des logos_dept scodoc7 -> scodoc9 --- scodoc.py | 9 ++++++ tools/__init__.py | 1 + tools/migrate_from_scodoc7.sh | 3 ++ tools/migrate_scodoc7_logos.py | 53 ++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 tools/migrate_scodoc7_logos.py diff --git a/scodoc.py b/scodoc.py index cfea19d7b1..6b34d941c9 100755 --- a/scodoc.py +++ b/scodoc.py @@ -22,6 +22,7 @@ from app import models from app.auth.models import User, Role, UserRole from app.models import ScoPreference +from app.scodoc.sco_logos import make_logo_local from app.models import Formation, UniteEns, Module from app.models import FormSemestre, FormsemestreInscription from app.models import ModuleImpl, ModuleImplInscription @@ -340,6 +341,14 @@ def migrate_scodoc7_dept_archives(dept: str): # migrate-scodoc7-dept-archives tools.migrate_scodoc7_dept_archives(dept) +@app.cli.command() +@click.argument("dept", default="") +@with_appcontext +def migrate_scodoc7_dept_logos(dept: str = ""): # migrate-scodoc7-dept-logos + """Post-migration: renomme les logos en fonction des id / dept de ScoDoc 9""" + tools.migrate_scodoc7_dept_logos(dept) + + @app.cli.command() @click.argument("formsemestre_id", type=click.INT) @click.argument("xlsfile", type=click.File("rb")) diff --git a/tools/__init__.py b/tools/__init__.py index 7ed85caed3..ac9e681c24 100644 --- a/tools/__init__.py +++ b/tools/__init__.py @@ -7,3 +7,4 @@ from tools.import_scodoc7_user_db import import_scodoc7_user_db from tools.import_scodoc7_dept import import_scodoc7_dept from tools.migrate_scodoc7_archives import migrate_scodoc7_dept_archives +from tools.migrate_scodoc7_logos import migrate_scodoc7_dept_logos diff --git a/tools/migrate_from_scodoc7.sh b/tools/migrate_from_scodoc7.sh index 59c0cee8a8..7d4132ca69 100755 --- a/tools/migrate_from_scodoc7.sh +++ b/tools/migrate_from_scodoc7.sh @@ -274,6 +274,9 @@ done # ----- Post-Migration: renomme archives en fonction des nouveaux ids su -c "(cd $SCODOC_DIR && source venv/bin/activate && flask migrate-scodoc7-dept-archives)" "$SCODOC_USER" || die "Erreur de la post-migration des archives" +# ----- Post-Migration: renomme logos en fonction des nouveaux ids +su -c "(cd $SCODOC_DIR && source venv/bin/activate && flask migrate-scodoc7-dept-logos)" || die "Erreur de la post-migration des logos" + # --- Si migration "en place", désactive ScoDoc 7 if [ "$INPLACE" == 1 ] diff --git a/tools/migrate_scodoc7_logos.py b/tools/migrate_scodoc7_logos.py new file mode 100644 index 0000000000..57c7775f77 --- /dev/null +++ b/tools/migrate_scodoc7_logos.py @@ -0,0 +1,53 @@ +# -*- mode: python -*- +# -*- coding: utf-8 -*- + +import glob +import os +import shutil + +from app.models import Departement + + +def migrate_scodoc7_dept_logos(dept_name=""): + if dept_name: + depts = Departement.query.filter_by(acronym=dept_name) + else: + depts = Departement.query + n_dir = 0 + n_moves = 0 + n_depts = 0 + purged_candidates = [] # directory that maybe purged at the end + for dept in depts: + logos_dir7 = f"/opt/scodoc-data/config/logos/logos_{dept.acronym}" + logos_dir9 = f"/opt/scodoc-data/config/logos/logos_{dept.id}" + if os.path.exists(logos_dir7): + print(f"Migrating {dept.acronym} logos...") + purged_candidates.append(logos_dir7) + n_depts += 1 + if not os.path.exists(logos_dir9): + # print(f"renaming {logos_dir7} to {logos_dir9}") + shutil.move(logos_dir7, logos_dir9) + n_dir += 1 + else: + # print(f"merging {logos_dir7} with {logos_dir9}") + for logo in glob.glob(f"{logos_dir7}/*"): + # print(f"\tmoving {logo}") + fn = os.path.split(logo)[1] + if not os.path.exists(os.path.sep.join([logos_dir9, fn])): + shutil.move(logo, logos_dir9) + n_moves += 1 + n_purged = 0 + for candidate in purged_candidates: + if len(os.listdir(candidate)) == 0: + os.rmdir(candidate) + n_purged += 1 + print(f"{n_depts} department(s) scanned") + if n_dir: + print(f"{n_dir} directory(ies) moved") + if n_moves: + print(f"{n_moves} file(s) moved") + if n_purged: + print(f"{n_purged} scodoc7 logo dir(s) removed") + if n_dir + n_moves + n_purged == 0: + print("nothing done") + # print(f"moved {n_moves}/{n} etuds")