Download ocr-tech/tests/local_artifacts.py from prosperious/ocr-projects-archive: direct link, hf CLI and curl.
- Browser
- Download file 3.17 kB
-
https://huggingface.co/prosperious/ocr-projects-archive/resolve/main/ocr-tech/tests/local_artifacts.py
- Command line
-
hf download hf://prosperious/ocr-projects-archive/ocr-tech/tests/local_artifacts.py
-
curl -L -o local_artifacts.py https://huggingface.co/prosperious/ocr-projects-archive/resolve/main/ocr-tech/tests/local_artifacts.py
3.17 kB
| """Skip guards for tests that read machine-local artifacts that are not in git. | |
| Some tests audit real packs, fonts, runs or sibling checkouts that live only on a workstation | |
| (ignored `data/`, `outputs/`, `runs/`, `/tmp`, `../nepal-ttf2utf`). On a clean clone those | |
| inputs do not exist, so the tests skip with the exact missing paths instead of failing. | |
| The guards refuse to cover committed files: a path inside the repository must be local-only | |
| (ignored `data/`, `outputs/`, `runs/` or `spaces/limbu-ocr/models/`, and not tracked by git). A missing tracked file | |
| still fails the test. | |
| """ | |
| from __future__ import annotations | |
| import re | |
| import subprocess | |
| from pathlib import Path | |
| import pytest | |
| ROOT = Path(__file__).resolve().parents[1] | |
| LOCAL_ONLY_PREFIXES = ("data/", "outputs/", "runs/", "spaces/limbu-ocr/models/") | |
| TRACKED_DATA_PREFIXES = ("data/dictionaries/", "data/mappings/") | |
| _MISSING_PATH_PATTERNS = ( | |
| re.compile(r"path does not exist for [^:]+: (?P<path>.+)$"), | |
| re.compile(r"does not exist: (?P<path>.+)$"), | |
| re.compile(r"is not a directory: (?P<path>.+)$"), | |
| re.compile(r"missing local path: .*?\.path=(?P<path>.+)$"), | |
| ) | |
| def _is_tracked(relative: str) -> bool: | |
| if not (ROOT / ".git").exists(): | |
| return False | |
| result = subprocess.run( | |
| ["git", "-C", str(ROOT), "ls-files", "--error-unmatch", relative], | |
| capture_output=True, | |
| text=True, | |
| check=False, | |
| ) | |
| return result.returncode == 0 | |
| def is_local_only(path: str | Path) -> bool: | |
| """True when ``path`` is outside the repo, or an untracked path under data/, outputs/ or runs/.""" | |
| candidate = Path(path) | |
| if not candidate.is_absolute(): | |
| candidate = ROOT / candidate | |
| try: | |
| relative = candidate.resolve().relative_to(ROOT).as_posix() | |
| except ValueError: | |
| return True | |
| if relative.startswith(TRACKED_DATA_PREFIXES) or not relative.startswith(LOCAL_ONLY_PREFIXES): | |
| return False | |
| return not _is_tracked(relative) | |
| def require_local_artifacts(*paths: str | Path, reason: str) -> None: | |
| """Skip (module or test) when any of ``paths`` is missing; each path must be local-only.""" | |
| committed = [str(path) for path in paths if not is_local_only(path)] | |
| if committed: | |
| raise ValueError(f"require_local_artifacts is only for local-only inputs; committed paths: {committed}") | |
| missing = [str(path) for path in paths if not Path(path).exists()] | |
| if missing: | |
| pytest.skip(f"{reason}; local-only inputs missing: {missing}", allow_module_level=True) | |
| def skip_if_only_local_artifacts_missing(errors: list[str], *, reason: str) -> None: | |
| """Skip when every validator error is a missing local-only path; otherwise return and let the test fail.""" | |
| if not errors: | |
| return | |
| missing: list[str] = [] | |
| for error in errors: | |
| match = next((m for m in (p.search(str(error)) for p in _MISSING_PATH_PATTERNS) if m), None) | |
| if match is None or not is_local_only(match.group("path").strip()): | |
| return | |
| missing.append(match.group("path").strip()) | |
| pytest.skip(f"{reason}; {len(missing)} local-only inputs missing, e.g. {missing[:3]}") | |