Repository navigation
Expand file tree
/
Copy pathpush_all.py
More file actions
330 lines (281 loc) · 11.4 KB
/
Copy pathpush_all.py
File metadata and controls
330 lines (281 loc) · 11.4 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"""push_all.py - preview the cleanup, apply it, gate, commit and push.
Cleanup runs in two passes by itself: a dry pass that prints what would happen,
then the real pass. It pauses between them for a keypress unless CONFIRM=1.
DRY=1 stops after the preview and touches nothing.
Deletion is tracked-aware: files git already knows are removed with git rm so the
deletion is staged, untracked ones are unlinked. Three things survive and are
reported: the PROTECTED set, anything matching .keep_scratch, and any tracked
file whose name carries no scratch prefix. AUDIT=1 prints a verdict per file
and stops without touching anything.
The handoff block goes into PLAN.md first so scripts/handoff.py can regenerate
docs/HANDOFF.md from it, and every gate runs before a commit is created. Any gate
failure aborts with the reason. Set ALLOW=1 to commit anyway.
"""
import os
import subprocess
import sys
import pathlib
TOKEN_FILE = pathlib.Path(".push_token")
def load_token():
tok = os.environ.get("PUSH_TOKEN", "").strip()
if tok:
return tok
if TOKEN_FILE.exists():
return TOKEN_FILE.read_text(encoding="utf-8").strip()
return ""
TOKEN = load_token()
REMOTE = "origin"
BRANCH = "main"
PLAN = pathlib.Path("docs/PLAN.md")
MARK = "## 61. HANDOFF"
KEEP_FILE = pathlib.Path(".keep_scratch")
ALWAYS_KEEP = {"push_all.py", "seal.py", "Q9.py"}
# Real root files. Tracked files are already covered by the prefix test
# below, but this set is the hard floor and the place to register anything
# new that must never go. A pattern alone is not enough: p*.txt and
# verify*.py look harmless right up until requirements.txt and verify.py
# are gone.
PROTECTED = {
"push_all.py", "seal.py", "Q9.py", "setup.py", "bootstrap.py",
"requirements.txt", "start_server.py", "version.txt", "Makefile",
"LICENSE", "README.md", ".gitignore", ".env.example", "pyproject.toml",
"pytest.ini", "tox.ini", "conftest.py",
}
# A tracked file only goes if its name starts with one of these. Untracked
# files are still judged by pattern, which is the safe asymmetry: scratch is
# always untracked, and a real file is normally tracked by the time a sweep
# reaches it.
SCRATCH_PREFIXES = ("fix_", "diag_", "apply_", "patch_", "inspect_", "loop",
"verify_", "q11_", "regions", "publish", "amend_",
"cleanup_")
def has_scratch_prefix(name):
return name.startswith(SCRATCH_PREFIXES)
PATTERNS = ("fix_*.py", "fix_*.py.txt", "diag_*.py", "diag_*.py.txt",
"apply_*.py", "apply_*.py.txt", "patch_*.py", "patch_*.py.txt",
"inspect_*.py", "publish.py", "loop*.py", "loop*.log",
"verify*.py", "verify*.log", "q11_*.log", "regions*.txt", "p*.txt")
DRY = os.environ.get("DRY") == "1"
CONFIRM = os.environ.get("CONFIRM") == "1"
AUDIT = os.environ.get("AUDIT") == "1"
def run(cmd):
r = subprocess.run(cmd, capture_output=True, text=True)
return r.returncode, ((r.stdout or "") + (r.stderr or "")).strip()
def tracked_set():
rc, out = run(["git", "ls-files", "-z"])
return set(x for x in out.split("\0") if x)
def keep_set():
keep = set(ALWAYS_KEEP)
if KEEP_FILE.exists():
for ln in KEEP_FILE.read_text(encoding="utf-8").splitlines():
ln = ln.strip()
if ln and not ln.startswith("#"):
keep.add(ln)
return keep
def matches(name, keep):
if name in keep:
return True
return any(pathlib.PurePath(name).match(k) for k in keep
if any(ch in k for ch in "*?["))
def classify(name, rel, keep, tracked):
"""Keep reason, or None when the file may be deleted."""
if name in PROTECTED or rel in PROTECTED:
return "protected"
if matches(name, keep) or rel in keep:
return "listed"
if tracked and not has_scratch_prefix(name):
return "tracked-real"
return None
def collect():
seen, out = set(), []
for pattern in PATTERNS:
for p in sorted(pathlib.Path(".").glob(pattern)):
if p.is_file() and p.name not in seen:
seen.add(p.name)
out.append(p)
return out
def cleanup(dry):
"""One pass. dry=True only reports."""
tracked = tracked_set()
keep = keep_set()
print("tracked: %d keep list: %d" % (len(tracked), len(keep)))
counts = {}
def bump(k):
counts[k] = counts.get(k, 0) + 1
for p in collect():
rel = p.as_posix()
is_tracked = rel in tracked
reason = classify(p.name, rel, keep, is_tracked)
if reason:
print(" keep %-28s (%s)" % (p.name, reason))
bump("kept")
continue
if dry:
print(" %-11s %-28s %s" % ("would-git-rm" if is_tracked else "would-rm",
p.name, "TRACKED" if is_tracked else "new"))
bump("pending")
continue
if is_tracked:
rc, out = run(["git", "rm", "-f", "--quiet", rel])
print(" %-11s %-28s TRACKED" % ("git-rm" if rc == 0 else "FAIL", p.name))
bump("git-rm" if rc == 0 else "failed")
else:
try:
p.unlink()
print(" %-11s %-28s new" % ("rm", p.name))
bump("rm")
except OSError as exc:
print(" %-11s %-28s %s" % ("FAIL", p.name, exc))
bump("failed")
for junk in ("__pycache__", ".pytest_cache"):
for d in pathlib.Path(".").glob("**/" + junk):
if not d.is_dir() or "node_modules" in d.parts:
continue
files = [f for f in d.rglob("*") if f.is_file()]
hit = [f for f in files if f.as_posix() in tracked]
if dry:
print(" %-11s %s (%d tracked, %d total)" %
("would-clean", d.as_posix(), len(hit), len(files)))
continue
for f in hit:
run(["git", "rm", "-f", "--quiet", f.as_posix()])
for f in files:
if f.as_posix() not in tracked:
try:
f.unlink()
except OSError:
pass
try:
d.rmdir()
except OSError:
pass
print(" %-11s %s" % ("cleaned", d.as_posix()))
print(" -> %s" % (", ".join("%s=%d" % kv for kv in sorted(counts.items())) or "nothing"))
return counts
# ---------------- 0. cleanup: preview then apply ----------------
if AUDIT:
_t, _k = tracked_set(), keep_set()
print("=== victim audit ===")
for _p in collect():
_r = classify(_p.name, _p.as_posix(), _k, _p.as_posix() in _t)
print(" %-14s %s" % (_r or "DELETE", _p.name))
print("keep list: %d protected: %d" % (len(_k), len(PROTECTED)))
sys.exit(0)
print("=== cleanup preview ===")
cleanup(dry=True)
if DRY:
print("\nDRY — nothing changed")
sys.exit(0)
if not CONFIRM:
try:
input("\nEnter to apply, Ctrl-C to stop... ")
except (EOFError, KeyboardInterrupt):
print("\nstopped")
sys.exit(1)
print("\n=== cleanup applied ===")
cleanup(dry=False)
# ---------------- 1. preflight ----------------
rc, branch = run(["git", "rev-parse", "--abbrev-ref", "HEAD"])
print("\nbranch:", branch)
if branch != BRANCH:
print("NOTE not on %s; switching" % BRANCH)
run(["git", "checkout", BRANCH])
rc, dirty = run(["git", "status", "--porcelain"])
print("changed files: %d" % len(dirty.splitlines()))
if not dirty:
print("nothing to commit")
sys.exit(0)
# ---------------- 2. handoff block ----------------
if not PLAN.exists():
print("MISS docs/PLAN.md")
sys.exit(1)
text = PLAN.read_text(encoding="utf-8")
if MARK in text:
print("skip handoff block present")
else:
BLOCK = [
"",
"",
"=== HANDOFF SECTION ===",
"",
MARK,
"",
"Project: Pegasus Phantom Menace v2 / web backend",
"Status: Backend imports and serves; suite green (75 passed), tsc clean",
"Features: 20 device routes re-registered behind an opt-in token gate; sweep 1.3, 7.5, 11.1, 12.5 closed",
"Log: log/events.jsonl",
"Release: unreleased since v2.1.0",
"Push blocked: none",
"Next: Phase 4 — 47-endpoint physical-ops dashboard, 343-route accessibility sweep, clean-VM Electron build, iOS provisioning",
"Author: Trey A. Smith | Authenticated, Inc. | @projectzerodays",
"Notes: job_manager aliased to the module-level JobManager singleton; duplicate /api/jobs/<job_id> removed (106 -> 105 rules); jsonify and stdlib imports restored; _require_token on every device route behind PEGASUS_REQUIRE_AUTH; filterVectors wired into PayloadFactory; VITE_API_BASE documented; pytest --import-mode=importlib.",
"",
]
PLAN.write_text(text.rstrip() + "\n" + "\n".join(BLOCK), encoding="utf-8")
print("ok handoff block appended to docs/PLAN.md")
# ---------------- 3. regenerate handoff ----------------
rc, out = run([sys.executable, "scripts/handoff.py"])
print("gen handoff:", "ok" if rc == 0 else out[-300:])
# ---------------- 4. gates ----------------
GATES = [
["scripts/handoff.py", "--check"],
["scripts/emit_agents.py", "--check"],
["scripts/modules_lint.py"],
["scripts/caps_consistency.py"],
["scripts/require.py", "--lint"],
["scripts/version_check.py"],
["scripts/risk_lint.py"],
["scripts/no_platform_branch.py"],
["scripts/audit_derive.py", "--check"],
["scripts/bootstrap-check.py"],
]
failed = []
print("\n=== gates ===")
for g in GATES:
if not pathlib.Path(g[0]).exists():
print("SKIP %s (absent)" % g[0])
continue
rc, out = run([sys.executable] + g)
print("%s %s" % ("PASS" if rc == 0 else "FAIL", " ".join(g)))
if rc != 0:
failed.append((g, out))
if failed:
print("\n--- failures ---")
for g, out in failed:
print("\n%s\n%s" % (" ".join(g), out[-800:]))
if os.environ.get("ALLOW") != "1":
print("\naborting; set ALLOW=1 to commit anyway")
sys.exit(1)
# ---------------- 5. commit ----------------
run(["git", "add", "-A"])
rc, out = run(["git", "status", "--porcelain"])
print("\nstaged: %d" % len(out.splitlines()))
MSG = [
"P4: restore job_manager, gate device routes, close sweep 1.3/7.5/11.1/12.5",
"",
"- jobs.py aliases job_manager to the module-level JobManager singleton",
"- restore the sixteen register() calls and # System endpoints in app.py",
"- jobs_list reads the shared manager; duplicate /api/jobs/<job_id> removed (105 rules)",
"- restore jsonify, sys, platform, shutil, subprocess and pathlib imports",
"- opt-in _require_token on all 20 device routes behind PEGASUS_REQUIRE_AUTH",
"- wire filterVectors into PayloadFactory including the empty-state guard",
"- document VITE_API_BASE and VITE_PEGASUS_API_TOKEN, add .env.example",
"- pytest --import-mode=importlib; 75 tests pass, tsc clean",
]
cmd = ["git", "commit"]
for line in MSG:
cmd += ["-m", line]
rc, out = run(cmd)
print(out[-1200:])
if rc != 0:
print("commit failed")
sys.exit(1)
# ---------------- 6. push ----------------
if TOKEN:
url = "https://%s@github.com/ProjectZeroDays/Pegasus_Phantom-Menace.git" % TOKEN
run(["git", "remote", "set-url", REMOTE, url])
else:
print("no token in env or .push_token; using the remote URL as-is")
rc, out = run(["git", "push", REMOTE, BRANCH])
print("\n" + out.replace(TOKEN, "<token>")[-1500:])
print("\npush:", "ok" if rc == 0 else "FAILED")
sys.exit(0 if rc == 0 else 1)