Spaces:
Sleeping
Sleeping
Download tools/sync_comment_lib.py from neosimHan/reels-bench: direct link, hf CLI and curl.
- Browser
- Download file 10.4 kB
-
https://huggingface.co/spaces/neosimHan/reels-bench/resolve/main/tools/sync_comment_lib.py
- Command line
-
hf download hf://spaces/neosimHan/reels-bench/tools/sync_comment_lib.py
-
curl -L -o sync_comment_lib.py https://huggingface.co/spaces/neosimHan/reels-bench/resolve/main/tools/sync_comment_lib.py
10.4 kB
| """comment-dashboard โ reels-bench-v2/comment_lib/ vendor ๋๊ธฐํ. | |
| ์ฌ์ฉ: | |
| python tools/sync_comment_lib.py # ์ค์ sync ์คํ | |
| python tools/sync_comment_lib.py --verify # ๊ธฐ์กด vendor์ import ๋ฌด๊ฒฐ์ฑ๋ง ๊ฒ์ฆ | |
| python tools/sync_comment_lib.py --src "C:/Users/USER/...comment-dashboard" # ๊ฒฝ๋ก ๋ช ์ | |
| ๋์: | |
| 1. comment-dashboard/{schema.py, parsers/*, analyzers/*, export/*, prompts/*} ๋ณต์ฌ | |
| 2. ๊ฐ .py์์ ๋ค์ ํจํด ์๋ rewrite: | |
| - "from schema import ..." โ "from comment_lib.schema import ..." | |
| - "from parsers.X import ..." โ "from comment_lib.parsers.X import ..." | |
| - "from analyzers.X import ..." โ "from comment_lib.analyzers.X import ..." | |
| - "from analyzers import X" โ "from comment_lib.analyzers import X" | |
| - "from export.X import ..." โ "from comment_lib.export.X import ..." | |
| - "from prompts.X import ..." โ "from comment_lib.prompts.X import ..." | |
| - sys.path.insert(..., os.path.dirname(...)) ์ค โ ์ญ์ (vendor ํ ๋ถํ์) | |
| 3. .SYNC_MANIFEST.json ๊ธฐ๋ก (source path + ISO timestamp + ํ์ผ๋ณ sha256) | |
| 4. --verify: ๊ฒฐ๊ณผ๋ฅผ importํด๋ณด๊ณ ์์ฌ bare import grep | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import hashlib | |
| import json | |
| import re | |
| import shutil | |
| import subprocess | |
| import sys | |
| from datetime import datetime | |
| from pathlib import Path | |
| ROOT = Path(__file__).resolve().parent.parent # reels-bench-v2/ | |
| DST = ROOT / "comment_lib" | |
| def _default_src() -> Path: | |
| return ROOT.parent / "comment-dashboard" | |
| # rewrite ๊ท์น (์์ ์ค์ โ ๊ธด ํจํด ๋จผ์ ๋งค์นญ) | |
| # (^[ \t]*) ๊ทธ๋ฃน์ผ๋ก ๋ค์ฌ์ฐ๊ธฐ ๋ณด์กด โ ํจ์ ๋ด๋ถ inline import๋ ์ก์ | |
| REWRITE_RULES = [ | |
| (re.compile(r"(?m)^([ \t]*)from\s+schema\s+import\s+"), r"\1from comment_lib.schema import "), | |
| (re.compile(r"(?m)^([ \t]*)from\s+parsers\.([\w_]+)\s+import\s+"), r"\1from comment_lib.parsers.\2 import "), | |
| (re.compile(r"(?m)^([ \t]*)from\s+parsers\s+import\s+"), r"\1from comment_lib.parsers import "), | |
| (re.compile(r"(?m)^([ \t]*)from\s+analyzers\.([\w_]+)\s+import\s+"), r"\1from comment_lib.analyzers.\2 import "), | |
| (re.compile(r"(?m)^([ \t]*)from\s+analyzers\s+import\s+"), r"\1from comment_lib.analyzers import "), | |
| (re.compile(r"(?m)^([ \t]*)from\s+export\.([\w_]+)\s+import\s+"), r"\1from comment_lib.export.\2 import "), | |
| (re.compile(r"(?m)^([ \t]*)from\s+export\s+import\s+"), r"\1from comment_lib.export import "), | |
| ] | |
| # sys.path ์กฐ์ ๋ผ์ธ ์ ๊ฑฐ (๋ค์ค ์ค ์ฒ๋ฆฌ) | |
| SYSPATH_INSERT_PATTERN = re.compile( | |
| r"^sys\.path\.insert\([^)]*os\.path\.dirname\([^)]*__file__[^)]*\)[^)]*\).*$", | |
| re.MULTILINE, | |
| ) | |
| # ๋ณต์ฌํ ํ์ผ/๋๋ ํ ๋ฆฌ spec | |
| FILES_TO_COPY = [ | |
| "schema.py", | |
| "parsers/__init__.py", | |
| "parsers/detect.py", | |
| "parsers/tiktok.py", | |
| "parsers/instagram_manual.py", | |
| "analyzers/__init__.py", | |
| "analyzers/_llm_utils.py", | |
| "analyzers/keywords.py", | |
| "analyzers/opinions.py", | |
| "analyzers/longest.py", | |
| "analyzers/sentiment.py", | |
| "analyzers/categorize.py", | |
| "analyzers/summary.py", | |
| "analyzers/translate.py", | |
| "export/__init__.py", | |
| "export/excel.py", | |
| "prompts/cluster_v1.md", | |
| "prompts/sentiment_v1.md", | |
| ] | |
| def rewrite_imports(text: str) -> tuple[str, dict]: | |
| """import rewrite + sys.path ์ ๊ฑฐ. (new_text, stats) ๋ฐํ.""" | |
| stats = {"import_rewrites": 0, "syspath_removed": 0} | |
| new_text = text | |
| for pat, repl in REWRITE_RULES: | |
| new_text, n = pat.subn(repl, new_text) | |
| stats["import_rewrites"] += n | |
| new_text, n = SYSPATH_INSERT_PATTERN.subn("# (sys.path.insert removed by sync_comment_lib.py)", new_text) | |
| stats["syspath_removed"] = n | |
| return new_text, stats | |
| def sha256_file(path: Path) -> str: | |
| h = hashlib.sha256() | |
| with path.open("rb") as f: | |
| for chunk in iter(lambda: f.read(1 << 16), b""): | |
| h.update(chunk) | |
| return h.hexdigest() | |
| def sha256_bytes(b: bytes) -> str: | |
| return hashlib.sha256(b).hexdigest() | |
| def get_source_commit(src: Path) -> str: | |
| """src๊ฐ git repo๋ฉด HEAD commit ๋ฐํ, ์๋๋ฉด 'no-git'.""" | |
| try: | |
| r = subprocess.run( | |
| ["git", "-C", str(src), "rev-parse", "HEAD"], | |
| capture_output=True, text=True, timeout=5, check=False, | |
| ) | |
| if r.returncode == 0: | |
| return r.stdout.strip() | |
| except Exception: | |
| pass | |
| return "no-git" | |
| def sync(src: Path) -> dict: | |
| """์ค์ vendor ์คํ.""" | |
| if not src.exists(): | |
| raise FileNotFoundError(f"comment-dashboard ํด๋ ์์: {src}") | |
| # ๊ธฐ์กด vendor ์ญ์ ํ ์ฌ์์ฑ | |
| if DST.exists(): | |
| shutil.rmtree(DST) | |
| DST.mkdir(parents=True) | |
| # __init__.py for comment_lib root | |
| (DST / "__init__.py").write_text( | |
| '"""Vendored copy of comment-dashboard. DO NOT EDIT BY HAND.\n' | |
| 'Re-sync via: python tools/sync_comment_lib.py\n"""\n', | |
| encoding="utf-8", | |
| ) | |
| manifest = { | |
| "synced_at": datetime.utcnow().isoformat(timespec="seconds") + "Z", | |
| "source_path": str(src), | |
| "source_commit": get_source_commit(src), | |
| "files": {}, | |
| "import_rewrites_total": 0, | |
| "syspath_removed_total": 0, | |
| } | |
| for rel in FILES_TO_COPY: | |
| src_path = src / rel | |
| if not src_path.exists(): | |
| print(f" โ skip (missing): {rel}") | |
| continue | |
| dst_path = DST / rel | |
| dst_path.parent.mkdir(parents=True, exist_ok=True) | |
| if rel.endswith(".py"): | |
| text = src_path.read_text(encoding="utf-8") | |
| new_text, stats = rewrite_imports(text) | |
| dst_path.write_text(new_text, encoding="utf-8") | |
| print(f" โ {rel} (rewrites={stats['import_rewrites']}, syspath_removed={stats['syspath_removed']})") | |
| manifest["files"][rel] = { | |
| "source_sha256": sha256_bytes(text.encode("utf-8")), | |
| "vendored_sha256": sha256_bytes(new_text.encode("utf-8")), | |
| "import_rewrites": stats["import_rewrites"], | |
| "syspath_removed": stats["syspath_removed"], | |
| } | |
| manifest["import_rewrites_total"] += stats["import_rewrites"] | |
| manifest["syspath_removed_total"] += stats["syspath_removed"] | |
| else: | |
| shutil.copy2(src_path, dst_path) | |
| print(f" โ {rel} (binary/text copy)") | |
| manifest["files"][rel] = { | |
| "source_sha256": sha256_file(src_path), | |
| "vendored_sha256": sha256_file(dst_path), | |
| } | |
| # manifest ์ ์ฅ | |
| (DST / ".SYNC_MANIFEST.json").write_text( | |
| json.dumps(manifest, ensure_ascii=False, indent=2), | |
| encoding="utf-8", | |
| ) | |
| return manifest | |
| def verify() -> int: | |
| """vendor ๋ฌด๊ฒฐ์ฑ ๊ฒ์ฆ. import ์ฑ๊ณต + ์์ฌ bare import = 0. ์คํจ ์ nonzero ๋ฐํ.""" | |
| if not DST.exists(): | |
| print(f"โ {DST} ์์. ๋จผ์ sync ์คํ ํ์.") | |
| return 1 | |
| errs = 0 | |
| # 1. import ๊ฒ์ฆ (๊ฐ ์ฃผ์ ๋ชจ๋ ๋ก๋) | |
| sys.path.insert(0, str(ROOT)) | |
| test_imports = [ | |
| "comment_lib", | |
| "comment_lib.schema", | |
| "comment_lib.parsers.detect", | |
| "comment_lib.parsers.tiktok", | |
| "comment_lib.parsers.instagram_manual", | |
| "comment_lib.analyzers._llm_utils", | |
| "comment_lib.analyzers.keywords", | |
| "comment_lib.analyzers.opinions", | |
| "comment_lib.analyzers.longest", | |
| "comment_lib.analyzers.sentiment", | |
| "comment_lib.analyzers.categorize", | |
| "comment_lib.analyzers.summary", | |
| "comment_lib.analyzers.translate", | |
| "comment_lib.export.excel", | |
| ] | |
| print("=== Import ๊ฒ์ฆ ===") | |
| for mod in test_imports: | |
| try: | |
| __import__(mod) | |
| print(f" โ {mod}") | |
| except Exception as e: | |
| print(f" โ {mod}: {e}") | |
| errs += 1 | |
| # 2. ์์ฌ bare import grep | |
| print("\n=== Bare import ์์ฌ ๊ฒ์ฌ (comment_lib/) ===") | |
| bad_patterns = [ | |
| re.compile(r"(?m)^[ \t]*from\s+schema\s+import"), | |
| re.compile(r"(?m)^[ \t]*from\s+parsers\.(?!comment_lib)"), | |
| re.compile(r"(?m)^[ \t]*from\s+analyzers\.(?!comment_lib)"), | |
| re.compile(r"(?m)^[ \t]*from\s+analyzers\s+import"), | |
| re.compile(r"(?m)^[ \t]*from\s+export\.(?!comment_lib)"), | |
| ] | |
| leftover = 0 | |
| for py in DST.rglob("*.py"): | |
| text = py.read_text(encoding="utf-8") | |
| for pat in bad_patterns: | |
| for m in pat.finditer(text): | |
| # comment_lib. ์ผ๋ก ์์ํ์ง ์๋ ๊ฒ๋ง | |
| line_start = text.rfind("\n", 0, m.start()) + 1 | |
| line_end = text.find("\n", m.start()) | |
| line = text[line_start:line_end] | |
| if "comment_lib." not in line: | |
| print(f" โ {py.relative_to(ROOT)}: {line.strip()}") | |
| leftover += 1 | |
| if leftover == 0: | |
| print(" โ ์์ฌ import 0๊ฑด") | |
| else: | |
| errs += leftover | |
| # 3. manifest ์กด์ฌ ํ์ธ | |
| manifest_path = DST / ".SYNC_MANIFEST.json" | |
| if manifest_path.exists(): | |
| m = json.loads(manifest_path.read_text(encoding="utf-8")) | |
| print(f"\n=== Manifest ===") | |
| print(f" synced_at: {m.get('synced_at')}") | |
| print(f" source_commit: {m.get('source_commit')}") | |
| print(f" files vendored: {len(m.get('files', {}))}") | |
| print(f" import rewrites: {m.get('import_rewrites_total')}") | |
| print(f" syspath removed: {m.get('syspath_removed_total')}") | |
| else: | |
| print(f"\nโ {manifest_path} ์์") | |
| errs += 1 | |
| return errs | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--src", type=Path, default=None) | |
| ap.add_argument("--verify", action="store_true", help="๊ธฐ์กด vendor๋ง ๊ฒ์ฆ") | |
| args = ap.parse_args() | |
| if args.verify: | |
| sys.exit(verify()) | |
| src = args.src or _default_src() | |
| print(f"Source: {src}") | |
| print(f"Target: {DST}") | |
| print() | |
| manifest = sync(src) | |
| print() | |
| print(f"โ sync ์๋ฃ. {len(manifest['files'])} ํ์ผ, " | |
| f"import rewrites {manifest['import_rewrites_total']}, " | |
| f"syspath ์ ๊ฑฐ {manifest['syspath_removed_total']}") | |
| print() | |
| print("์ด์ --verify ์คํ:") | |
| print(" python tools/sync_comment_lib.py --verify") | |
| if __name__ == "__main__": | |
| main() | |