Ajout d'une gestion conditionnelle de la version applicative avec mise à jour ou lecture depuis VERSION.txt selon le mode (DEV/PROD).

This commit is contained in:
mrtoine 2025-12-15 13:37:56 +01:00
parent a5c1aa06a5
commit b3f201dd8d

View file

@ -158,15 +158,44 @@ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# On défini une fonction qui va s'occuper de récupérer la version actuelle de l'application
def get_git_version():
try:
import subprocess
hash_version = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).strip().decode('utf-8')
app_version = subprocess.check_output(
["git", "describe", "--tags", "--abbrev=0"],
stderr=subprocess.STDOUT
).strip().decode('utf-8')
return app_version + " (" + hash_version + ")"
except Exception:
return 'Dev / Pas de git'
import subprocess
"""
DEV : Calcule la version Git et met à jour le fichier VERSION.txt
PROD : Lit simplement le fichier VERSION.txt
"""
version_file = os.path.join(BASE_DIR, 'VERSION.txt')
# --- CAS 1 : MODE DÉVELOPPEMENT (Mise à jour du fichier) ---
if DEBUG:
try:
# 1. On récupère les infos Git
hash_part = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'],
stderr=subprocess.DEVNULL).strip().decode('utf-8')
try:
tag_part = subprocess.check_output(["git", "describe", "--tags", "--abbrev=0"],
stderr=subprocess.DEVNULL).strip().decode('utf-8')
except:
tag_part = "v0.0.x" # Fallback si pas de tag
full_version = f"{tag_part} ({hash_part})"
# 2. On ÉCRIT/MET À JOUR le fichier texte
with open(version_file, 'w') as f:
f.write(full_version)
return full_version
except Exception as e:
# Si Git échoue en local (rare, mais possible)
return f"Dev Mode (Err: {e})"
# --- CAS 2 : MODE PRODUCTION (Lecture seule) ---
else:
if os.path.exists(version_file):
with open(version_file, 'r') as f:
return f.read().strip()
else:
return "Version inconnue (Fichier manquant)"
GIT_VERSION = get_git_version()