Spaces:
Running
Running
Download solar_eval/hub_hook/indexes.py from dev-strender/proofread-demo: direct link, hf CLI and curl.
- Browser
- Download file 16.8 kB
-
https://huggingface.co/spaces/dev-strender/proofread-demo/resolve/main/solar_eval/hub_hook/indexes.py
- Command line
-
hf download hf://spaces/dev-strender/proofread-demo/solar_eval/hub_hook/indexes.py
-
curl -L -o indexes.py https://huggingface.co/spaces/dev-strender/proofread-demo/resolve/main/solar_eval/hub_hook/indexes.py
16.8 kB
| """๋ฐ์ดํฐ์ ยทTagSetยท์ ์ฑ ์์ธ ๊ฐฑ์ (๊ณํ 2026-09-27 L6 ยง2.5 ยท D-L6-7 ยท ๊ฒํ F2ยทF9). | |
| **ํค.** `sha256(JCS([lock_sha, template_sha, text_mode, INDEX_EXTRACTOR, index_code_sha]))` โ `lock_sha` = lock ํ์ผ ๋ฐ์ดํธ sha256 | |
| (์์ผ๋ฉด null), `template_sha` = ์ ์ฑ ํ ํ๋ฆฟ ๋ฐ์ดํธ sha256(์์ผ๋ฉด null), `text_mode` = `optin.text_mode()`, `INDEX_EXTRACTOR` = | |
| `poc_bundle.index.row_tags.EXTRACTOR`, `index_code_sha` = **`poc_bundle` ํจํค์ง ์ ์ฒด**์ `*.py` (์๋ ๊ฒฝ๋ก, ๊ธธ์ด, ๋ฐ์ดํธ)๋ฅผ | |
| ๊ฒฝ๋ก์์ผ๋ก ์ด์ sha256. ์์ธ ์ถ๋ ฅ์ `index/` ๋ฐ ๋ชจ๋(`keys`ยท`scoring.policy`ยท`scoring.ids`ยท`snapshot`ยท`lock_match`ยท`canonical` โฆ) | |
| ์๋ ๋ฌ๋ ค ์์ด `index/*.py` ๋ง ๋ณด๋ฉด ๊ทธ์ชฝ ๋ณ๊ฒฝ์ ๋์น๋ค(๋ฆฌ๋ทฐ ์ง์ 3) โ ๋๊ฒ ์ก์ PB ๊ฐ ๋ฐ๋๋ฉด ํ ๋ฒ ๋ ๋ง๋๋ ์ชฝ์ ํํ๋ค | |
| (`poc_bundle.__version__` ์ ๊ณ ์ ๊ฐ์ด๋ผ ์ฝ๋ ๋ณํ๋ฅผ ๋ชป ์ก๋๋ค). ํค ํ์ผ์ `<hub_reports_dir>/<a>/<c>/hook/index-key.json`. | |
| **ํ์ (์์๋๋ก).** | |
| 1. lock ์ด ์์ผ๋ฉด(`load_existing_lock(layout) is None`) ์ธ ๋ช ๋ น ๋ชจ๋ ๊ฑด๋๋ด๋ค โ `skipped:no_lock`(์คํจ ์๋, D-L6-11). | |
| click ์์ธ ๋ฌธ๊ตฌ๋ ํ์ฑํ์ง ์๋๋ค โ CLI ๋ฅผ ๋ถ๋ฅด๊ธฐ **์ ์** ๊ฐ๋ฅธ๋ค. ๋ ์ด์์ยทlock ์ ์ฝ์ง ๋ชปํ๋ฉด(๊นจ์ง lock ๋ฑ) ๊ฐ๋ฅด์ง ์๊ณ | |
| CLI ๋ฅผ ๋ถ๋ฌ ๊ทธ์ชฝ์ด ์คํจ๋ฅผ ๋ณด๊ณ ํ๊ฒ ๋๋ค. | |
| 2. ์ ์ฑ ํ ํ๋ฆฟ์ด ์์ผ๋ฉด `tags`ยท`policy` ๋ ๊ฑด๋๋ฐ๊ณ `datasets` ๋ง โ ๊ฒฐ๊ณผ `written:datasets_only`. | |
| 3. ํค๊ฐ ๊ฐ๊ณ ์์ธ ๋ฃจํธ์ ๊ทธ case ๋๋ ํ ๋ฆฌ(๋ ๋ช ๋ น์ ๊ฒ โ `datasets/`, ํ ํ๋ฆฟ์ด ์์ผ๋ฉด `tagsets/`ยท`policies/` ๋)๊ฐ ๋ชจ๋ ์์ผ๋ฉด | |
| `unchanged`. ์๋๋ฉด **์ ํค๋ฅผ ๋จผ์ ์ง์ฐ๊ณ ** ๋ช ๋ น์ ์ฐจ๋ก๋ก ๋ถ๋ฅธ ๋ค, ๊ฑด๋๋ฐ์ง ์์ ๋ช ๋ น์ด ๋ชจ๋ ์ฑ๊ณตํ์ ๋๋ง ์ ํค๋ฅผ ์ด๋ค | |
| (์์์ ์ฐ๊ธฐ). ์ ํค๋ฅผ ๋จ๊ฒจ ๋๋ฉด ๋ช ๋ น์ด ์ค๊ฐ์ ์คํจํ์ ๋(datasets ๋ ์ ์ ๋ ฅ์ผ๋ก ๋ค์ ์ฐ๊ณ ์ ํ์ผ์ ์ง์ ๋๋ฐ tags ๊ฐ | |
| ์คํจ) ์ ๋ ฅ์ด ์ ๊ฐ์ผ๋ก ๋์์ค๋ ์๊ฐ `unchanged` ๋ก ์คํํด ์์ธ ์ฌ์ด๋์นด๊ฐ ์์ ๋จ๋๋ค(๋ฆฌ๋ทฐ ์ง์ 1). ์ค๊ฐ์ | |
| KeyboardInterrupt ๋ ๊ฐ์ ๊ธธ๋ก ๋งํ๋ค. | |
| **๋จ์ ์ ์ถ๋ ฅ ์น์ฐ๊ธฐ(๋ฆฌ๋ทฐ ์ง์ 2).** ๊ฑด๋๋ด ๋ช ๋ น์ด ์์ ์ ์ด ๋๋ ํ ๋ฆฌ๋ hub ๊ฐ ๊ทธ๋๋ก ์ฝ๋๋ค(`ES/src/hub/stages/tagsets.ts` | |
| `loadPolicies` โ ์๋ ๋๋ ํ ๋ฆฌ๋ ๋น ๋ชฉ๋ก์ด๋ค). ๊ทธ๋์ lock ์ด ์์ผ๋ฉด `datasets/`ยท`tagsets/`ยท`policies/`(์ ํค ํ์ผ)๋ฅผ, ํ ํ๋ฆฟ๋ง | |
| ์์ผ๋ฉด `tagsets/`ยท`policies/` ๋ฅผ ์์ธ ๋ฃจํธ์ ๊ทธ case ์๋์์ ์ง์ด๋ค โ lockยทํ ํ๋ฆฟ์์๋ง ๋์ค๋ ํ์๋ฌผ์ด๋ค. ๋ฒ๋ค ์์ธ | |
| (`bundles/`)์ ๊ฑด๋๋ฆฌ์ง ์๋๋ค. ์ง์ด ์ด๋ฆ์ `IndexOutcome.removed` ์ stderr ํ ์ค๋ก ์๋ฆฐ๋ค. ๋ ์ด์์ยทlock ์ ๋ชป ์ฝ์ด ์ธ ๋ช ๋ น์ | |
| ๋ชจ๋ ๋ถ๋ฅด๋ ๊ฒฝ์ฐ์๋ ์ง์ฐ์ง ์๋๋ค(์๋ค๊ณ ํ์ ํ์ง ๋ชปํ๋ค). | |
| **ํธ์ถ.** `poc_bundle.index.cli.index_group.main([...], prog_name="poc-bundle index", standalone_mode=False)` โ ์์นด์ด๋ธ ๋ฃจํธ | |
| (`--projects-dir target.projects_dir`)ยท์์ธ ๋ฃจํธ(`--index-root hub_index_root()`)ยทํ ์คํธ ๋ชจ๋(`--text`)๋ฅผ **๋ช ์**ํ๋ค. stdoutยท | |
| stderr ๋ ๋ฒํผ์ ๋ฐ๋๋ค(๊ฒฝ๋กยท๊ฑด์๋ฟ์ด๋ผ ํ ์ฝ์์ ๋ค๋ฆฌ๋ง๋ค ํ ์ค์ด๋ค). ๋ค๋ง stderr ์ **`warning:` ์ค์ ๋ฒ๋ฆฌ์ง ์๋๋ค**(๋ฆฌ๋ทฐ | |
| ์ง์ 4) โ `index datasets` ์ "๊ฐ์ dataset_version ์ ๋๋ ๊ฐ์ง lock ํญ๋ชฉ"(hub ๋ ๊ทธ์ค ํ๋๋ง ์ฃ๋๋ค)ยท`index policy` ์ "๊ด์ธก 0 ์ธ | |
| ์ ์ธ ํ๊ทธ" ๊ฐ์ ์ ํฉ์ฑ ๊ฒฝ๊ณ ๋ค. ๋ช ๋ น ์ด๋ฆ์ ๋ถ์ฌ stderr ์ ๋ค์ ๋ด๊ณ `IndexOutcome.warnings` ์ ์ฃ๋๋ค(๋ฒ๋ค ๋ค๋ฆฌ๋ detail ์ | |
| ๊ฑด์๋ง ๋๋ค โ ์ํ ํ์ผ์ ์ฝ๋ยท๊ฑด์๋ง). ์คํจ๋ `failed:index_<๋ช ๋ น>:<์ข ๋ฃ ์ฝ๋>` ์ด๊ณ ์ฌ๋์ฉ ์ด์ ๋ click ์ค๋ฅ ๋ฌธ๊ตฌ(๊ฒฝ๋กยท์ฝ๋๋ง | |
| โ `index/cli.py` ๋ชจ๋ ์ค๋ช "ํ ๋ณธ๋ฌธ์ ์ฃ์ง ์๋๋ค")์ ๋ง์ง๋ง ์ค์ด๋ค. | |
| ๋๋ ค์ฃผ๋ ๊ฐ: `unchanged` ยท `written` ยท `written:datasets_only` ยท `skipped:no_lock` ยท `failed:index_<๋ช ๋ น>:<exit>`. | |
| `poc_bundle` ์ ํจ์ ์์์๋ง import ํ๋ค(D-L6-14). | |
| """ | |
| from __future__ import annotations | |
| import contextlib | |
| import hashlib | |
| import io | |
| import json | |
| import os | |
| from collections.abc import Sequence | |
| from dataclasses import dataclass | |
| from pathlib import Path | |
| from typing import Any | |
| import click | |
| from solar_eval.hub_hook import optin | |
| from solar_eval.hub_hook.types import ( | |
| HOOK_DIRNAME, | |
| INDEX_CMD_DATASETS, | |
| INDEX_CMD_POLICY, | |
| INDEX_CMD_TAGS, | |
| INDEX_FAILED_PREFIX, | |
| INDEX_KEY_FILENAME, | |
| INDEX_UNCHANGED, | |
| INDEX_WRITTEN, | |
| INDEX_WRITTEN_DATASETS_ONLY, | |
| NO_LOCK, | |
| STATUS_FAILED, | |
| STATUS_SKIPPED, | |
| Target, | |
| ) | |
| #: ์์ธ ๋ช ๋ น๊ณผ ๊ทธ ๋ช ๋ น์ด ์ฐ๋ ์์ธ ๋ฃจํธ ์๋ ๋๋ ํ ๋ฆฌ(`<index_root>/accounts/<a>/<c>/<dir>`). | |
| CMD_DATASETS, CMD_TAGS, CMD_POLICY = INDEX_CMD_DATASETS, INDEX_CMD_TAGS, INDEX_CMD_POLICY | |
| OUTPUT_DIRS = {CMD_DATASETS: "datasets", CMD_TAGS: "tagsets", CMD_POLICY: "policies"} | |
| PROG_NAME = "poc-bundle index" | |
| #: ํค ํ์ผ ์คํค๋ง โ ํ ๋ด๋ถ ๊ธฐ๋ก(์ฌ๋ยท๋ค๋ฅธ ๋๊ตฌ๊ฐ ์ฝ์ง ์๋๋ค). | |
| INDEX_KEY_SCHEMA = "solar-eval-hook-index-key/v1" | |
| #: ์์ธ CLI stderr ์์ ์ ํฉ์ฑ ๊ฒฝ๊ณ ๋ก ๋ณด๋ ์ค ๋จธ๋ฆฌ(`poc_bundle/index/cli.py` ์ `warning: โฆ`). | |
| WARNING_PREFIX = "warning:" | |
| class IndexOutcome: | |
| """`refresh` ๊ฒฐ๊ณผ โ `code` ๋ ๋ชจ๋ ์ค๋ช ์ ๋ฐํ ์ดํ, `reason` ์ ์คํจ์ผ ๋๋ง ์ฌ๋์ฉ ํ ์ค. | |
| `warnings` ๋ ์ฑ๊ณตํ ๋ช ๋ น์ stderr `warning:` ์ค(`index <๋ช ๋ น>: ` ์ ๋), `removed` ๋ ์ด๋ฒ์ ์น์ด ์ ์ถ๋ ฅ ๋๋ ํ ๋ฆฌ ์ด๋ฆ. | |
| """ | |
| code: str | |
| reason: str | None = None | |
| warnings: tuple[str, ...] = () | |
| removed: tuple[str, ...] = () | |
| def failed(self) -> bool: | |
| return self.code.startswith(f"{STATUS_FAILED}:") | |
| class IndexCall: | |
| """์์ธ ๋ช ๋ น ํ ๋ฒ์ ๊ฒฐ๊ณผ โ ์ข ๋ฃ ์ฝ๋, ์คํจ๋ฉด click ๋ฌธ๊ตฌ ๋ง์ง๋ง ์ค, stderr ์ `warning:` ์ค.""" | |
| exit_code: int | |
| reason: str | None = None | |
| warnings: tuple[str, ...] = () | |
| class _Plan: | |
| commands: tuple[str, ...] | |
| key: str | None | |
| parts: dict[str, Any] | None | |
| def refresh_case_indexes(target: Target) -> str: | |
| """case ํ๋์ ์์ธ์ ํค๊ฐ ๋ฐ๋์์ ๋๋ง ๋ค์ ๋ง๋ ๋ค(D-L6-7). ๋ฐํ ์ดํ๋ ๋ชจ๋ ์ค๋ช .""" | |
| return refresh(target).code | |
| def refresh(target: Target) -> IndexOutcome: | |
| """`refresh_case_indexes` ์ ๊ฐ๊ณ ์คํจ ์ด์ (์ฌ๋์ฉ ํ ์ค)๋ฅผ ํจ๊ป ๋๋ ค์ค๋ค.""" | |
| plan = _plan(target) | |
| root = _index_root() | |
| removed = _remove_stale_outputs(target, plan, root) | |
| if not plan.commands: | |
| # lock ์ด ์๋ค โ ํค๋ ์ด์ ๊ฐ๋ฆฌํฌ ์์ธ์ด ์๋ค(lock ์ด ๋์์ค๋ฉด ๋๋ ํ ๋ฆฌ๊ฐ ์์ด ๋ค์ ๋ง๋ ๋ค) | |
| key_path(target).unlink(missing_ok=True) | |
| return _announce(target, IndexOutcome(f"{STATUS_SKIPPED}:{NO_LOCK}", removed=removed)) | |
| if plan.key is not None and _unchanged(target, plan, root): | |
| return _announce(target, IndexOutcome(INDEX_UNCHANGED, removed=removed)) | |
| # ์ ํค๋ฅผ ๋จผ์ ์ง์ด๋ค โ ๋ช ๋ น์ด ์ค๊ฐ์ ์คํจํ๋ฉด ์ฌ์ด๋์นด๊ฐ ์์ธ ์ฑ ์ ํค๊ฐ ๋จ์ `unchanged` ๋ก ์คํํ๋ค(๋ฆฌ๋ทฐ ์ง์ 1) | |
| key_path(target).unlink(missing_ok=True) | |
| from poc_bundle.index.case import index_lock_derived | |
| warnings: list[str] = [] | |
| for command, call in index_lock_derived( | |
| target.case_ref, | |
| root, | |
| repo_root=target.repo_root, | |
| commands=plan.commands, | |
| text_mode=optin.text_mode(), | |
| projects_dir=target.projects_dir, | |
| invoke=invoke_index, | |
| ): | |
| warnings += [f"index {command}: {line}" for line in call.warnings] | |
| if call.exit_code != 0: | |
| code = f"{STATUS_FAILED}:{INDEX_FAILED_PREFIX}{command}:{call.exit_code}" | |
| return _announce(target, IndexOutcome(code, call.reason, tuple(warnings), removed)) | |
| if plan.key is not None and plan.parts is not None: | |
| _write_key(target, plan.key, plan.parts) | |
| code = INDEX_WRITTEN if CMD_POLICY in plan.commands else INDEX_WRITTEN_DATASETS_ONLY | |
| return _announce(target, IndexOutcome(code, warnings=tuple(warnings), removed=removed)) | |
| def _announce(target: Target, outcome: IndexOutcome) -> IndexOutcome: | |
| """์น์ด ๋๋ ํ ๋ฆฌยท์์ธ ๊ฒฝ๊ณ ๋ฅผ stderr ์ ํ ์ค์ฉ(๊ฒฝ๋กยทํ๊ทธ ์ด๋ฆยท๊ฑด์๋ฟ โ ํ ๋ณธ๋ฌธ์ CLI ๊ฐ ์ฃ์ง ์๋๋ค).""" | |
| if outcome.removed: | |
| names = "ยท".join(f"{name}/" for name in outcome.removed) | |
| click.secho( | |
| f" ์์ธ({target.case_ref}): lockยท์ ์ฑ ํ ํ๋ฆฟ์ด ์์ด ์ {names} ๋ฅผ ์น์ ๋ค", | |
| fg="yellow", | |
| err=True, | |
| ) | |
| for line in outcome.warnings: | |
| click.secho(f" ์์ธ ๊ฒฝ๊ณ ({target.case_ref}) โ {line}", fg="yellow", err=True) | |
| return outcome | |
| def _remove_stale_outputs(target: Target, plan: _Plan, root: Path) -> tuple[str, ...]: | |
| """๊ฑด๋๋ด ๋ช ๋ น์ด ์์ ์ ์ด ์ถ๋ ฅ ๋๋ ํ ๋ฆฌ๋ฅผ ์ง์ด๋ค(๋ฆฌ๋ทฐ ์ง์ 2). ์ง์ด ๋๋ ํ ๋ฆฌ ์ด๋ฆ์ ๋๋ ค์ค๋ค. | |
| lockยทํ ํ๋ฆฟ์ด ์๋ค๊ณ **ํ์ **ํ์ ๋๋ง(`plan.key` ๊ฐ ์๊ฑฐ๋ ๋ช ๋ น์ด 0) โ ๋ชป ์ฝ์ด ์ธ ๋ช ๋ น์ ๋ชจ๋ ๋ถ๋ฅด๋ ๊ฒฝ์ฐ๋ ์น์ธ ๊ฒ์ด ์๋ค. | |
| ์ฌ๋ณผ๋ฆญ ๋งํฌ๋ ๋งํฌ๋ง ์ง์ด๋ค(๊ฐ๋ฆฌํค๋ ๊ณณ์ ๊ฑด๋๋ฆฌ์ง ์๋๋ค). | |
| """ | |
| from poc_bundle.index.case import prune_skipped_lock_indexes | |
| return prune_skipped_lock_indexes(root, target.account, target.case, plan.commands) | |
| # โโ ์ ํ ํ์ ยทํค โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def _plan(target: Target) -> _Plan: | |
| """๋ ๋ช ๋ น๊ณผ ํค. lock ์ด ์์ผ๋ฉด ๋ช ๋ น 0. ๋ ์ด์์ยทlock ์ ๋ชป ์ฝ์ผ๋ฉด ํค ์์ด ์ธ ๋ช ๋ น(CLI ๊ฐ ์คํจ๋ฅผ ๋ณด๊ณ ).""" | |
| from poc_bundle.data.data_root import build_case_layout, load_existing_lock | |
| from poc_bundle.data.errors import DataError | |
| try: | |
| layout = build_case_layout( | |
| target.repo_root, target.case_ref, projects_dir=target.projects_dir | |
| ) | |
| found = load_existing_lock(layout) | |
| except (DataError, OSError): | |
| return _Plan((CMD_DATASETS, CMD_TAGS, CMD_POLICY), None, None) | |
| if found is None: | |
| return _Plan((), None, None) | |
| template = _template_bytes(target) | |
| commands = (CMD_DATASETS,) if template is None else (CMD_DATASETS, CMD_TAGS, CMD_POLICY) | |
| parts = _key_parts(found[1], template) | |
| return _Plan(commands, _key_of(parts), parts) | |
| def _template_bytes(target: Target) -> bytes | None: | |
| """์ ์ฑ ํ ํ๋ฆฟ(`index policy` ๊ธฐ๋ณธ ์์น) ๋ฐ์ดํธ. ์์ผ๋ฉด None. ์๋๋ฐ ๋ชป ์ฝ์ผ๋ฉด `OSError` ๋ฅผ ์ฌ๋ฆฐ๋ค(๋ฒ๋ค ๋ค๋ฆฌ๊ฐ ๋ณด๊ณ ).""" | |
| from poc_bundle.index.cli import TEMPLATE_DEFAULT | |
| path = target.repo_root / "accounts" / target.account / target.case / TEMPLATE_DEFAULT | |
| if not path.exists() and not path.is_symlink(): | |
| return None | |
| return path.read_bytes() | |
| def _key_parts(lock_bytes: bytes, template: bytes | None) -> dict[str, Any]: | |
| from poc_bundle.index.row_tags import EXTRACTOR | |
| return { | |
| "lock_sha": _sha256(lock_bytes), | |
| "template_sha": None if template is None else _sha256(template), | |
| "text_mode": optin.text_mode(), | |
| "extractor": EXTRACTOR, | |
| "index_code_sha": index_code_sha(), | |
| } | |
| def _key_of(parts: dict[str, Any]) -> str: | |
| from poc_bundle.canonical import jcs_bytes | |
| ordered = [ | |
| parts["lock_sha"], | |
| parts["template_sha"], | |
| parts["text_mode"], | |
| parts["extractor"], | |
| parts["index_code_sha"], | |
| ] | |
| return _sha256(jcs_bytes(ordered)) | |
| def index_code_sha(package_root: Path | None = None) -> str: | |
| """`poc_bundle` ํจํค์ง ์ ์ฒด `*.py` ์ (์๋ ๊ฒฝ๋ก, ๊ธธ์ด, ๋ฐ์ดํธ)๋ฅผ ๊ฒฝ๋ก์์ผ๋ก ์ด์ sha256(๊ฒํ F9 ยท ๋ฆฌ๋ทฐ ์ง์ 3). | |
| ์์ธ ์ถ๋ ฅ์ `index/` ๋ฐ PB ๋ชจ๋(`keys.dataset_version` ๋ฑ)์๋ ๋ฌ๋ ค ์๋ค โ ๊ทธ๋์ `index/` ๋ง์ด ์๋๋ผ ํจํค์ง ์ ์ฒด๋ฅผ ๋ณธ๋ค. | |
| `package_root` ๋ ์ํ์ฉ(๊ธฐ๋ณธ์ ์ค์น๋ `poc_bundle` ํจํค์ง ๋๋ ํ ๋ฆฌ). ๊ฒฝ๋ก๋ฅผ ์์ด ์ด๋ฆ ๋ฐ๊พธ๊ธฐยท์ฎ๊ธฐ๊ธฐ๋ ์ก๋๋ค. | |
| """ | |
| if package_root is None: | |
| import poc_bundle | |
| package_root = Path(poc_bundle.__file__).parent | |
| digest = hashlib.sha256() | |
| sources = sorted( | |
| (p.relative_to(package_root).as_posix(), p) for p in package_root.rglob("*.py") | |
| ) | |
| for rel, path in sources: | |
| data = path.read_bytes() | |
| digest.update(f"{rel}\0{len(data)}\0".encode()) | |
| digest.update(data) | |
| return digest.hexdigest() | |
| def _sha256(data: bytes) -> str: | |
| return hashlib.sha256(data).hexdigest() | |
| def key_path(target: Target) -> Path: | |
| """`<hub_reports_dir>/<a>/<c>/hook/index-key.json`.""" | |
| from poc_bundle.home import hub_reports_dir | |
| return hub_reports_dir() / target.account / target.case / HOOK_DIRNAME / INDEX_KEY_FILENAME | |
| def _stored_key(target: Target) -> str | None: | |
| """์ ์ฅ๋ ํค. ์๊ฑฐ๋ ๊นจ์ก์ผ๋ฉด None โ ๋ค์ ๋ง๋ ๋ค(ํค ํ์ผ์ ์บ์์ผ ๋ฟ์ด๋ค).""" | |
| try: | |
| doc = json.loads(key_path(target).read_text("utf-8")) | |
| except (OSError, ValueError): | |
| return None | |
| key = doc.get("key") if isinstance(doc, dict) else None | |
| return key if isinstance(key, str) else None | |
| def _write_key(target: Target, key: str, parts: dict[str, Any]) -> None: | |
| """์์ ํ์ผ โ `os.replace`(์์์ ). ์คํจ๋ ํธ์ถ์์๊ฒ ์ฌ๋ฆฐ๋ค โ ์กฐ์ฉํ ์ผํค๋ฉด ๋งค๋ฒ ๋ค์ ๋ง๋ ๋ค๋ ์ฌ์ค์ด ์จ๋๋ค.""" | |
| path = key_path(target) | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| doc = {"schema": INDEX_KEY_SCHEMA, "key": key, "parts": parts} | |
| tmp = path.with_name(f".{path.name}.{os.getpid()}.tmp") | |
| tmp.write_text(json.dumps(doc, ensure_ascii=False, sort_keys=True) + "\n", "utf-8") | |
| os.replace(tmp, path) | |
| def _index_root() -> Path: | |
| from poc_bundle.home import hub_index_root | |
| return hub_index_root() | |
| def _unchanged(target: Target, plan: _Plan, root: Path) -> bool: | |
| if _stored_key(target) != plan.key: | |
| return False | |
| case_dir = root / "accounts" / target.account / target.case | |
| return all((case_dir / OUTPUT_DIRS[c]).is_dir() for c in plan.commands) | |
| # โโ click ํธ์ถ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def command_args(target: Target, command: str, root: Path) -> list[str]: | |
| """์์ธ ๋ช ๋ น ํ๋์ ์ธ์ โ ๋ฃจํธ๋ ๋ชจ๋ ๋ช ์ํ๋ค(CLI ๊ธฐ๋ณธ๊ฐ์ ๊ฑฐ์น์ง ์๋๋ค).""" | |
| from poc_bundle.index.case import lock_index_args | |
| return lock_index_args( | |
| command, | |
| target.case_ref, | |
| root, | |
| text_mode=optin.text_mode(), | |
| projects_dir=target.projects_dir, | |
| ) | |
| def invoke_index(args: Sequence[str]) -> IndexCall: | |
| """`poc-bundle index <args>` ๋ฅผ `standalone_mode=False` ๋ก โ ์ข ๋ฃ ์ฝ๋, ์คํจ๋ฉด click ๋ฌธ๊ตฌ ๋ง์ง๋ง ์ค, stderr ๊ฒฝ๊ณ ์ค.""" | |
| from poc_bundle.index.cli import index_group | |
| sink_out, sink_err = io.StringIO(), io.StringIO() | |
| try: | |
| with contextlib.redirect_stdout(sink_out), contextlib.redirect_stderr(sink_err): | |
| rv = index_group.main(list(args), prog_name=PROG_NAME, standalone_mode=False) | |
| except click.ClickException as exc: | |
| return _call(exc.exit_code, sink_err, _last_line(exc.format_message())) | |
| except click.exceptions.Exit as exc: | |
| return _call(exc.exit_code, sink_err) | |
| except click.Abort: | |
| return _call(1, sink_err, "aborted") | |
| except SystemExit as exc: | |
| return _call(exc.code if isinstance(exc.code, int) else 1, sink_err) | |
| return _call(rv if isinstance(rv, int) and not isinstance(rv, bool) else 0, sink_err) | |
| def _call(exit_code: int, sink_err: io.StringIO, reason: str | None = None) -> IndexCall: | |
| """๋ฒํผ์์ ๊ฒฝ๊ณ ์ค์ ๋ฝ์ ๊ฒฐ๊ณผ๋ฅผ ๋ง๋ ๋ค. ์คํจ์ธ๋ฐ ์ด์ ๊ฐ ์์ผ๋ฉด stderr ๋ง์ง๋ง ์ค์ ์ด์ ๋ก.""" | |
| text = sink_err.getvalue() | |
| if exit_code != 0 and reason is None: | |
| reason = _last_line(text) | |
| return IndexCall(exit_code, None if exit_code == 0 else reason, warning_lines(text)) | |
| def warning_lines(text: str) -> tuple[str, ...]: | |
| """stderr ๋ฒํผ์์ `warning:` ๋ก ์์ํ๋ ์ค(์๋ค ๊ณต๋ฐฑ ์ ๊ฑฐ)๋ง.""" | |
| return tuple( | |
| line.strip() for line in text.splitlines() if line.strip().startswith(WARNING_PREFIX) | |
| ) | |
| def _last_line(text: str) -> str | None: | |
| lines = [line.strip() for line in text.splitlines() if line.strip()] | |
| return lines[-1] if lines else None | |