Repository Public
1#!/usr/bin/env python32# =============================================================================3# SP BOUCHER | SOFTWARE & RESEARCH4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# PROJECT METADATA8# -----------------------------------------------------------------------------9# Project : Lou·Ka10# File : run.py11# Description : python run.py12# {sync|watch|sync-ct|watch-ct|serve|record|geocode|quality|imgaudit|fairvalue|poi|quartier|env|kascores|buildings|managers|tal|rdl|...}13# Repository : https://git.spboucher.ai/lou-ka14# Created : 2026-08-0615# Last Modified : 2026-09-0416#17# =============================================================================18# AUTHOR & PROFESSIONAL AFFILIATION19# =============================================================================20#21# Author : Simon-Pierre Boucher22# Position : Professor of Finance23# Institution : Université du Québec en Outaouais (UQO)24# Department : Département des sciences administratives25# Organization : SP Boucher26#27# Website : https://spboucher.ai28# Git : https://git.spboucher.ai29#30# =============================================================================31# OFFICIAL CONTACT DIRECTORY32# =============================================================================33#34# General : info@spboucher.ai35# Professional : contact@spboucher.ai36# Direct : simon@spboucher.ai37# Research : research@spboucher.ai38# Development : dev@spboucher.ai39# Support : support@spboucher.ai40# Security/Admin : admin@spboucher.ai41# Notifications : notifications@spboucher.ai42#43# Preferred Contact: contact@spboucher.ai44#45# =============================================================================46# COPYRIGHT & INTELLECTUAL PROPERTY47# =============================================================================48#49# Copyright (c) 202650# Simon-Pierre Boucher51#52# All rights reserved where applicable.53#54# This file is governed by the applicable project license,55# copyright notices, institutional agreements and relevant law.56#57# Unless expressly authorized by the applicable license or58# rights holder, copying, redistribution, sublicensing,59# publication and commercial exploitation are not permitted.60#61# Existing third-party rights and notices remain unaffected.62#63# =============================================================================64# CONFIDENTIALITY & SECURITY65# =============================================================================66#67# Classification : Public68# Distribution : Public repository; use is governed by the project license69# Security Contact : admin@spboucher.ai70#71# Access to this source does not independently grant permission72# to redistribute or disclose confidential materials.73#74# Do not embed credentials, private keys, access tokens,75# certificates or confidential personal information.76#77# =============================================================================78# LICENSE & MAINTENANCE79# =============================================================================80#81# License : None specified (no license file in this repository)82# Maintainer : Simon-Pierre Boucher83# Technical Contact: dev@spboucher.ai84#85# =============================================================================86# END OF SP BOUCHER SOURCE HEADER87# =============================================================================8889# Lou-Ka — run.py : point d'entrée — sync/watch/serve (+ outils)90"""python run.py {sync|watch|sync-ct|watch-ct|serve|record|geocode|quality|imgaudit|fairvalue|poi|quartier|env|kascores|buildings|managers|tal|rdl|...}"""91from __future__ import annotations9293import os94import sys95from pathlib import Path9697_env = Path(__file__).parent / ".env"98if _env.exists():99 for line in _env.read_text().splitlines():100 line = line.strip()101 if line and not line.startswith("#") and "=" in line:102 k, _, v = line.partition("=")103 os.environ.setdefault(k.strip(), v.strip())104105106def _disk_guard() -> None:107 # imgcache (miniatures /api/img, régénérables) : aucune éviction → peut108 # remplir le disque (ENOSPC → 500). Purge si critique, sinon TTL 30 j.109 import shutil110 import threading111 import time112113 root = Path(__file__).parent114 cache = root / "data" / "imgcache"115116 def _run() -> None:117 try:118 free = shutil.disk_usage(root).free / 1e9119 print(f"[lou-ka] disk_guard: {free:.2f} Go libres", flush=True)120 if not cache.is_dir():121 return122 crit = free < 5123 cutoff = time.time() - 30 * 86400124 n = 0125 with os.scandir(cache) as it:126 for e in it:127 try:128 if crit or e.stat().st_mtime < cutoff:129 os.unlink(e.path)130 n += 1131 except OSError:132 pass133 if n:134 free = shutil.disk_usage(root).free / 1e9135 print(f"[lou-ka] disk_guard: {n} fichiers purgés "136 f"({'critique' if crit else 'TTL'}), "137 f"{free:.2f} Go libres", flush=True)138 except Exception as exc:139 print(f"[lou-ka] disk_guard: erreur ignorée : {exc}", flush=True)140141 threading.Thread(target=_run, name="disk-guard", daemon=True).start()142143144def main() -> None:145 cmd = sys.argv[1] if len(sys.argv) > 1 else "serve"146 if cmd in ("serve", "watch", "watch-ct", "sync", "sync-ct"):147 _disk_guard()148 if cmd == "sync":149 from louka import ingest150 ingest.run(sys.argv[2:] or None)151 elif cmd == "watch":152 from louka import ingest153 minutes = int(sys.argv[2]) if len(sys.argv) > 2 else 60154 ingest.watch(minutes * 60)155 elif cmd == "geocode1":156 from louka import geocode157 limit = int(sys.argv[2]) if len(sys.argv) > 2 else None158 geocode.run(limit)159 elif cmd == "geocode":160 from louka import geocode161 limit = int(sys.argv[2]) if len(sys.argv) > 2 else None162 geocode.run_batch(limit)163 elif cmd == "quality":164 from louka import quality165 limit = int(sys.argv[2]) if len(sys.argv) > 2 else None166 quality.backfill(limit)167 elif cmd == "imgaudit":168 from louka import imgcheck169 limit = int(sys.argv[2]) if len(sys.argv) > 2 else 1500170 src = sys.argv[3] if len(sys.argv) > 3 else None171 imgcheck.run(limit, source=src)172 elif cmd == "fairvalue":173 from louka import fairvalue174 fairvalue.compute_all()175 elif cmd == "poi":176 from louka import poi177 limit = int(sys.argv[2]) if len(sys.argv) > 2 else None178 poi.run(limit)179 elif cmd == "quartier":180 from louka import quartier181 limit = int(sys.argv[2]) if len(sys.argv) > 2 else None182 quartier.enrich(limit)183 elif cmd == "env":184 from louka import environment185 limit = int(sys.argv[2]) if len(sys.argv) > 2 else None186 print(environment.run(limit))187 elif cmd == "kascores":188 from louka import kascores189 force = len(sys.argv) > 2 and sys.argv[2] == "all"190 print(kascores.run(recompute_all=force))191 elif cmd == "inondation-build":192 from louka import inondation193 inondation.build()194 elif cmd == "air-refresh":195 from louka import air196 air.refresh()197 elif cmd == "gaz-refresh":198 from louka import gaz199 print(f"[gaz] {gaz.refresh()} stations")200 elif cmd == "hydro-precompute":201 from louka import hydro202 n = int(sys.argv[2]) if len(sys.argv) > 2 else 100203 b = int(sys.argv[3]) if len(sys.argv) > 3 else 40204 hydro.precompute(limit=n, budget=b)205 elif cmd == "rdl":206 from louka import rdl207 rdl.refresh()208 elif cmd == "tal":209 from louka import tal210 n = int(sys.argv[2]) if len(sys.argv) > 2 else 60211 b = int(sys.argv[3]) if len(sys.argv) > 3 else 600212 tal.precompute(limit=n, budget=b)213 elif cmd == "buildings":214 from louka import building215 print(building.rollup())216 elif cmd == "managers":217 from louka import managers218 b = float(sys.argv[2]) if len(sys.argv) > 2 else 300219 print(managers.precompute(budget_s=b))220 elif cmd == "record":221 from louka import fixtures222 from louka.connectors import CONNECTORS223 targets = sys.argv[2:] or sorted(CONNECTORS)224 for sid in targets:225 try:226 print(f"[lou-ka] record {sid} ... {fixtures.record(sid)}")227 except Exception as exc:228 print(f"[lou-ka] record {sid} ÉCHEC : {exc}", file=sys.stderr)229 elif cmd == "serve":230 import uvicorn231 port = int(sys.argv[2]) if len(sys.argv) > 2 else 8080232 uvicorn.run("louka.web:app", host="0.0.0.0", port=port)233 else:234 print(__doc__)235 sys.exit(1)236237238if __name__ == "__main__":239 main()240# ---------------------------------------------------------------------------241# [ka2 2026-08-31] padding intentionnel : sur disque APFS saturé (ENOSPC),242# les réécritures ne tronquaient pas le fichier — une queue corrompue issue243# d'une écriture précédente (pannes du 2026-08-31) subsistait après l'ancien244# EOF et cassait le parse (SyntaxError). Ces lignes de commentaire allongent245# le fichier au-delà de l'ancienne taille pour garantir l'écrasement complet.246# Elles sont inertes et peuvent être supprimées lors d'un prochain commit247# une fois le disque assaini.248# ---------------------------------------------------------------------------249###############################################################################250###############################################################################251###############################################################################252###############################################################################253###############################################################################254###############################################################################255###############################################################################256###############################################################################257###############################################################################258###############################################################################259###############################################################################260###############################################################################261###############################################################################262###############################################################################263###############################################################################264# fin du padding ka2265