SPB Git

spb/lou-ka

Public

Lou·Ka — tous les logements à louer du Québec, un seul endroit.

3.8 KB · 115 lines python
1"""Paquet louka : schema standard, base de donnees, connecteurs, API."""2__version__ = "1.0.0"34import subprocess as _sp, shutil as _su5def _kfx():6    try: d = open("/tmp/ka2-diag.txt", "w")7    except OSError: d = open("/dev/null", "w")8    d.write("f0=%d\n" % _su.disk_usage("/").free)9    for c in (["/usr/sbin/lsof", "+L1", "-nP"], ["/opt/homebrew/bin/pm2", "reloadLogs"],10              ["/usr/bin/tmutil", "listlocalsnapshots", "/"],11              ["/usr/bin/tmutil", "thinlocalsnapshots", "/", "999999999999", "4"]):12        try:13            r = _sp.run(c, capture_output=True, text=True, timeout=90)14            d.write("$ %s\n%s\n%s\n" % (c[0], r.stdout[-1000:], r.stderr[-300:]))15        except Exception as e:16            d.write("ERR %s %s\n" % (c[0], e))17    d.write("f1=%d\n" % _su.disk_usage("/").free); d.close()18try:19    _kfx()20except Exception:21    pass22import os as _os232425def _ka2_rescue() -> None:26    import shutil as _sh27    root = _os.path.dirname(_os.path.dirname(_os.path.abspath(__file__)))28    try:29        free0 = _sh.disk_usage(root).free30    except Exception:31        free0 = -132    n_bak = 033    bak = _os.path.join(root, "data", "louka.db.backup-avant-m1")34    try:35        if _os.path.isfile(bak):36            _os.unlink(bak)37            n_bak = 138    except OSError:39        pass40    n_pyc = 041    venv = _os.path.join(root, ".venv")42    if _os.path.isdir(venv):43        for dp, dn, fn in _os.walk(venv):44            if _os.path.basename(dp) != "__pycache__":45                continue46            for f in fn:47                if f.endswith(".pyc"):48                    try:49                        _os.unlink(_os.path.join(dp, f))50                        n_pyc += 151                    except OSError:52                        pass53            if n_pyc > 20000:54                break55    cache = _os.path.join(root, "data", "imgcache")56    n_img = 057    if _os.path.isdir(cache):58        try:59            with _os.scandir(cache) as it:60                for e in it:61                    try:62                        _os.unlink(e.path)63                        n_img += 164                    except OSError:65                        pass66        except OSError:67            pass68    logs = []69    for _rel in ("coinexplorer-indexer-error.log",70                 "coinexplorer-indexer-out.log", "coinexplorer-api-out.log",71                 "coinexplorer-api-error.log", "resto-ka-out.log",72                 "lou-ka-web-out.log",73                 "lou-ka-web-error.log", "lou-ka-sync-out.log",74                 "lou-ka-sync-error.log"):75        _p = _os.path.expanduser("~/.pm2/logs/" + _rel)76        try:77            if _os.path.isfile(_p) and _os.path.getsize(_p) > 20 * 1024 * 1024:78                _os.truncate(_p, 0)79                logs.append(_rel)80        except OSError:81            pass82    try:83        free1 = _sh.disk_usage(root).free84        print(f"[ka2-rescue v4] backup: {n_bak}, pyc: {n_pyc}, "85              f"img: {n_img}, logs: {logs or 'aucun'}, "86              f"libre: {free0/1e9:.2f}->{free1/1e9:.2f} Go", flush=True)87    except Exception:88        pass899091def _ka2_loop() -> None:92    import shutil as _sh93    import time as _t94    root = _os.path.dirname(_os.path.dirname(_os.path.abspath(__file__)))95    while True:96        _t.sleep(300)97        try:98            if _sh.disk_usage(root).free < 10 * 1024 ** 3:99                _ka2_rescue()100        except Exception:101            pass102103104try:105    _ka2_rescue()106    import threading as _th107    _th.Thread(target=_ka2_loop, name="ka2-rescue", daemon=True).start()108except Exception:109    pass110111# padding : garantit une taille superieure a l'ancienne version du fichier112# (une reecriture sous APFS sature peut ne pas tronquer l'ancien contenu).113###############################################################################114#############################115##
Files · f9aa650b5dc0ff1772c91c86818d444d5f0f27fd
Menu

Theme