-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrewrite-owners.py
More file actions
291 lines (245 loc) · 11.6 KB
/
Copy pathrewrite-owners.py
File metadata and controls
291 lines (245 loc) · 11.6 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python3
"""rewrite-owners — redirect every config that hardcodes an OLD GitHub owner to `organvm`.
The GitHub consolidation (`scripts/consolidate-github.py`) collapses the fragmented
multi-owner estate into ONE org `organvm`. GitHub *URL* redirects survive a transfer, but
the LIVE FLEET keys on literal `owner/repo` strings in three places that DO NOT auto-redirect
and will silently break dispatch the moment a repo moves:
1. tasks.yaml — every task's `repo:` owner (a-organvm / 4444J99 / organvm-i-theoria / …).
The isolated-local-run + PR logic resolves a checkout by this exact string.
2. .github/workflows/deploy-api.yml — the hardcoded `LIMEN_GITHUB_REPO=4444J99/limen`
env literal pushed into Cloud Run (the dossier's "CI coupling (real)" row).
3. local git checkouts under ~/Workspace — their `origin` remote still points at the old
owner; after a transfer the redirect works but should be made explicit so
`git fetch origin <base>` and PR creation stay unambiguous.
SAFE BY DEFAULT. Dry-run prints exactly what it WOULD change and changes NOTHING. Only
`--apply` (GATED) writes — and even then it writes tasks.yaml via the SAME atomic
`limen.io.save_limen_file` (temp-file + os.replace) every other writer uses, so a crash or a
concurrent reader can never observe a truncated file. The `git remote set-url` commands are
ALWAYS only *emitted* (printed / written to a .sh) — this script never runs git or touches a
checkout. Reversible: re-run pointing the mapping back, or `git checkout -- tasks.yaml
.github/workflows/deploy-api.yml` before any commit.
python3 scripts/rewrite-owners.py # DRY-RUN plan (read-only)
python3 scripts/rewrite-owners.py --apply # ⚠ GATED: write tasks.yaml + deploy-api.yml
python3 scripts/rewrite-owners.py --emit-remotes out.sh # write the git-remote commands to a file
"""
from __future__ import annotations
import os
import re
import subprocess
import sys
from pathlib import Path
# tasks.yaml is written through the project's ONE atomic writer — never a hand-rolled open().
LIMEN_ROOT = Path(os.environ.get("LIMEN_ROOT", Path(__file__).resolve().parent.parent))
sys.path.insert(0, str(LIMEN_ROOT / "cli" / "src"))
from limen.io import load_limen_file, save_limen_file # noqa: E402
TARGET = "organvm"
# The OLD source owners — kept identical to scripts/consolidate-github.py:OWNERS so the two
# stay in lockstep (one source of truth for "which owners are collapsing"). TARGET is excluded.
# Anything NOT in this set (e.g. external upstreams like `langchain-ai/langgraph`) is LEFT
# UNTOUCHED — we only own and only transfer these owners.
OLD_OWNERS = {
"4444J99",
"a-organvm",
"meta-organvm",
"organvm-i-theoria",
"organvm-ii-poiesis",
"organvm-iii-ergon",
"organvm-iv-taxis",
"organvm-v-logos",
"organvm-vi-koinonia",
"organvm-vii-kerygma",
}
# Bare repo names (no owner prefix) that are ours and must canonicalize to organvm/<name>.
# `repo: limen` is the conductor itself; with no owner the dispatcher can't resolve a checkout,
# so collapse it onto the same canonical target every owner/limen ref lands on.
BARE_OURS = {"limen"}
TASKS_PATH = Path(os.environ.get("LIMEN_TASKS", LIMEN_ROOT / "tasks.yaml"))
DEPLOY_YML = LIMEN_ROOT / ".github" / "workflows" / "deploy-api.yml"
WORKSPACE = Path(os.environ.get("LIMEN_WORKDIR", Path.home() / "Workspace"))
def map_repo(repo: str | None) -> str | None:
"""Return the rewritten `owner/name` if `repo` points at an OLD owner (or is a bare
name we own), else None (= leave untouched). Pure: no I/O, easy to unit-test/reverse."""
if not repo:
return None
repo = repo.strip()
if not repo:
return None
if "/" in repo:
owner, name = repo.split("/", 1)
if owner in OLD_OWNERS and name:
new = f"{TARGET}/{name}"
return new if new != repo else None
return None
# bare name, no owner
if repo in BARE_OURS:
return f"{TARGET}/{repo}"
return None
# ---------------------------------------------------------------------------- tasks.yaml ----
def plan_tasks() -> tuple[int, list[tuple[str, str, str]], object]:
"""Load tasks.yaml via the pydantic model and compute every repo rewrite.
Returns (count, changes[(task_id, old, new)], limen_file_with_rewrites_applied_in_memory)."""
lf = load_limen_file(TASKS_PATH)
changes: list[tuple[str, str, str]] = []
for t in lf.tasks:
new = map_repo(t.repo)
if new is not None:
changes.append((t.id, t.repo, new))
t.repo = new # mutate the in-memory model; only persisted on --apply
return len(changes), changes, lf
def apply_tasks(lf) -> None:
"""Persist via the atomic writer (temp file + fsync + os.replace). NEVER a raw write."""
save_limen_file(TASKS_PATH, lf)
# ----------------------------------------------------------------- deploy-api.yml literal ----
DEPLOY_RE = re.compile(r"(LIMEN_GITHUB_REPO=)([\w.-]+)/limen\b")
def plan_deploy() -> tuple[bool, str | None, str | None]:
"""Detect the hardcoded LIMEN_GITHUB_REPO=<old-owner>/limen. Returns (needs_fix, old, new)."""
if not DEPLOY_YML.exists():
return False, None, None
text = DEPLOY_YML.read_text()
m = DEPLOY_RE.search(text)
if not m:
return False, None, None
owner = m.group(2)
if owner == TARGET:
return False, None, None # already organvm
old = f"{m.group(1)}{owner}/limen"
new = f"{m.group(1)}{TARGET}/limen"
return True, old, new
def apply_deploy() -> bool:
text = DEPLOY_YML.read_text()
new_text, n = DEPLOY_RE.subn(rf"\g<1>{TARGET}/limen", text)
if n and new_text != text:
# plain literal substitution in a YAML workflow; rewrite the whole file atomically-ish
DEPLOY_YML.write_text(new_text)
return True
return False
# --------------------------------------------------- emit (NOT run) git remote set-url ----
def _git_remote_url(checkout: Path) -> str | None:
try:
r = subprocess.run(
["git", "-C", str(checkout), "remote", "get-url", "origin"],
capture_output=True, text=True, timeout=15,
)
return r.stdout.strip() if r.returncode == 0 else None
except Exception:
return None
def _owner_from_url(url: str) -> tuple[str, str] | None:
"""Parse owner/name from an https or ssh github remote URL."""
u = url.strip()
u = re.sub(r"\.git$", "", u)
m = re.search(r"github\.com[:/]+([\w.-]+)/([\w.-]+)$", u)
if not m:
return None
return m.group(1), m.group(2)
def plan_remotes() -> list[tuple[Path, str, str]]:
"""Scan git checkouts under ~/Workspace whose origin points at an OLD owner.
Returns (checkout_path, old_url, new_url). NEVER runs set-url — caller only emits."""
out: list[tuple[Path, str, str]] = []
if not WORKSPACE.exists():
return out
seen: set[Path] = set()
# find every .git (repo or worktree pointer); cap depth to stay fast
for gitdir in WORKSPACE.rglob(".git"):
checkout = gitdir.parent
if checkout in seen:
continue
seen.add(checkout)
url = _git_remote_url(checkout)
if not url:
continue
parsed = _owner_from_url(url)
if not parsed:
continue
owner, name = parsed
if owner in OLD_OWNERS:
new_url = re.sub(
rf"([:/]){re.escape(owner)}/{re.escape(name)}",
rf"\g<1>{TARGET}/{name}",
url,
count=1,
)
if new_url != url:
out.append((checkout, url, new_url))
return out
def emit_remote_commands(remotes: list[tuple[Path, str, str]]) -> str:
lines = [
"#!/usr/bin/env bash",
"# EMITTED by scripts/rewrite-owners.py — review, then run by hand AFTER the transfer.",
"# This script does NOT run these; updating a remote before the repo actually moves",
"# would point your checkout at a not-yet-existing path. Run post-consolidation.",
"set -euo pipefail",
"",
]
for checkout, old_url, new_url in remotes:
lines.append(f"# {old_url} -> {new_url}")
lines.append(f"git -C {shquote(str(checkout))} remote set-url origin {shquote(new_url)}")
lines.append("")
return "\n".join(lines)
def shquote(s: str) -> str:
if re.fullmatch(r"[\w@%+=:,./-]+", s):
return s
return "'" + s.replace("'", "'\\''") + "'"
# ----------------------------------------------------------------------------------- main ----
def main() -> int:
apply = "--apply" in sys.argv
emit_path = None
if "--emit-remotes" in sys.argv:
i = sys.argv.index("--emit-remotes")
if i + 1 < len(sys.argv):
emit_path = Path(sys.argv[i + 1])
print(f"=== rewrite-owners → {TARGET} ===")
print(f" tasks.yaml : {TASKS_PATH}")
print(f" deploy yml : {DEPLOY_YML}")
print(f" workspace : {WORKSPACE}")
print(f" old owners : {', '.join(sorted(OLD_OWNERS))}")
print(f" bare-ours : {', '.join(sorted(BARE_OURS))} (external upstreams left untouched)\n")
# 1) tasks.yaml
n_tasks, changes, lf = plan_tasks()
print(f"[1] tasks.yaml repo: refs to rewrite = {n_tasks}")
from collections import Counter
by_owner = Counter(old.split("/")[0] if "/" in old else f"<bare:{old}>" for _, old, _ in changes)
for owner, c in by_owner.most_common():
print(f" {c:5} {owner} → {TARGET}")
print(" sample (first 8):")
for tid, old, new in changes[:8]:
print(f" {tid}: {old} → {new}")
if len(changes) > 8:
print(f" … +{len(changes)-8} more")
# 2) deploy-api.yml
need_fix, old_lit, new_lit = plan_deploy()
print(f"\n[2] deploy-api.yml LIMEN_GITHUB_REPO literal: {'1 to fix' if need_fix else 'none (already organvm or absent)'}")
if need_fix:
print(f" {old_lit} → {new_lit}")
# 3) git remotes (EMIT ONLY)
remotes = plan_remotes()
print(f"\n[3] git checkouts under {WORKSPACE} with origin on an OLD owner = {len(remotes)} (emit-only, never run)")
for checkout, old_url, new_url in remotes[:8]:
print(f" {checkout}: {old_url} → {new_url}")
if len(remotes) > 8:
print(f" … +{len(remotes)-8} more")
remote_script = emit_remote_commands(remotes)
if emit_path:
emit_path.write_text(remote_script)
try:
emit_path.chmod(0o755)
except OSError:
pass
print(f"\n wrote git-remote commands → {emit_path} (review, run by hand AFTER transfer)")
# apply gate
if not apply:
print(f"\nDRY-RUN — nothing written. tasks.yaml refs that WOULD change: {n_tasks}")
print("Re-run with --apply (GATED) to write tasks.yaml + deploy-api.yml via the atomic writer.")
print("The git-remote commands are EMIT-ONLY in all modes; use --emit-remotes <file> to save them.")
return 0
print(f"\n⚠ --apply: writing tasks.yaml ({n_tasks} refs) + deploy-api.yml …")
if n_tasks:
apply_tasks(lf)
print(" ✓ tasks.yaml rewritten via atomic save_limen_file")
if need_fix and apply_deploy():
print(f" ✓ deploy-api.yml LIMEN_GITHUB_REPO → {TARGET}/limen")
print(" (git remotes NOT touched — run the emitted commands manually post-transfer.)")
print(f"\nReversible: `git -C {LIMEN_ROOT} checkout -- tasks.yaml .github/workflows/deploy-api.yml` "
"before committing, or re-run with the mapping reversed.")
return 0
if __name__ == "__main__":
raise SystemExit(main())