Spaces:
Sleeping
Sleeping
Download solar_eval/core/snapshot/environment.py from dev-strender/proofread-demo: direct link, hf CLI and curl.
- Browser
- Download file 7.85 kB
-
https://huggingface.co/spaces/dev-strender/proofread-demo/resolve/main/solar_eval/core/snapshot/environment.py
- Command line
-
hf download hf://spaces/dev-strender/proofread-demo/solar_eval/core/snapshot/environment.py
-
curl -L -o environment.py https://huggingface.co/spaces/dev-strender/proofread-demo/resolve/main/solar_eval/core/snapshot/environment.py
7.85 kB
| """์คํ ํ๊ฒฝ ์ฌ๋ฃ โ ์์กด ๋ฒ์ ยท์์ง ๋ฒ์ ยท์์ง ๋ ํฌ ์ํ (๊ณํ 2026-09-24-phase1a D8ยทD9). | |
| config ์ ๋ค์ด๊ฐ๋ ๊ฒ(์ง๋ฌธ ๋์)์ ์ถ๋ ฅ ๊ฒฝ๋ก์ ์๋ **์ง์ ** ๋ฐํ์ ์์กด๊ณผ ์์ง ๋ฒ์ ยทํ์ด์ฌ ๋ถ๋ฒ์ ๋ฟ์ด๋ค. | |
| ์ ์ด ์ค์น ํํฌยทuv.lockยท์์ง ์ปค๋ฐยทํธ๋ฆฌ ํด์ยทdirty ๋ provenance(์ง๋ฌธ ๋ฐ)๋ก ๊ฐ๋ค โ ๋ฌด๊ดํ ์ํฌ์คํ์ด์ค | |
| ๋ฉค๋ฒ์ ๋ณํ๊ฐ ์ค์ ์ง๋ฌธ์ ํ๋ค์ง ์๊ฒ. | |
| """ | |
| from __future__ import annotations | |
| import logging | |
| import subprocess | |
| import sys | |
| from collections.abc import Callable | |
| from importlib import metadata | |
| from pathlib import Path | |
| from typing import Any | |
| from packaging.requirements import InvalidRequirement, Requirement | |
| from packaging.utils import canonicalize_name | |
| from poc_bundle.canonical import content_sha256 | |
| import solar_eval | |
| from solar_eval._version import __version__ | |
| from solar_eval.core.snapshot.impl_hash import ImplRoots, engine_tree_sha256 | |
| logger = logging.getLogger(__name__) | |
| #: ์์ง ๋ฐฐํฌ ์ด๋ฆ. | |
| ENGINE_DISTRIBUTION = "solar-eval" | |
| #: ๋ชจ๋ธ ์ ์ถ๋ ฅ์ ๋ฟ์ง ์๋ ์ง์ ์์กด โ CLI ํ์ยท์ค์ ๋ก๋ยท๋ฒ๋ค ๋๊ตฌ. `dependency_lock` ์์ ๋บ๋ค. | |
| #: ์ ์ง์ ์์กด์ ์ถ๊ฐํ๋ฉด ์ด ๋ชฉ๋ก์ด๋ ์ถ๋ ฅ ์์กด ์ค ํ๋๋ก ๋ถ๋ฅํ๊ธฐ ์ ๊น์ง ํ ์คํธ๊ฐ ๊นจ์ง๋ค(์๋). | |
| NON_OUTPUT_DEPENDENCIES = frozenset( | |
| {"click", "tabulate", "rich", "pydantic-settings", "poc-bundle", "packaging"} | |
| ) | |
| DEPENDENCY_LOCK_SCHEME = "direct-runtime/v1" | |
| #: config_dir ์์์ ์ถ๋ ฅ์ ์ํฅ์ ์ฃผ๋ ๊ฒฝ๋ก(dirty ๋ฒ์). scripts/ยทinsights/ยทREADME ๋ ๋บ๋ค(๊ฒํ F12ยทm3). | |
| CONFIG_DIRTY_ENTRIES = ( | |
| "project.yaml", | |
| "prompts", | |
| "pipelines", | |
| "resources", | |
| "dataset/datasets.lock.yaml", | |
| ) | |
| GitRun = Callable[..., Any] | |
| def _marker_env() -> dict[str, str]: | |
| return {"extra": ""} | |
| def direct_requirements(distribution: str = ENGINE_DISTRIBUTION) -> list[str]: | |
| """๋ฐฐํฌ์ ์ง์ `Requires-Dist` ์ค ํ์ฌ ํ๊ฒฝ์์ ์ผ์ง๋ ๊ฒ(extra ์์)์ ์ ๊ทํ ์ด๋ฆ(์ ๋ ฌ).""" | |
| names: set[str] = set() | |
| for raw in metadata.requires(distribution) or []: | |
| try: | |
| req = Requirement(raw) | |
| except InvalidRequirement as exc: | |
| raise ValueError(f"{distribution}: unparsable requirement {raw!r}: {exc}") from exc | |
| if req.marker is not None and not req.marker.evaluate(_marker_env()): | |
| continue | |
| names.add(canonicalize_name(req.name)) | |
| return sorted(names) | |
| def direct_runtime_dependencies() -> dict[str, str]: | |
| """`dependency_lock.packages` โ ์ถ๋ ฅ ๊ฒฝ๋ก์ ์๋ ์ง์ ์์กด์ ์ค์น ๋ฒ์ .""" | |
| return { | |
| name: metadata.version(name) | |
| for name in direct_requirements() | |
| if name not in NON_OUTPUT_DEPENDENCIES | |
| } | |
| def dependency_lock() -> dict[str, Any]: | |
| return { | |
| "scheme": DEPENDENCY_LOCK_SCHEME, | |
| "root": ENGINE_DISTRIBUTION, | |
| "packages": direct_runtime_dependencies(), | |
| } | |
| def installed_closure(distribution: str = ENGINE_DISTRIBUTION) -> dict[str, str | None]: | |
| """์์ง์์ ๋ฟ๋ ์ค์น ํํฌ `{์ด๋ฆ: ๋ฒ์ }` โ provenance ์ ์ฉ(ํ๊ฒฝ ์ฌ๊ตฌ์ฑยท๋๋ฒ๊ทธ). | |
| ์ผ์ง ์๊ตฌ์ธ๋ฐ ์ค์น๋ผ ์์ง ์์ผ๋ฉด ๋ฒ์ None ์ผ๋ก ๋จ๊ธด๋ค(์จ๊ธฐ์ง ์๋๋ค). | |
| """ | |
| seen: dict[str, str | None] = {} | |
| queue: list[str] = [str(canonicalize_name(distribution))] | |
| while queue: | |
| name = queue.pop() | |
| if name in seen: | |
| continue | |
| try: | |
| seen[name] = metadata.version(name) | |
| except metadata.PackageNotFoundError: | |
| seen[name] = None | |
| continue | |
| queue.extend(n for n in direct_requirements(name) if n not in seen) | |
| return dict(sorted(seen.items())) | |
| def runner_version() -> dict[str, str]: | |
| """`{"package", "version", "python": "<major>.<minor>"}` โ ํจ์น ๋ฒ์ ์ ๋ฃ์ง ์๋๋ค.""" | |
| return { | |
| "package": ENGINE_DISTRIBUTION, | |
| "version": __version__, | |
| "python": f"{sys.version_info.major}.{sys.version_info.minor}", | |
| } | |
| def engine_src_dir() -> Path: | |
| """์์ง ์์ค ๋ฃจํธ(`packages/solar-eval-python/src`) โ `solar_eval.__file__` ๊ธฐ์ค.""" | |
| return Path(solar_eval.__file__).resolve().parent.parent | |
| def engine_repo(start: Path | None = None) -> Path | None: | |
| """์์ง ์ฝ๋๊ฐ ๋ git ๋ ํฌ โ `start`(๊ธฐ๋ณธ ์์ง ์์ค)์์ ์๋ก `.git` ์ ์ฐพ๋๋ค. | |
| `cfg.repo_root` ๋ฅผ ์ฐ์ง ์๋๋ค: `--projects-dir` ๋ฉด None ์ด ๋๋ค(๊ฒํ F13). ์ํฌํธ๋ฆฌ์ `.git` ์ | |
| ํ์ผ์ด๋ผ ์กด์ฌ๋ง ๋ณธ๋ค. | |
| """ | |
| here = (start or engine_src_dir()).resolve() | |
| for candidate in (here, *here.parents): | |
| if (candidate / ".git").exists(): | |
| return candidate | |
| return None | |
| def dirty_scope( | |
| repo: Path, config_dir: Path | None, repo_managed: bool, src_dir: Path | None = None | |
| ) -> list[str]: | |
| """dirty ํ์ ๋ฒ์(๋ ํฌ ์๋ POSIX, ์ ๋ ฌ). ๋ ํฌ ๋ฐ ๊ฒฝ๋ก๋ ๋ฃ์ง ์๋๋ค.""" | |
| src = (src_dir or engine_src_dir()).resolve() | |
| candidates = [src, src.parent / "pyproject.toml", repo / "uv.lock"] | |
| if repo_managed and config_dir is not None: | |
| candidates += [Path(config_dir).resolve() / entry for entry in CONFIG_DIRTY_ENTRIES] | |
| root = repo.resolve() | |
| scope = set() | |
| for path in candidates: | |
| try: | |
| scope.add(path.resolve().relative_to(root).as_posix()) | |
| except ValueError: | |
| logger.warning("dirty scope path outside the engine repo, skipped: %s", path) | |
| return sorted(scope) | |
| def _git(run: GitRun, repo: Path, *args: str) -> str | None: | |
| """git ํ ๋ฒ. ์คํจ(git ์์ยท๋ ํฌ ์๋ยท๋น์ ์ ์ข ๋ฃ)๋ None โ ํธ์ถ์๊ฐ null ๋ก ๊ธฐ๋กํ๋ค.""" | |
| try: | |
| result = run(["git", "-C", str(repo), *args], capture_output=True, text=True, check=True) | |
| except (OSError, subprocess.CalledProcessError) as exc: | |
| logger.warning("git %s failed in %s: %s", " ".join(args[:2]), repo, exc) | |
| return None | |
| return result.stdout | |
| def engine_state( | |
| config_dir: Path | None, | |
| repo_managed: bool, | |
| *, | |
| repo: Path | None = None, | |
| src_dir: Path | None = None, | |
| roots: ImplRoots | None = None, | |
| run: GitRun = subprocess.run, | |
| ) -> dict[str, Any]: | |
| """`provenance.engine = {commit, tree_sha256, dirty, dirty_scope}` (D8). | |
| `repo` ๋ฅผ ์ ์ฃผ๋ฉด `engine_repo()`. ๋ ํฌ๋ฅผ ๋ชป ์ฐพ๊ฑฐ๋ git ์ด ์คํจํ๋ฉด `commit`ยท`dirty` ๋ null, | |
| `tree_sha256` ์ git ์์ด๋ ๊ณ์ฐ๋๋ค. | |
| """ | |
| repo = repo if repo is not None else engine_repo(src_dir) | |
| tree = engine_tree_sha256(roots) | |
| if repo is None: | |
| return {"commit": None, "tree_sha256": tree, "dirty": None, "dirty_scope": []} | |
| scope = dirty_scope(repo, config_dir, repo_managed, src_dir) | |
| head = _git(run, repo, "rev-parse", "HEAD") | |
| # ๋น ๋ฒ์๋ก status ๋ฅผ ๋ถ๋ฅด๋ฉด ๋ ํฌ ์ ์ฒด๋ฅผ ๋ณธ๋ค โ ๋ฒ์๊ฐ ์์ผ๋ฉด ํ์ ํ์ง ์๋๋ค(null). | |
| # `--literal-pathspecs`: config_dir ์ด๋ฆ์ `[`ยท`*`ยท`?` ๋ฅผ glob ์ผ๋ก ์ฝ์ง ์๊ฒ(๋ฒ์ ๋ฐ ๋ณ๊ฒฝ์ด dirty ๋ก). | |
| status = ( | |
| _git( | |
| run, | |
| repo, | |
| "--literal-pathspecs", | |
| "status", | |
| "--porcelain", | |
| "--untracked-files=normal", | |
| "--", | |
| *scope, | |
| ) | |
| if scope | |
| else None | |
| ) | |
| return { | |
| "commit": head.strip() if head else None, | |
| "tree_sha256": tree, | |
| "dirty": None if status is None else bool(status.strip()), | |
| "dirty_scope": scope, | |
| } | |
| def uv_lock_sha256(repo: Path | None) -> str | None: | |
| """๋ ํฌ `uv.lock` ์ sha256 โ provenance ์ ์ฉ(๋ชจ๋ ธ๋ ํฌ lock ์ ๋ฌด๊ดํ ๋ฉค๋ฒ๋ก๋ ๋ฐ๋๋ค).""" | |
| if repo is None: | |
| return None | |
| lock = repo / "uv.lock" | |
| return content_sha256(lock) if lock.is_file() else None | |