Files
coruna-lab/tools/new_project.py
T
hashbro ea0be57180 init
2026-08-04 05:00:42 +08:00

112 lines
3.1 KiB
Python

#!/usr/bin/env python3
"""Reset lab-root web/ + sync/ from source/, then patch_all --apply (new DGA seeds/domains)."""
from __future__ import annotations
import argparse
import shutil
import subprocess
import sys
from pathlib import Path
TOOLS = Path(__file__).resolve().parent
LAB_ROOT = TOOLS.parent
SOURCE_ROOT = LAB_ROOT / "source"
CAMPAIGN_HASH = "34f5121f572d6742703eb84ec2f866a6"
def _ignore_junk(_dir: str, names: list[str]) -> set[str]:
skip = {"_bak", "__pycache__", ".DS_Store"}
return {n for n in names if n in skip or n.endswith(".pyc")}
def replace_tree(src: Path, dst: Path) -> None:
if dst.exists():
shutil.rmtree(dst)
shutil.copytree(src, dst, symlinks=False, ignore=_ignore_junk)
def main() -> int:
parser = argparse.ArgumentParser(
description=(
"Copy source/web + source/sync to coruna-lab root, then run "
"patch_all.py --apply --root <lab-root> (fresh Deployment/Reporting domains)."
)
)
parser.add_argument(
"--deployment-seed",
help="optional; forwarded to patch_all (default: random)",
)
parser.add_argument(
"--reporting-seed",
help="optional; forwarded to patch_all (default: random)",
)
parser.add_argument(
"-n",
"--count",
type=int,
default=5,
help="DGA candidates to print per pool (default 5)",
)
parser.add_argument(
"--skip-patch",
action="store_true",
help="only copy source trees to lab root; do not run patch_all",
)
args = parser.parse_args()
src_web = SOURCE_ROOT / "web"
src_sync = SOURCE_ROOT / "sync"
if not (src_web / CAMPAIGN_HASH).is_dir():
raise SystemExit(f"missing source campaign: {src_web / CAMPAIGN_HASH}")
if not src_sync.is_dir():
raise SystemExit(f"missing source sync: {src_sync}")
dst_web = LAB_ROOT / "web"
dst_sync = LAB_ROOT / "sync"
print("=== reset lab root from source ===")
print(f"from: {SOURCE_ROOT}")
print(f"to: {LAB_ROOT}/{{web,sync}}")
replace_tree(src_web, dst_web)
replace_tree(src_sync, dst_sync)
print(f"copied web/ ({CAMPAIGN_HASH}) + sync/")
if args.skip_patch:
print("skip-patch: done (source copy only)")
return 0
cmd = [
sys.executable,
str(TOOLS / "patch_all.py"),
"--apply",
"--root",
str(LAB_ROOT),
"-n",
str(args.count),
]
if args.deployment_seed:
cmd += ["--deployment-seed", args.deployment_seed]
if args.reporting_seed:
cmd += ["--reporting-seed", args.reporting_seed]
print()
print("=== patch_all --apply ===")
print("+", " ".join(cmd), flush=True)
subprocess.run(cmd, cwd=str(LAB_ROOT), check=True)
print()
print("=== ready ===")
print(f"web: {dst_web / CAMPAIGN_HASH}")
print(f"sync: {dst_sync}")
print(f"seeds: {LAB_ROOT / 'out' / 'seeds.json'}")
print(f"domains: {LAB_ROOT / 'out' / 'domains.json'}")
print()
print("serve example:")
print(f" cd {LAB_ROOT} && python3 -m http.server 8765 --bind 0.0.0.0")
return 0
if __name__ == "__main__":
raise SystemExit(main())