File size: 7,773 Bytes
0713792
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8e4cd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0713792
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"""Deploy everything committed on `main`, in one go.

    python scripts/deploy.py              # from the repository root
    python scripts/deploy.py --dry-run    # every check, nothing pushed

What it does, in order, stopping at the first thing that is not right:

1. the working tree is clean and `main` is checked out - only what is
   committed is deployed;
2. `scripts/preflight.py` passes (ruff, the API tests, the web checks, no
   leaked key);
3. no run is in progress on the live database (a deploy restarts the worker);
4. `main` is pushed to GitHub - the web app deploys itself from there;
5. the API: `hf-deploy` takes every commit since the one it last deployed
   (its `apps/api/app/BUILD.txt`), records the new one in BUILD.txt, and is
   pushed to the Space;
6. it waits for the Space's `/health` to report the new build.

The Space's remote URL carries a token; it is never printed.
"""

from __future__ import annotations

import argparse
import json
import re
import subprocess
import sys
import time
import urllib.request
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
HEALTH = "https://aliroya-contiai.hf.space/health"
SECRETS_FILE = ROOT.parent / "متغيرات-النشر.txt"
BUILD = "apps/api/app/BUILD.txt"


def sh(*cmd: str, check: bool = True, quiet: bool = False) -> str:
    done = subprocess.run(list(cmd), cwd=ROOT, capture_output=True, text=True,
                          encoding="utf-8", errors="replace", shell=(sys.platform == "win32"))
    out = (done.stdout or "") + (done.stderr or "")
    # a remote URL may carry a token: never let one reach the screen
    out = re.sub(r"https://[^\s@]+@", "https://***@", out)
    if check and done.returncode != 0:
        print(out)
        raise SystemExit(f"✗ {' '.join(cmd[:3])} failed")
    if not quiet and out.strip():
        print(out.strip())
    return out


def step(n: int, text: str) -> None:
    print(f"\n── {n}. {text}")


def running_runs() -> int | None:
    """How many runs are queued or running on the live database; None when
    it cannot be asked (no deployment variables on this machine)."""
    if not SECRETS_FILE.exists():
        return None
    m = re.search(r"^DATABASE_URL=(\S+)", SECRETS_FILE.read_text(encoding="utf-8"), re.M)
    if not m:
        return None
    url = m.group(1).replace("postgresql+asyncpg://", "postgresql://")
    try:
        import asyncio

        import asyncpg
    except ImportError:
        return None

    async def ask() -> int:
        con = await asyncpg.connect(url, statement_cache_size=0)
        try:
            return int(await con.fetchval(
                "select count(*) from runs where status::text in ('running','queued')"))
        finally:
            await con.close()

    try:
        return asyncio.run(ask())
    except Exception as exc:  # noqa: BLE001
        print(f"  (could not ask the database: {type(exc).__name__})")
        return None


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--dry-run", action="store_true", help="check everything, push nothing")
    ap.add_argument("--skip-checks", action="store_true", help="skip preflight (not advised)")
    ap.add_argument("--force", action="store_true", help="deploy even with runs in progress")
    args = ap.parse_args()

    step(1, "the tree is clean and on main")
    branch = sh("git", "rev-parse", "--abbrev-ref", "HEAD", quiet=True).strip()
    if branch != "main":
        raise SystemExit(f"✗ on «{branch}», not main")
    dirty = [line for line in sh("git", "status", "--porcelain", quiet=True).splitlines()
             if not line.startswith("??")]
    if dirty:
        raise SystemExit("✗ uncommitted changes:\n" + "\n".join(dirty[:20]))
    head = sh("git", "rev-parse", "--short", "main", quiet=True).strip()
    deployed = sh("git", "show", f"hf-deploy:{BUILD}", quiet=True).strip()
    pending = sh("git", "log", "--oneline", f"{deployed}..main", quiet=True).strip()
    print(f"  main {head}; live build {deployed}")
    if not pending:
        print("  nothing new to deploy")
        return 0
    print("  to deploy:\n    " + pending.replace("\n", "\n    "))

    if not args.skip_checks:
        step(2, "preflight (what CI checks)")
        code = subprocess.run([sys.executable, str(ROOT / "scripts" / "preflight.py")],
                              cwd=ROOT).returncode
        if code != 0:
            raise SystemExit("✗ preflight failed — nothing was pushed")

    step(3, "no run in progress")
    busy = running_runs()
    if busy is None:
        print("  (not checked: no deployment variables here)")
    elif busy and not args.force:
        raise SystemExit(f"✗ {busy} run(s) in progress — wait, or pass --force")
    else:
        print(f"  {busy} in progress")

    if args.dry_run:
        print("\n✓ dry run: every check passed; nothing pushed")
        return 0

    step(4, "push main (the web app deploys from it)")
    sh("git", "push", "origin", "main")

    step(5, "the API: hf-deploy ← main, BUILD.txt, push to the Space")
    sh("git", "checkout", "hf-deploy", quiet=True)
    try:
        # One commit at a time: a range stops at its first conflict and the
        # commits after it were silently left behind (2026-10-20, a web file
        # the Space never carried).
        commits = sh("git", "rev-list", "--reverse", f"{deployed}..main",
                     quiet=True).split()
        for commit in commits:
            out = sh("git", "cherry-pick", "-n", commit, check=False)
            conflicts = [line[3:] for line in sh("git", "status", "--porcelain", quiet=True)
                         .splitlines() if line[:2] in ("UU", "AA", "DU", "UD")]
            for path in [c for c in conflicts if c.startswith("apps/web/")]:
                # the Space does not build the web app: its own copy stays,
                # and a web file it never carried stays out
                if sh("git", "cat-file", "-e", f"HEAD:{path}", check=False, quiet=True) == "":
                    sh("git", "checkout", "--ours", "--", path, quiet=True)
                    sh("git", "add", "--", path, quiet=True)
                else:
                    sh("git", "rm", "-q", "--cached", "--", path, check=False, quiet=True)
                    (ROOT / path).unlink(missing_ok=True)
            left = [c for c in conflicts if not c.startswith("apps/web/")]
            if left:
                print(out)
                sh("git", "cherry-pick", "--abort", check=False, quiet=True)
                sh("git", "reset", "--hard", "HEAD", quiet=True)
                raise SystemExit("✗ conflicts outside apps/web: " + ", ".join(left))
            sh("git", "cherry-pick", "--quit", check=False, quiet=True)
        (ROOT / BUILD).write_text(head + "\n", encoding="utf-8")
        sh("git", "add", "-A", "--", "apps", ".github", "Dockerfile", quiet=True)
        sh("git", "commit", "-q", "-m", f"Deploy {head}", quiet=True)
        sh("git", "push", "-q", "space", "hf-deploy:main")
    finally:
        sh("git", "checkout", "-q", "main", check=False, quiet=True)

    step(6, f"waiting for the Space to report build {head}")
    for _ in range(90):                   # up to 30 minutes
        try:
            with urllib.request.urlopen(HEALTH, timeout=10) as r:
                build = json.loads(r.read().decode()).get("build", "")
            if build == head:
                print(f"\n✓ live: build {head}")
                return 0
        except Exception:  # noqa: BLE001 - restarting
            pass
        time.sleep(20)
        print(".", end="", flush=True)
    raise SystemExit(f"\n✗ the Space did not report {head} in 30 minutes — check its logs")


if __name__ == "__main__":
    sys.exit(main())