Erminig/erminig/models/db.py

44 lines
1.1 KiB
Python
Raw Permalink Normal View History

#
# Erminig - initialise la base sqlite
# Copyright (C) 2025 L0m1g
# Sous licence DOUARN - Voir le fichier LICENCE pour les détails
#
# Ce fichier fait partie du projet Erminig.
# Libre comme lair, stable comme un menhir, et salé comme le beurre.
#
2025-05-03 16:11:28 +02:00
import os
import pwd
2025-04-29 17:15:19 +02:00
import sqlite3
2025-05-03 18:33:53 +02:00
from erminig.core.config import Config
2025-04-29 17:15:19 +02:00
def init_db():
if Config.DB_PATH.exists():
return
conn = sqlite3.connect(Config.DB_PATH)
with open(Config.BASE_DIR / "schema.sql", "r") as f:
conn.executescript(f.read())
conn.commit()
conn.close()
2025-05-03 16:11:28 +02:00
# Attribution au user pak
pak_uid = pwd.getpwnam("pak").pw_uid
pak_gid = pwd.getpwnam("pak").pw_gid
os.chown(Config.DB_PATH, pak_uid, pak_gid)
os.chmod(Config.DB_PATH, 0o664)
2025-04-29 17:15:19 +02:00
print("Base erminig.db initialisée avec succès.")
class ErminigDB:
def __enter__(self):
self.conn = sqlite3.connect(Config.DB_PATH)
self.conn.row_factory = sqlite3.Row
self.cursor = self.conn.cursor()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.conn.commit()
self.conn.close()