| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | from __future__ import annotations |
| 17 | |
| 18 | import argparse |
| 19 | import base64 |
| 20 | import json |
| 21 | import pathlib |
| 22 | import sys |
| 23 | import time |
| 24 | import urllib.error |
| 25 | import urllib.request |
| 26 | |
| 27 | |
| 28 | CHUNK_BYTES = 512 * 1024 |
| 29 | |
| 30 | |
| 31 | def split_statements(text: str) -> list[str]: |
| 32 | |
| 33 | out: list[str] = [] |
| 34 | buf: list[str] = [] |
| 35 | quote: str | None = None |
| 36 | i = 0 |
| 37 | n = len(text) |
| 38 | while i < n: |
| 39 | c = text[i] |
| 40 | if quote: |
| 41 | buf.append(c) |
| 42 | if c == quote: |
| 43 | quote = None |
| 44 | elif c in "'\"": |
| 45 | quote = c |
| 46 | buf.append(c) |
| 47 | elif c == "-" and text.startswith("--", i): |
| 48 | |
| 49 | j = text.find("\n", i) |
| 50 | i = n if j < 0 else j |
| 51 | continue |
| 52 | elif c == ";": |
| 53 | buf.append(c) |
| 54 | stmt = "".join(buf).strip() |
| 55 | if stmt and stmt != ";": |
| 56 | out.append(stmt) |
| 57 | buf = [] |
| 58 | else: |
| 59 | buf.append(c) |
| 60 | i += 1 |
| 61 | tail = "".join(buf).strip() |
| 62 | if tail: |
| 63 | out.append(tail) |
| 64 | return out |
| 65 | |
| 66 | |
| 67 | def chunks(statements: list[str], limit: int = CHUNK_BYTES): |
| 68 | |
| 69 | cur: list[str] = [] |
| 70 | size = 0 |
| 71 | for s in statements: |
| 72 | b = len(s.encode("utf-8")) + 1 |
| 73 | if cur and size + b > limit: |
| 74 | yield cur |
| 75 | cur, size = [], 0 |
| 76 | cur.append(s) |
| 77 | size += b |
| 78 | if cur: |
| 79 | yield cur |
| 80 | |
| 81 | |
| 82 | def post(endpoint: str, ns: str, db: str, user: str, pw: str, sql: str) -> list: |
| 83 | req = urllib.request.Request(endpoint.rstrip("/") + "/sql", data=sql.encode("utf-8")) |
| 84 | req.add_header("Accept", "application/json") |
| 85 | req.add_header("Content-Type", "text/plain; charset=utf-8") |
| 86 | req.add_header("surreal-ns", ns) |
| 87 | req.add_header("surreal-db", db) |
| 88 | token = base64.b64encode(f"{user}:{pw}".encode()).decode() |
| 89 | req.add_header("Authorization", "Basic " + token) |
| 90 | with urllib.request.urlopen(req, timeout=600) as r: |
| 91 | return json.loads(r.read().decode("utf-8")) |
| 92 | |
| 93 | |
| 94 | def load_file(path: pathlib.Path, args, db: str) -> tuple[int, int, list[str]]: |
| 95 | text = path.read_text(encoding="utf-8") |
| 96 | statements = split_statements(text) |
| 97 | ok = 0 |
| 98 | errs: list[str] = [] |
| 99 | for group in chunks(statements): |
| 100 | sql = "\n".join(group) |
| 101 | try: |
| 102 | results = post(args.endpoint, args.ns, db, args.user, args.password, sql) |
| 103 | except urllib.error.HTTPError as e: |
| 104 | errs.append(f"HTTP {e.code}: {e.read().decode('utf-8', 'replace')[:200]}") |
| 105 | continue |
| 106 | except Exception as e: |
| 107 | errs.append(f"{type(e).__name__}: {e}") |
| 108 | continue |
| 109 | for r in results: |
| 110 | if r.get("status") == "OK": |
| 111 | ok += 1 |
| 112 | else: |
| 113 | errs.append(str(r.get("result"))[:200]) |
| 114 | return len(statements), ok, errs |
| 115 | |
| 116 | |
| 117 | def main() -> int: |
| 118 | ap = argparse.ArgumentParser() |
| 119 | ap.add_argument("--endpoint", default="http://hzk-surreal:8000") |
| 120 | ap.add_argument("--ns", default="hezarfen") |
| 121 | ap.add_argument("--db", default="zeka", help="okul veritabanı adı") |
| 122 | ap.add_argument("--control-db", default="control") |
| 123 | ap.add_argument("--user", default="root") |
| 124 | ap.add_argument("--password", default="root") |
| 125 | ap.add_argument("--seed-dir", default="/seed") |
| 126 | ap.add_argument("--only", default=None, help="yalnız bu dosya adı") |
| 127 | args = ap.parse_args() |
| 128 | |
| 129 | seed = pathlib.Path(args.seed_dir) |
| 130 | files = sorted(p for p in seed.glob("*.surql") if not p.name.startswith("99_")) |
| 131 | if args.only: |
| 132 | files = [p for p in files if p.name == args.only] |
| 133 | |
| 134 | total_ok = total_st = 0 |
| 135 | failed: list[str] = [] |
| 136 | t_all = time.time() |
| 137 | |
| 138 | for p in files: |
| 139 | |
| 140 | db = args.control_db if p.name.startswith("00_") else args.db |
| 141 | t0 = time.time() |
| 142 | st, ok, errs = load_file(p, args, db) |
| 143 | dt = time.time() - t0 |
| 144 | total_ok += ok |
| 145 | total_st += st |
| 146 | flag = "TAMAM" if not errs else f"HATA({len(errs)})" |
| 147 | print(f"{p.name:<28} {st:>6} ifade {ok:>6} ok {dt:>6.1f}s {flag}", flush=True) |
| 148 | for e in errs[:3]: |
| 149 | print(f" ! {e}", flush=True) |
| 150 | if errs: |
| 151 | failed.append(p.name) |
| 152 | |
| 153 | print(f"\nTOPLAM: {total_st} ifade, {total_ok} basarili, " |
| 154 | f"{time.time() - t_all:.1f} saniye") |
| 155 | if failed: |
| 156 | print("HATALI DOSYALAR: " + ", ".join(failed)) |
| 157 | return 1 |
| 158 | print("TUM DOSYALAR HATASIZ YUKLENDI") |
| 159 | return 0 |
| 160 | |
| 161 | |
| 162 | if __name__ == "__main__": |
| 163 | sys.exit(main()) |