Repository Public
1#!/usr/bin/env python32# =============================================================================3# SP BOUCHER | SOFTWARE & RESEARCH4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# PROJECT METADATA8# -----------------------------------------------------------------------------9# Project : SPSearch10# File : deploy/scripts/sq.py11# Description : Petit remplaçant de la commande sqlite3 pour backup.sh /12# restore.sh (elle n'est pas installée partout).13# Module : deploy14# Repository : https://git.spboucher.ai/spweb15# Project Website : https://search.spboucher.ai16# Created : 2026-11-3017# Last Modified : 2026-11-3018#19# =============================================================================20# AUTHOR & PROFESSIONAL AFFILIATION21# =============================================================================22#23# Author : Simon-Pierre Boucher24# Position : Professor of Finance25# Institution : Université du Québec en Outaouais (UQO)26# Department : Département des sciences administratives27# Organization : SP Boucher28#29# Website : https://spboucher.ai30# Git : https://git.spboucher.ai31#32# =============================================================================33# OFFICIAL CONTACT DIRECTORY34# =============================================================================35#36# General : info@spboucher.ai37# Professional : contact@spboucher.ai38# Direct : simon@spboucher.ai39# Research : research@spboucher.ai40# Development : dev@spboucher.ai41# Support : support@spboucher.ai42# Security/Admin : admin@spboucher.ai43# Notifications : notifications@spboucher.ai44#45# Preferred Contact: contact@spboucher.ai46#47# =============================================================================48# COPYRIGHT & INTELLECTUAL PROPERTY49# =============================================================================50#51# Copyright (c) 202652# Simon-Pierre Boucher53#54# All rights reserved where applicable.55#56# This file is governed by the applicable project license,57# copyright notices, institutional agreements and relevant law.58#59# Unless expressly authorized by the applicable license or60# rights holder, copying, redistribution, sublicensing,61# publication and commercial exploitation are not permitted.62#63# Existing third-party rights and notices remain unaffected.64#65# =============================================================================66# CONFIDENTIALITY & SECURITY67# =============================================================================68#69# Classification : Public70# Distribution : Public repository; use is governed by the project license71# Security Contact : admin@spboucher.ai72#73# Access to this source does not independently grant permission74# to redistribute or disclose confidential materials.75#76# Do not embed credentials, private keys, access tokens,77# certificates or confidential personal information.78#79# =============================================================================80# LICENSE & MAINTENANCE81# =============================================================================82#83# License : None specified (no license file in this repository)84# Maintainer : Simon-Pierre Boucher85# Technical Contact: dev@spboucher.ai86#87# =============================================================================88# END OF SP BOUCHER SOURCE HEADER89# =============================================================================9091"""Petit remplaçant de la commande sqlite3 pour backup.sh / restore.sh (elle n'est pas installée partout).92 sq.py dump <base> <table>... : instructions SQL (CREATE + INSERT) des tables demandées, lecture seule93 sq.py exec <base> : exécute le SQL lu sur l'entrée standard94 sq.py query <base> <sql> : affiche la première colonne de la première ligne95Sort avec un code non nul à la moindre erreur : une sauvegarde vide ne doit jamais passer pour réussie."""96import sqlite3, sys9798def main():99 cmd, db = sys.argv[1], sys.argv[2]100 if cmd == "dump":101 con = sqlite3.connect(f"file:{db}?mode=ro", uri=True, timeout=10)102 want = set(sys.argv[3:])103 have = {r[0] for r in con.execute("SELECT name FROM sqlite_master WHERE type='table'")}104 missing = want - have105 if missing:106 sys.exit(f"tables absentes de {db} : {', '.join(sorted(missing))}")107 for line in con.iterdump():108 head = line.split("(")[0].replace('"', "")109 if any(head.endswith(" " + t) or head.endswith(" " + t + " ") or f" {t} " in head + " " for t in want) and line.startswith(("CREATE TABLE", "INSERT INTO")):110 print(line)111 elif cmd == "exec":112 con = sqlite3.connect(db, timeout=10)113 con.executescript(sys.stdin.read())114 con.commit()115 elif cmd == "query":116 con = sqlite3.connect(f"file:{db}?mode=ro", uri=True, timeout=10)117 row = con.execute(sys.argv[3]).fetchone()118 print(row[0] if row else "")119 else:120 sys.exit("commande inconnue")121122if __name__ == "__main__":123 main()124