135 lines
3.0 KiB
Python
135 lines
3.0 KiB
Python
import sys
|
|
import subprocess
|
|
from pathlib import Path
|
|
from datetime import date
|
|
|
|
# =========================================================
|
|
# CONFIG
|
|
# =========================================================
|
|
|
|
VBS_SCRIPT = Path(__file__).parent / "set_cell_silent.vbs"
|
|
|
|
# =========================================================
|
|
# PREFIXE VERSION
|
|
# =========================================================
|
|
|
|
def detecter_major(path: Path) -> int:
|
|
|
|
nom = path.name.lower()
|
|
|
|
if "restauration" in nom:
|
|
return 2
|
|
|
|
return 1
|
|
|
|
|
|
# =========================================================
|
|
# LECTURE VERSION
|
|
# =========================================================
|
|
|
|
def lire_version(path: Path, major: int):
|
|
|
|
if not path.exists():
|
|
return [major, 0, 0]
|
|
|
|
contenu = path.read_text(encoding="utf-8").strip().splitlines()
|
|
|
|
if not contenu:
|
|
return [major, 0, 0]
|
|
|
|
try:
|
|
version = [int(x) for x in contenu[0].split(".")]
|
|
|
|
while len(version) < 3:
|
|
version.append(0)
|
|
|
|
version = version[:3]
|
|
|
|
# force le major correct
|
|
version[0] = major
|
|
|
|
return version
|
|
|
|
except Exception:
|
|
return [major, 0, 0]
|
|
|
|
|
|
# =========================================================
|
|
# INCREMENT
|
|
# =========================================================
|
|
|
|
def increment_patch(version):
|
|
version[2] += 1
|
|
return version
|
|
|
|
|
|
# =========================================================
|
|
# ECRITURE TXT
|
|
# =========================================================
|
|
|
|
def ecrire_version_txt(path: Path, version):
|
|
|
|
version_str = ".".join(str(x) for x in version)
|
|
|
|
contenu = f"{version_str}\n{date.today().isoformat()}\n"
|
|
|
|
path.write_text(contenu, encoding="utf-8")
|
|
|
|
return version_str
|
|
|
|
|
|
# =========================================================
|
|
# ECRITURE EXCEL
|
|
# =========================================================
|
|
|
|
def ecrire_version_excel(classeur: Path, version_str: str):
|
|
|
|
if not classeur.exists():
|
|
print(f"Classeur introuvable : {classeur}")
|
|
return
|
|
|
|
valeur = f"Version : {version_str}"
|
|
|
|
cmd = [
|
|
"cscript",
|
|
"//nologo",
|
|
str(VBS_SCRIPT),
|
|
str(classeur),
|
|
"Tableau de bord",
|
|
"C1",
|
|
valeur
|
|
]
|
|
|
|
subprocess.run(cmd, check=True)
|
|
|
|
|
|
# =========================================================
|
|
# MAIN
|
|
# =========================================================
|
|
|
|
def main():
|
|
|
|
if len(sys.argv) < 3:
|
|
print("Usage :")
|
|
print("python maj_version.py VERSION.txt classeur.xlsm")
|
|
sys.exit(1)
|
|
|
|
version_file = Path(sys.argv[1])
|
|
classeur = Path(sys.argv[2])
|
|
|
|
major = detecter_major(version_file)
|
|
|
|
version = lire_version(version_file, major)
|
|
|
|
version = increment_patch(version)
|
|
|
|
version_str = ecrire_version_txt(version_file, version)
|
|
|
|
ecrire_version_excel(classeur, version_str)
|
|
|
|
print(f"Nouvelle version : {version_str}")
|
|
print(f"Classeur mis à jour : {classeur.name}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |