43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
#
|
||
# 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 l’air, stable comme un menhir, et salé comme le beurre.
|
||
#
|
||
|
||
import os
|
||
import pwd
|
||
import sqlite3
|
||
from erminig.core.config import Config
|
||
|
||
|
||
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()
|
||
# 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)
|
||
|
||
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()
|