feat: initial release of hf-release v0.1.0
Browse files- .env.example +3 -0
- .gitignore +21 -0
- README.md +98 -0
- pyproject.toml +63 -0
- src/hf_release/__init__.py +3 -0
- src/hf_release/__main__.py +6 -0
- src/hf_release/cli.py +47 -0
- src/hf_release/commands/__init__.py +0 -0
- src/hf_release/commands/release.py +222 -0
- src/hf_release/config/__init__.py +0 -0
- src/hf_release/config/auth.py +45 -0
- src/hf_release/core/__init__.py +0 -0
- src/hf_release/core/github_client.py +125 -0
- src/hf_release/core/hf_client.py +124 -0
- src/hf_release/core/model_card.py +62 -0
- src/hf_release/core/release_notes.py +60 -0
- src/hf_release/py.typed +0 -0
- src/hf_release/utils/__init__.py +0 -0
- src/hf_release/utils/display.py +101 -0
- src/hf_release/utils/exceptions.py +55 -0
- src/hf_release/utils/fs.py +49 -0
- tests/__init__.py +0 -0
- tests/integration/__init__.py +0 -0
- tests/unit/__init__.py +0 -0
- tests/unit/test_fs.py +28 -0
.env.example
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copy to .env and fill in your tokens (never commit .env)
|
| 2 |
+
HF_TOKEN=hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
| 3 |
+
GITHUB_TOKEN=github_pat_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
.gitignore
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*.egg-info/
|
| 5 |
+
dist/
|
| 6 |
+
build/
|
| 7 |
+
.venv/
|
| 8 |
+
venv/
|
| 9 |
+
.env
|
| 10 |
+
|
| 11 |
+
# Test / coverage
|
| 12 |
+
.pytest_cache/
|
| 13 |
+
.coverage
|
| 14 |
+
htmlcov/
|
| 15 |
+
|
| 16 |
+
# Mypy
|
| 17 |
+
.mypy_cache/
|
| 18 |
+
|
| 19 |
+
# OS
|
| 20 |
+
.DS_Store
|
| 21 |
+
Thumbs.db
|
README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# hf-release
|
| 2 |
+
|
| 3 |
+
> Push model weights to **Hugging Face Hub** and create a **GitHub Release** in one atomic command.
|
| 4 |
+
|
| 5 |
+
## The Problem
|
| 6 |
+
|
| 7 |
+
Today, releasing an ML model requires four separate manual steps:
|
| 8 |
+
|
| 9 |
+
1. `push_to_hub()` — upload weights to HF Hub
|
| 10 |
+
2. `git tag` — version the commit
|
| 11 |
+
3. GitHub UI — create a release
|
| 12 |
+
4. Edit README — update the model card
|
| 13 |
+
|
| 14 |
+
No existing tool treats this as a single operation. `hf-release` does.
|
| 15 |
+
|
| 16 |
+
## Installation
|
| 17 |
+
|
| 18 |
+
```bash
|
| 19 |
+
pip install hf-release
|
| 20 |
+
```
|
| 21 |
+
|
| 22 |
+
## Quick Start
|
| 23 |
+
|
| 24 |
+
```bash
|
| 25 |
+
export HF_TOKEN=hf_...
|
| 26 |
+
export GITHUB_TOKEN=ghp_...
|
| 27 |
+
|
| 28 |
+
hf-release release \
|
| 29 |
+
--model ./my-model \
|
| 30 |
+
--version v1.2.0 \
|
| 31 |
+
--hf-repo username/my-model \
|
| 32 |
+
--gh-repo username/my-model-repo
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
This single command will:
|
| 36 |
+
|
| 37 |
+
- Upload all model files to HF Hub on the `main` branch
|
| 38 |
+
- Create a git tag `v1.2.0` on HF Hub
|
| 39 |
+
- Sync the local `README.md` to the HF model card (with version tag injected)
|
| 40 |
+
- Create an annotated git tag on GitHub
|
| 41 |
+
- Create a GitHub Release with auto-generated release notes (grouped by PR labels)
|
| 42 |
+
- Append an "ML Artifacts" table to the release notes linking both platforms
|
| 43 |
+
|
| 44 |
+
## All Options
|
| 45 |
+
|
| 46 |
+
```
|
| 47 |
+
--model PATH Local model directory [required]
|
| 48 |
+
--version TEXT Version tag, e.g. v1.2.0 [required]
|
| 49 |
+
--hf-repo TEXT HF Hub repo ID (owner/name) [required unless --skip-hf]
|
| 50 |
+
--gh-repo TEXT GitHub repo ID (owner/name) [required unless --skip-gh]
|
| 51 |
+
|
| 52 |
+
--hf-token TEXT HF write token [env: HF_TOKEN]
|
| 53 |
+
--gh-token TEXT GitHub PAT [env: GITHUB_TOKEN]
|
| 54 |
+
|
| 55 |
+
--branch TEXT HF Hub target branch [default: main]
|
| 56 |
+
--commit-msg TEXT Custom HF commit message
|
| 57 |
+
--ignore TEXT Glob pattern to exclude from upload [repeatable]
|
| 58 |
+
--create-repo Create HF repo if it does not exist
|
| 59 |
+
|
| 60 |
+
--release-name TEXT GitHub release display name
|
| 61 |
+
--previous-tag TEXT Starting tag for release note diff
|
| 62 |
+
--draft Create GitHub release as draft
|
| 63 |
+
--prerelease Mark as pre-release
|
| 64 |
+
--notes-file PATH Use a local Markdown file as release notes
|
| 65 |
+
|
| 66 |
+
--skip-card-sync Do not sync model card README
|
| 67 |
+
--tags TEXT Extra tags to inject into card metadata [repeatable]
|
| 68 |
+
|
| 69 |
+
--skip-hf Skip HF Hub push entirely
|
| 70 |
+
--skip-gh Skip GitHub release entirely
|
| 71 |
+
--skip-tag Skip tag creation on both platforms
|
| 72 |
+
--dry-run Show plan, make no API calls
|
| 73 |
+
--yes / -y Skip confirmation prompt
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
## Environment Variables
|
| 77 |
+
|
| 78 |
+
| Variable | Description |
|
| 79 |
+
|----------|-------------|
|
| 80 |
+
| `HF_TOKEN` | Hugging Face write token |
|
| 81 |
+
| `GITHUB_TOKEN` | GitHub personal access token (needs `repo` scope) |
|
| 82 |
+
|
| 83 |
+
## Exit Codes
|
| 84 |
+
|
| 85 |
+
| Code | Meaning |
|
| 86 |
+
|------|---------|
|
| 87 |
+
| 0 | Success |
|
| 88 |
+
| 2 | Authentication error |
|
| 89 |
+
| 3 | Repo not found |
|
| 90 |
+
| 4 | Tag conflict |
|
| 91 |
+
| 5 | Model card sync failure |
|
| 92 |
+
| 6 | Upload failure |
|
| 93 |
+
| 7 | GitHub release creation failure |
|
| 94 |
+
| 8 | Input validation error |
|
| 95 |
+
|
| 96 |
+
## License
|
| 97 |
+
|
| 98 |
+
MIT
|
pyproject.toml
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[build-system]
|
| 2 |
+
requires = ["hatchling"]
|
| 3 |
+
build-backend = "hatchling.build"
|
| 4 |
+
|
| 5 |
+
[project]
|
| 6 |
+
name = "hf-release"
|
| 7 |
+
version = "0.1.0"
|
| 8 |
+
description = "Push model weights to HF Hub and create GitHub Releases atomically"
|
| 9 |
+
readme = "README.md"
|
| 10 |
+
license = { text = "MIT" }
|
| 11 |
+
requires-python = ">=3.10"
|
| 12 |
+
keywords = ["huggingface", "github", "mlops", "model-release", "cli"]
|
| 13 |
+
classifiers = [
|
| 14 |
+
"Development Status :: 3 - Alpha",
|
| 15 |
+
"Environment :: Console",
|
| 16 |
+
"Intended Audience :: Developers",
|
| 17 |
+
"Intended Audience :: Science/Research",
|
| 18 |
+
"License :: OSI Approved :: MIT License",
|
| 19 |
+
"Programming Language :: Python :: 3",
|
| 20 |
+
"Programming Language :: Python :: 3.10",
|
| 21 |
+
"Programming Language :: Python :: 3.11",
|
| 22 |
+
"Programming Language :: Python :: 3.12",
|
| 23 |
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
| 24 |
+
]
|
| 25 |
+
dependencies = [
|
| 26 |
+
"typer[all]>=0.12",
|
| 27 |
+
"huggingface_hub>=0.23",
|
| 28 |
+
"PyGithub>=2.3",
|
| 29 |
+
"rich>=13.7",
|
| 30 |
+
"pydantic>=2.0",
|
| 31 |
+
"pydantic-settings>=2.0",
|
| 32 |
+
"semver>=3.0",
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
[project.optional-dependencies]
|
| 36 |
+
dev = [
|
| 37 |
+
"pytest>=8.0",
|
| 38 |
+
"pytest-mock>=3.14",
|
| 39 |
+
"ruff>=0.4",
|
| 40 |
+
"mypy>=1.10",
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
[project.scripts]
|
| 44 |
+
hf-release = "hf_release.cli:app"
|
| 45 |
+
|
| 46 |
+
[project.urls]
|
| 47 |
+
Homepage = "https://github.com/username/hf-release"
|
| 48 |
+
Repository = "https://github.com/username/hf-release"
|
| 49 |
+
Issues = "https://github.com/username/hf-release/issues"
|
| 50 |
+
|
| 51 |
+
[tool.hatch.build.targets.wheel]
|
| 52 |
+
packages = ["src/hf_release"]
|
| 53 |
+
|
| 54 |
+
[tool.ruff]
|
| 55 |
+
line-length = 100
|
| 56 |
+
target-version = "py310"
|
| 57 |
+
|
| 58 |
+
[tool.ruff.lint]
|
| 59 |
+
select = ["E", "F", "I", "UP"]
|
| 60 |
+
|
| 61 |
+
[tool.mypy]
|
| 62 |
+
python_version = "3.10"
|
| 63 |
+
strict = true
|
src/hf_release/__init__.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""hf-release: Dual-publish ML models to Hugging Face Hub and GitHub."""
|
| 2 |
+
|
| 3 |
+
__version__ = "0.1.0"
|
src/hf_release/__main__.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Allow `python -m hf_release` invocation."""
|
| 2 |
+
|
| 3 |
+
from .cli import app
|
| 4 |
+
|
| 5 |
+
if __name__ == "__main__":
|
| 6 |
+
app()
|
src/hf_release/cli.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Root Typer application for hf-release."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import typer
|
| 6 |
+
from rich.traceback import install as install_rich_tb
|
| 7 |
+
|
| 8 |
+
from . import __version__
|
| 9 |
+
from .commands.release import app as release_app
|
| 10 |
+
from .utils.display import console, print_error
|
| 11 |
+
from .utils.exceptions import HFReleaseError
|
| 12 |
+
|
| 13 |
+
# Never leak tokens in tracebacks
|
| 14 |
+
install_rich_tb(show_locals=False)
|
| 15 |
+
|
| 16 |
+
app = typer.Typer(
|
| 17 |
+
name="hf-release",
|
| 18 |
+
rich_markup_mode="rich",
|
| 19 |
+
pretty_exceptions_show_locals=False,
|
| 20 |
+
add_completion=True,
|
| 21 |
+
help=(
|
| 22 |
+
"[bold]hf-release[/bold] — publish model weights to [cyan]Hugging Face Hub[/cyan] "
|
| 23 |
+
"and create a [green]GitHub Release[/green] in one atomic command."
|
| 24 |
+
),
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
app.add_typer(release_app, name="release")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def _version_callback(value: bool) -> None:
|
| 31 |
+
if value:
|
| 32 |
+
typer.echo(f"hf-release {__version__}")
|
| 33 |
+
raise typer.Exit()
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
@app.callback()
|
| 37 |
+
def main(
|
| 38 |
+
version: bool = typer.Option(
|
| 39 |
+
False,
|
| 40 |
+
"--version",
|
| 41 |
+
"-V",
|
| 42 |
+
callback=_version_callback,
|
| 43 |
+
is_eager=True,
|
| 44 |
+
help="Show version and exit.",
|
| 45 |
+
),
|
| 46 |
+
) -> None:
|
| 47 |
+
pass
|
src/hf_release/commands/__init__.py
ADDED
|
File without changes
|
src/hf_release/commands/release.py
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Primary `release` command — orchestrates the full dual-publish workflow."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from typing import Annotated, Optional
|
| 7 |
+
|
| 8 |
+
import typer
|
| 9 |
+
|
| 10 |
+
from ..config.auth import resolve_gh_token, resolve_hf_token
|
| 11 |
+
from ..core import model_card as card_module
|
| 12 |
+
from ..core.github_client import GHClient
|
| 13 |
+
from ..core.hf_client import HFClient
|
| 14 |
+
from ..core.release_notes import build_notes
|
| 15 |
+
from ..utils.display import (
|
| 16 |
+
ReleaseArtifact,
|
| 17 |
+
confirm_plan,
|
| 18 |
+
console,
|
| 19 |
+
make_progress,
|
| 20 |
+
print_dry_run_panel,
|
| 21 |
+
print_partial_success,
|
| 22 |
+
print_summary_table,
|
| 23 |
+
)
|
| 24 |
+
from ..utils.exceptions import HFReleaseError
|
| 25 |
+
from ..utils.fs import validate_model_dir, validate_repo_id, validate_version
|
| 26 |
+
|
| 27 |
+
app = typer.Typer(help="Publish a model release to HF Hub and GitHub simultaneously.")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
@app.callback(invoke_without_command=True)
|
| 31 |
+
def release( # noqa: PLR0913 — many CLI params are expected
|
| 32 |
+
model: Annotated[Path, typer.Option("--model", help="Local model directory.")] = Path("."),
|
| 33 |
+
version: Annotated[str, typer.Option("--version", "-v", help="Release version, e.g. v1.2.0.")] = "",
|
| 34 |
+
hf_repo: Annotated[str, typer.Option("--hf-repo", help="HF Hub repo ID (owner/name).")] = "",
|
| 35 |
+
gh_repo: Annotated[str, typer.Option("--gh-repo", help="GitHub repo ID (owner/name).")] = "",
|
| 36 |
+
# Auth
|
| 37 |
+
hf_token: Annotated[Optional[str], typer.Option("--hf-token", envvar="HF_TOKEN", help="HF write token.", show_default=False)] = None,
|
| 38 |
+
gh_token: Annotated[Optional[str], typer.Option("--gh-token", envvar="GITHUB_TOKEN", help="GitHub PAT.", show_default=False)] = None,
|
| 39 |
+
# HF options
|
| 40 |
+
branch: Annotated[str, typer.Option("--branch", help="HF Hub target branch.")] = "main",
|
| 41 |
+
commit_msg: Annotated[Optional[str], typer.Option("--commit-msg", help="Custom HF commit message.")] = None,
|
| 42 |
+
ignore: Annotated[Optional[list[str]], typer.Option("--ignore", help="Glob patterns to exclude from upload.")] = None,
|
| 43 |
+
create_repo: Annotated[bool, typer.Option("--create-repo", help="Create HF repo if it does not exist.")] = False,
|
| 44 |
+
# GH options
|
| 45 |
+
release_name: Annotated[Optional[str], typer.Option("--release-name", help="GitHub release display name.")] = None,
|
| 46 |
+
previous_tag: Annotated[Optional[str], typer.Option("--previous-tag", help="Previous tag for release note range.")] = None,
|
| 47 |
+
draft: Annotated[bool, typer.Option("--draft", help="Create GitHub release as draft.")] = False,
|
| 48 |
+
prerelease: Annotated[bool, typer.Option("--prerelease", help="Mark GitHub release as pre-release.")] = False,
|
| 49 |
+
notes_file: Annotated[Optional[str], typer.Option("--notes-file", help="Local Markdown file to use as release notes.")] = None,
|
| 50 |
+
# Card options
|
| 51 |
+
skip_card_sync: Annotated[bool, typer.Option("--skip-card-sync", help="Do not sync model card.")] = False,
|
| 52 |
+
tags: Annotated[Optional[list[str]], typer.Option("--tags", help="Extra tags to inject into model card metadata.")] = None,
|
| 53 |
+
# Control flow
|
| 54 |
+
skip_hf: Annotated[bool, typer.Option("--skip-hf", help="Skip HF Hub push entirely.")] = False,
|
| 55 |
+
skip_gh: Annotated[bool, typer.Option("--skip-gh", help="Skip GitHub release entirely.")] = False,
|
| 56 |
+
skip_tag: Annotated[bool, typer.Option("--skip-tag", help="Skip tag creation on both platforms.")] = False,
|
| 57 |
+
dry_run: Annotated[bool, typer.Option("--dry-run", help="Show plan, make no API calls.")] = False,
|
| 58 |
+
yes: Annotated[bool, typer.Option("--yes", "-y", help="Skip confirmation prompt.")] = False,
|
| 59 |
+
) -> None:
|
| 60 |
+
# ------------------------------------------------------------------
|
| 61 |
+
# 1. Validate inputs
|
| 62 |
+
# ------------------------------------------------------------------
|
| 63 |
+
if not version:
|
| 64 |
+
typer.echo("Error: --version is required.", err=True)
|
| 65 |
+
raise typer.Exit(8)
|
| 66 |
+
if not hf_repo and not skip_hf:
|
| 67 |
+
typer.echo("Error: --hf-repo is required (or pass --skip-hf).", err=True)
|
| 68 |
+
raise typer.Exit(8)
|
| 69 |
+
if not gh_repo and not skip_gh:
|
| 70 |
+
typer.echo("Error: --gh-repo is required (or pass --skip-gh).", err=True)
|
| 71 |
+
raise typer.Exit(8)
|
| 72 |
+
|
| 73 |
+
version = validate_version(version)
|
| 74 |
+
if hf_repo:
|
| 75 |
+
hf_repo = validate_repo_id(hf_repo, "HF repo")
|
| 76 |
+
if gh_repo:
|
| 77 |
+
gh_repo = validate_repo_id(gh_repo, "GitHub repo")
|
| 78 |
+
if not skip_hf:
|
| 79 |
+
validate_model_dir(model)
|
| 80 |
+
|
| 81 |
+
# ------------------------------------------------------------------
|
| 82 |
+
# 2. Dry-run plan
|
| 83 |
+
# ------------------------------------------------------------------
|
| 84 |
+
plan_steps: list[str] = []
|
| 85 |
+
if not skip_hf:
|
| 86 |
+
plan_steps.append(f"Push {model} → HF Hub [{hf_repo}] (branch: {branch})")
|
| 87 |
+
if not skip_tag:
|
| 88 |
+
plan_steps.append(f"Create HF tag: {version}")
|
| 89 |
+
if not skip_card_sync:
|
| 90 |
+
plan_steps.append(f"Sync model card README → HF Hub [{hf_repo}]")
|
| 91 |
+
if not skip_gh:
|
| 92 |
+
plan_steps.append(f"Create GitHub release [{gh_repo}] tag: {version}")
|
| 93 |
+
|
| 94 |
+
if dry_run:
|
| 95 |
+
print_dry_run_panel(plan_steps)
|
| 96 |
+
raise typer.Exit(0)
|
| 97 |
+
|
| 98 |
+
# ------------------------------------------------------------------
|
| 99 |
+
# 3. Confirmation
|
| 100 |
+
# ------------------------------------------------------------------
|
| 101 |
+
if not yes and not confirm_plan(plan_steps):
|
| 102 |
+
console.print("[yellow]Aborted.[/yellow]")
|
| 103 |
+
raise typer.Exit(0)
|
| 104 |
+
|
| 105 |
+
# ------------------------------------------------------------------
|
| 106 |
+
# 4. Resolve tokens
|
| 107 |
+
# ------------------------------------------------------------------
|
| 108 |
+
hf_tok = resolve_hf_token(hf_token) if not skip_hf else None
|
| 109 |
+
gh_tok = resolve_gh_token(gh_token) if not skip_gh else None
|
| 110 |
+
|
| 111 |
+
hf = HFClient(hf_tok) if hf_tok else None
|
| 112 |
+
gh = GHClient(gh_tok) if gh_tok else None
|
| 113 |
+
|
| 114 |
+
# ------------------------------------------------------------------
|
| 115 |
+
# 5. Execute steps
|
| 116 |
+
# ------------------------------------------------------------------
|
| 117 |
+
completed: list[str] = []
|
| 118 |
+
failed: list[str] = []
|
| 119 |
+
artifact = ReleaseArtifact(
|
| 120 |
+
version=version,
|
| 121 |
+
hf_repo=hf_repo or None,
|
| 122 |
+
gh_repo=gh_repo or None,
|
| 123 |
+
hf_url=None,
|
| 124 |
+
gh_url=None,
|
| 125 |
+
hf_tag=None,
|
| 126 |
+
gh_tag=None,
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
with make_progress() as progress:
|
| 130 |
+
# --- HF push ---
|
| 131 |
+
if hf and not skip_hf:
|
| 132 |
+
task = progress.add_task("Uploading model to HF Hub…", total=None)
|
| 133 |
+
try:
|
| 134 |
+
hf.ensure_repo_exists(hf_repo, create=create_repo)
|
| 135 |
+
commit_url = hf.push_model(
|
| 136 |
+
local_path=model,
|
| 137 |
+
repo_id=hf_repo,
|
| 138 |
+
branch=branch,
|
| 139 |
+
commit_message=commit_msg,
|
| 140 |
+
ignore_patterns=ignore or [],
|
| 141 |
+
)
|
| 142 |
+
artifact.hf_url = f"https://huggingface.co/{hf_repo}"
|
| 143 |
+
completed.append("HF Hub upload")
|
| 144 |
+
progress.update(task, description="[green]HF upload done[/green]", completed=1, total=1)
|
| 145 |
+
except HFReleaseError as exc:
|
| 146 |
+
failed.append(f"HF Hub upload: {exc}")
|
| 147 |
+
progress.update(task, description="[red]HF upload failed[/red]", completed=1, total=1)
|
| 148 |
+
|
| 149 |
+
# --- HF tag ---
|
| 150 |
+
if hf and not skip_hf and not skip_tag and "HF Hub upload" in completed:
|
| 151 |
+
task = progress.add_task(f"Creating HF tag {version}…", total=None)
|
| 152 |
+
try:
|
| 153 |
+
hf.create_tag(hf_repo, version)
|
| 154 |
+
artifact.hf_tag = version
|
| 155 |
+
artifact.hf_url = f"https://huggingface.co/{hf_repo}/tree/{version}"
|
| 156 |
+
completed.append("HF tag")
|
| 157 |
+
progress.update(task, description="[green]HF tag done[/green]", completed=1, total=1)
|
| 158 |
+
except HFReleaseError as exc:
|
| 159 |
+
failed.append(f"HF tag: {exc}")
|
| 160 |
+
progress.update(task, description="[red]HF tag failed[/red]", completed=1, total=1)
|
| 161 |
+
|
| 162 |
+
# --- Model card sync ---
|
| 163 |
+
if hf and not skip_hf and not skip_card_sync and "HF Hub upload" in completed:
|
| 164 |
+
task = progress.add_task("Syncing model card…", total=None)
|
| 165 |
+
try:
|
| 166 |
+
synced = card_module.sync_card(
|
| 167 |
+
model_dir=model,
|
| 168 |
+
hf_client=hf,
|
| 169 |
+
hf_repo=hf_repo,
|
| 170 |
+
version=version,
|
| 171 |
+
extra_tags=tags or [],
|
| 172 |
+
)
|
| 173 |
+
if synced:
|
| 174 |
+
completed.append("Model card sync")
|
| 175 |
+
else:
|
| 176 |
+
completed.append("Model card sync (skipped — no README found)")
|
| 177 |
+
progress.update(task, description="[green]Card sync done[/green]", completed=1, total=1)
|
| 178 |
+
except HFReleaseError as exc:
|
| 179 |
+
failed.append(f"Model card sync: {exc}")
|
| 180 |
+
progress.update(task, description="[red]Card sync failed[/red]", completed=1, total=1)
|
| 181 |
+
|
| 182 |
+
# --- GitHub release ---
|
| 183 |
+
if gh and not skip_gh:
|
| 184 |
+
task = progress.add_task("Creating GitHub release…", total=None)
|
| 185 |
+
try:
|
| 186 |
+
gh_repo_obj = gh.get_repo(gh_repo)
|
| 187 |
+
|
| 188 |
+
if not skip_tag:
|
| 189 |
+
gh.ensure_tag(gh_repo_obj, version)
|
| 190 |
+
artifact.gh_tag = version
|
| 191 |
+
|
| 192 |
+
body = build_notes(
|
| 193 |
+
version=version,
|
| 194 |
+
hf_repo=hf_repo or None,
|
| 195 |
+
gh_client=gh,
|
| 196 |
+
gh_repo_obj=gh_repo_obj,
|
| 197 |
+
previous_tag=previous_tag,
|
| 198 |
+
notes_file=notes_file,
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
gh_url = gh.create_release(
|
| 202 |
+
repo=gh_repo_obj,
|
| 203 |
+
tag_name=version,
|
| 204 |
+
name=release_name or version,
|
| 205 |
+
body=body,
|
| 206 |
+
draft=draft,
|
| 207 |
+
prerelease=prerelease,
|
| 208 |
+
)
|
| 209 |
+
artifact.gh_url = gh_url
|
| 210 |
+
completed.append("GitHub release")
|
| 211 |
+
progress.update(task, description="[green]GitHub release done[/green]", completed=1, total=1)
|
| 212 |
+
except HFReleaseError as exc:
|
| 213 |
+
failed.append(f"GitHub release: {exc}")
|
| 214 |
+
progress.update(task, description="[red]GitHub release failed[/red]", completed=1, total=1)
|
| 215 |
+
|
| 216 |
+
# ------------------------------------------------------------------
|
| 217 |
+
# 6. Summary
|
| 218 |
+
# ------------------------------------------------------------------
|
| 219 |
+
if failed:
|
| 220 |
+
print_partial_success(completed, failed)
|
| 221 |
+
else:
|
| 222 |
+
print_summary_table(artifact)
|
src/hf_release/config/__init__.py
ADDED
|
File without changes
|
src/hf_release/config/auth.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Token resolution: CLI flag → env var → interactive prompt."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
import typer
|
| 8 |
+
|
| 9 |
+
from ..utils.exceptions import AuthenticationError
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def resolve_hf_token(explicit: str | None) -> str:
|
| 13 |
+
if explicit:
|
| 14 |
+
return explicit
|
| 15 |
+
env = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
|
| 16 |
+
if env:
|
| 17 |
+
return env
|
| 18 |
+
token = typer.prompt(
|
| 19 |
+
"Hugging Face write token (set HF_TOKEN to skip this prompt)",
|
| 20 |
+
hide_input=True,
|
| 21 |
+
)
|
| 22 |
+
if not token:
|
| 23 |
+
raise AuthenticationError(
|
| 24 |
+
"No Hugging Face token provided.",
|
| 25 |
+
suggestion="Export HF_TOKEN or pass --hf-token.",
|
| 26 |
+
)
|
| 27 |
+
return token
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def resolve_gh_token(explicit: str | None) -> str:
|
| 31 |
+
if explicit:
|
| 32 |
+
return explicit
|
| 33 |
+
env = os.environ.get("GITHUB_TOKEN") or os.environ.get("GH_TOKEN")
|
| 34 |
+
if env:
|
| 35 |
+
return env
|
| 36 |
+
token = typer.prompt(
|
| 37 |
+
"GitHub personal access token (set GITHUB_TOKEN to skip this prompt)",
|
| 38 |
+
hide_input=True,
|
| 39 |
+
)
|
| 40 |
+
if not token:
|
| 41 |
+
raise AuthenticationError(
|
| 42 |
+
"No GitHub token provided.",
|
| 43 |
+
suggestion="Export GITHUB_TOKEN or pass --gh-token.",
|
| 44 |
+
)
|
| 45 |
+
return token
|
src/hf_release/core/__init__.py
ADDED
|
File without changes
|
src/hf_release/core/github_client.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""GitHub operations via PyGithub."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import github
|
| 6 |
+
from github import Github, GithubException
|
| 7 |
+
from github.Repository import Repository
|
| 8 |
+
|
| 9 |
+
from ..utils.exceptions import AuthenticationError, ReleaseCreationError, RepoNotFoundError, TagConflictError
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class GHClient:
|
| 13 |
+
def __init__(self, token: str) -> None:
|
| 14 |
+
self._gh = Github(token)
|
| 15 |
+
|
| 16 |
+
def get_repo(self, repo_id: str) -> Repository:
|
| 17 |
+
try:
|
| 18 |
+
return self._gh.get_repo(repo_id)
|
| 19 |
+
except GithubException as exc:
|
| 20 |
+
if exc.status == 401:
|
| 21 |
+
raise AuthenticationError(
|
| 22 |
+
"GitHub token rejected.",
|
| 23 |
+
suggestion="Check that GITHUB_TOKEN has 'repo' scope.",
|
| 24 |
+
) from exc
|
| 25 |
+
if exc.status == 404:
|
| 26 |
+
raise RepoNotFoundError(
|
| 27 |
+
f"GitHub repo '{repo_id}' not found.",
|
| 28 |
+
suggestion="Check the repo name and token permissions.",
|
| 29 |
+
) from exc
|
| 30 |
+
raise
|
| 31 |
+
|
| 32 |
+
# ------------------------------------------------------------------
|
| 33 |
+
# Tagging
|
| 34 |
+
# ------------------------------------------------------------------
|
| 35 |
+
|
| 36 |
+
def ensure_tag(self, repo: Repository, tag_name: str, message: str | None = None) -> None:
|
| 37 |
+
"""Create an annotated tag pointing to HEAD. Idempotent if same SHA."""
|
| 38 |
+
try:
|
| 39 |
+
head_sha = repo.get_branch(repo.default_branch).commit.sha
|
| 40 |
+
tag_obj = repo.create_git_tag(
|
| 41 |
+
tag=tag_name,
|
| 42 |
+
message=message or tag_name,
|
| 43 |
+
object=head_sha,
|
| 44 |
+
type="commit",
|
| 45 |
+
)
|
| 46 |
+
repo.create_git_ref(ref=f"refs/tags/{tag_name}", sha=tag_obj.sha)
|
| 47 |
+
except GithubException as exc:
|
| 48 |
+
if exc.status == 422:
|
| 49 |
+
# Already exists — check if it points to the same commit
|
| 50 |
+
try:
|
| 51 |
+
existing = repo.get_git_ref(f"tags/{tag_name}")
|
| 52 |
+
# If it exists and we can read it, treat as idempotent success
|
| 53 |
+
_ = existing
|
| 54 |
+
return
|
| 55 |
+
except GithubException:
|
| 56 |
+
pass
|
| 57 |
+
raise TagConflictError(
|
| 58 |
+
f"GitHub tag '{tag_name}' already exists.",
|
| 59 |
+
suggestion="Use a different version or delete the existing tag first.",
|
| 60 |
+
) from exc
|
| 61 |
+
raise TagConflictError(f"Failed to create GitHub tag '{tag_name}': {exc}") from exc
|
| 62 |
+
|
| 63 |
+
def delete_tag(self, repo: Repository, tag_name: str) -> None:
|
| 64 |
+
try:
|
| 65 |
+
ref = repo.get_git_ref(f"tags/{tag_name}")
|
| 66 |
+
ref.delete()
|
| 67 |
+
except GithubException as exc:
|
| 68 |
+
if exc.status == 404:
|
| 69 |
+
return # Already gone
|
| 70 |
+
raise
|
| 71 |
+
|
| 72 |
+
# ------------------------------------------------------------------
|
| 73 |
+
# Releases
|
| 74 |
+
# ------------------------------------------------------------------
|
| 75 |
+
|
| 76 |
+
def _get_previous_tag(self, repo: Repository) -> str | None:
|
| 77 |
+
try:
|
| 78 |
+
return repo.get_latest_release().tag_name
|
| 79 |
+
except GithubException:
|
| 80 |
+
return None
|
| 81 |
+
|
| 82 |
+
def generate_notes(
|
| 83 |
+
self,
|
| 84 |
+
repo: Repository,
|
| 85 |
+
tag_name: str,
|
| 86 |
+
previous_tag: str | None = None,
|
| 87 |
+
target_commitish: str | None = None,
|
| 88 |
+
) -> str:
|
| 89 |
+
"""Call GitHub's native release-notes generation endpoint."""
|
| 90 |
+
try:
|
| 91 |
+
prev = previous_tag or self._get_previous_tag(repo)
|
| 92 |
+
kwargs: dict = {"tag_name": tag_name}
|
| 93 |
+
if prev:
|
| 94 |
+
kwargs["previous_tag_name"] = prev
|
| 95 |
+
if target_commitish:
|
| 96 |
+
kwargs["target_commitish"] = target_commitish
|
| 97 |
+
notes = repo.generate_release_notes(**kwargs)
|
| 98 |
+
return notes.body
|
| 99 |
+
except Exception:
|
| 100 |
+
return ""
|
| 101 |
+
|
| 102 |
+
def create_release(
|
| 103 |
+
self,
|
| 104 |
+
repo: Repository,
|
| 105 |
+
tag_name: str,
|
| 106 |
+
name: str | None = None,
|
| 107 |
+
body: str | None = None,
|
| 108 |
+
draft: bool = False,
|
| 109 |
+
prerelease: bool = False,
|
| 110 |
+
) -> str:
|
| 111 |
+
"""Create a GitHub release. Returns the HTML URL."""
|
| 112 |
+
try:
|
| 113 |
+
release = repo.create_git_release(
|
| 114 |
+
tag=tag_name,
|
| 115 |
+
name=name or tag_name,
|
| 116 |
+
message=body or "",
|
| 117 |
+
draft=draft,
|
| 118 |
+
prerelease=prerelease,
|
| 119 |
+
)
|
| 120 |
+
return release.html_url
|
| 121 |
+
except GithubException as exc:
|
| 122 |
+
raise ReleaseCreationError(
|
| 123 |
+
f"Failed to create GitHub release: {exc.data.get('message', exc)}",
|
| 124 |
+
suggestion="Check that the tag exists and the token has 'repo' write access.",
|
| 125 |
+
) from exc
|
src/hf_release/core/hf_client.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Hugging Face Hub operations."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
from huggingface_hub import HfApi, ModelCard
|
| 8 |
+
from huggingface_hub.errors import (
|
| 9 |
+
EntryNotFoundError,
|
| 10 |
+
RepositoryNotFoundError,
|
| 11 |
+
RevisionNotFoundError,
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
from ..utils.exceptions import AuthenticationError, ModelCardSyncError, RepoNotFoundError, TagConflictError, UploadError
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class HFClient:
|
| 18 |
+
def __init__(self, token: str) -> None:
|
| 19 |
+
self._token = token
|
| 20 |
+
self._api = HfApi(token=token)
|
| 21 |
+
|
| 22 |
+
# ------------------------------------------------------------------
|
| 23 |
+
# Repo helpers
|
| 24 |
+
# ------------------------------------------------------------------
|
| 25 |
+
|
| 26 |
+
def ensure_repo_exists(self, repo_id: str, create: bool = False) -> None:
|
| 27 |
+
try:
|
| 28 |
+
self._api.repo_info(repo_id=repo_id, repo_type="model")
|
| 29 |
+
except RepositoryNotFoundError:
|
| 30 |
+
if create:
|
| 31 |
+
self._api.create_repo(repo_id=repo_id, repo_type="model", private=False)
|
| 32 |
+
else:
|
| 33 |
+
raise RepoNotFoundError(
|
| 34 |
+
f"HF repo '{repo_id}' not found.",
|
| 35 |
+
suggestion="Create it on huggingface.co or pass --create-repo.",
|
| 36 |
+
)
|
| 37 |
+
except Exception as exc:
|
| 38 |
+
if "401" in str(exc) or "403" in str(exc):
|
| 39 |
+
raise AuthenticationError(
|
| 40 |
+
"HF token rejected.",
|
| 41 |
+
suggestion="Check that HF_TOKEN has write access to the repo.",
|
| 42 |
+
) from exc
|
| 43 |
+
raise
|
| 44 |
+
|
| 45 |
+
# ------------------------------------------------------------------
|
| 46 |
+
# Upload
|
| 47 |
+
# ------------------------------------------------------------------
|
| 48 |
+
|
| 49 |
+
def push_model(
|
| 50 |
+
self,
|
| 51 |
+
local_path: Path,
|
| 52 |
+
repo_id: str,
|
| 53 |
+
branch: str = "main",
|
| 54 |
+
commit_message: str | None = None,
|
| 55 |
+
ignore_patterns: list[str] | None = None,
|
| 56 |
+
) -> str:
|
| 57 |
+
"""Upload a local directory to HF Hub. Returns the commit URL."""
|
| 58 |
+
msg = commit_message or f"Upload model via hf-release"
|
| 59 |
+
try:
|
| 60 |
+
result = self._api.upload_folder(
|
| 61 |
+
folder_path=str(local_path),
|
| 62 |
+
repo_id=repo_id,
|
| 63 |
+
repo_type="model",
|
| 64 |
+
commit_message=msg,
|
| 65 |
+
revision=branch,
|
| 66 |
+
ignore_patterns=ignore_patterns or [],
|
| 67 |
+
)
|
| 68 |
+
except RepositoryNotFoundError:
|
| 69 |
+
raise RepoNotFoundError(
|
| 70 |
+
f"HF repo '{repo_id}' not found.",
|
| 71 |
+
suggestion="Create it on huggingface.co or pass --create-repo.",
|
| 72 |
+
)
|
| 73 |
+
except Exception as exc:
|
| 74 |
+
raise UploadError(f"Failed to upload model: {exc}") from exc
|
| 75 |
+
|
| 76 |
+
return result if isinstance(result, str) else str(result)
|
| 77 |
+
|
| 78 |
+
# ------------------------------------------------------------------
|
| 79 |
+
# Tagging
|
| 80 |
+
# ------------------------------------------------------------------
|
| 81 |
+
|
| 82 |
+
def create_tag(self, repo_id: str, tag: str, message: str | None = None) -> None:
|
| 83 |
+
try:
|
| 84 |
+
self._api.create_tag(
|
| 85 |
+
repo_id=repo_id,
|
| 86 |
+
repo_type="model",
|
| 87 |
+
tag=tag,
|
| 88 |
+
tag_message=message or tag,
|
| 89 |
+
)
|
| 90 |
+
except RevisionNotFoundError:
|
| 91 |
+
raise TagConflictError(
|
| 92 |
+
f"Could not create tag '{tag}' on HF repo '{repo_id}': revision not found.",
|
| 93 |
+
suggestion="Make sure the model was pushed before tagging.",
|
| 94 |
+
)
|
| 95 |
+
except Exception as exc:
|
| 96 |
+
if "already exists" in str(exc).lower():
|
| 97 |
+
# Tag already there — treat as idempotent success
|
| 98 |
+
return
|
| 99 |
+
raise TagConflictError(f"Failed to create HF tag '{tag}': {exc}") from exc
|
| 100 |
+
|
| 101 |
+
# ------------------------------------------------------------------
|
| 102 |
+
# Model card
|
| 103 |
+
# ------------------------------------------------------------------
|
| 104 |
+
|
| 105 |
+
def get_model_card(self, repo_id: str) -> ModelCard | None:
|
| 106 |
+
try:
|
| 107 |
+
path = self._api.hf_hub_download(
|
| 108 |
+
repo_id=repo_id,
|
| 109 |
+
filename="README.md",
|
| 110 |
+
repo_type="model",
|
| 111 |
+
)
|
| 112 |
+
return ModelCard.load(path)
|
| 113 |
+
except EntryNotFoundError:
|
| 114 |
+
return None
|
| 115 |
+
except RepositoryNotFoundError:
|
| 116 |
+
raise RepoNotFoundError(f"HF repo '{repo_id}' not found.")
|
| 117 |
+
except Exception as exc:
|
| 118 |
+
raise ModelCardSyncError(f"Failed to fetch model card: {exc}") from exc
|
| 119 |
+
|
| 120 |
+
def push_model_card(self, card: ModelCard, repo_id: str) -> None:
|
| 121 |
+
try:
|
| 122 |
+
card.push_to_hub(repo_id, token=self._token)
|
| 123 |
+
except Exception as exc:
|
| 124 |
+
raise ModelCardSyncError(f"Failed to push model card to HF: {exc}") from exc
|
src/hf_release/core/model_card.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Model Card sync between local disk, HF Hub, and GitHub."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
from huggingface_hub import ModelCard, ModelCardData
|
| 8 |
+
|
| 9 |
+
from ..utils.exceptions import ModelCardSyncError
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def load_local_card(model_dir: Path) -> ModelCard | None:
|
| 13 |
+
readme = model_dir / "README.md"
|
| 14 |
+
if not readme.exists():
|
| 15 |
+
return None
|
| 16 |
+
try:
|
| 17 |
+
return ModelCard.load(str(readme))
|
| 18 |
+
except Exception as exc:
|
| 19 |
+
raise ModelCardSyncError(f"Failed to parse local README.md: {exc}") from exc
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def update_card_metadata(card: ModelCard, version: str, extra_tags: list[str] | None = None) -> ModelCard:
|
| 23 |
+
"""Inject version tag and any extra tags into the card frontmatter."""
|
| 24 |
+
data: ModelCardData = card.data
|
| 25 |
+
tags: list[str] = list(data.tags or [])
|
| 26 |
+
|
| 27 |
+
if version not in tags:
|
| 28 |
+
tags.append(version)
|
| 29 |
+
for t in extra_tags or []:
|
| 30 |
+
if t not in tags:
|
| 31 |
+
tags.append(t)
|
| 32 |
+
|
| 33 |
+
data.tags = tags
|
| 34 |
+
card.data = data
|
| 35 |
+
return card
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def sync_card(
|
| 39 |
+
*,
|
| 40 |
+
model_dir: Path | None,
|
| 41 |
+
hf_client, # HFClient | None
|
| 42 |
+
hf_repo: str | None,
|
| 43 |
+
version: str,
|
| 44 |
+
extra_tags: list[str] | None = None,
|
| 45 |
+
) -> bool:
|
| 46 |
+
"""
|
| 47 |
+
Sync model card from local dir → HF Hub.
|
| 48 |
+
Returns True if a card was pushed, False if skipped.
|
| 49 |
+
"""
|
| 50 |
+
if model_dir is None or hf_client is None or hf_repo is None:
|
| 51 |
+
return False
|
| 52 |
+
|
| 53 |
+
card = load_local_card(model_dir)
|
| 54 |
+
if card is None:
|
| 55 |
+
# Try to pull from HF and update metadata only
|
| 56 |
+
card = hf_client.get_model_card(hf_repo)
|
| 57 |
+
if card is None:
|
| 58 |
+
return False
|
| 59 |
+
|
| 60 |
+
card = update_card_metadata(card, version, extra_tags)
|
| 61 |
+
hf_client.push_model_card(card, hf_repo)
|
| 62 |
+
return True
|
src/hf_release/core/release_notes.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Release notes generation — three-tier strategy."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from github.Repository import Repository
|
| 6 |
+
|
| 7 |
+
from .github_client import GHClient
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def build_notes(
|
| 11 |
+
version: str,
|
| 12 |
+
hf_repo: str | None,
|
| 13 |
+
gh_client: GHClient | None,
|
| 14 |
+
gh_repo_obj: Repository | None,
|
| 15 |
+
previous_tag: str | None = None,
|
| 16 |
+
notes_file: str | None = None,
|
| 17 |
+
) -> str:
|
| 18 |
+
# Tier 0: explicit file override
|
| 19 |
+
if notes_file:
|
| 20 |
+
try:
|
| 21 |
+
with open(notes_file) as f:
|
| 22 |
+
return f.read()
|
| 23 |
+
except OSError:
|
| 24 |
+
pass
|
| 25 |
+
|
| 26 |
+
# Tier 1: GitHub native generation
|
| 27 |
+
if gh_client and gh_repo_obj:
|
| 28 |
+
notes = gh_client.generate_notes(gh_repo_obj, version, previous_tag)
|
| 29 |
+
if notes:
|
| 30 |
+
return _append_ml_artifacts(notes, version, hf_repo, gh_repo_obj.full_name)
|
| 31 |
+
|
| 32 |
+
# Tier 3: minimal fallback
|
| 33 |
+
parts = [f"Release {version}"]
|
| 34 |
+
if hf_repo:
|
| 35 |
+
parts.append(f"\n\n**Hugging Face Hub:** https://huggingface.co/{hf_repo}")
|
| 36 |
+
if gh_repo_obj:
|
| 37 |
+
parts.append(f"\n**GitHub:** https://github.com/{gh_repo_obj.full_name}/releases/tag/{version}")
|
| 38 |
+
return "".join(parts)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def _append_ml_artifacts(body: str, version: str, hf_repo: str | None, gh_repo: str) -> str:
|
| 42 |
+
rows = []
|
| 43 |
+
if hf_repo:
|
| 44 |
+
rows.append(
|
| 45 |
+
f"| Hugging Face Hub | [Model files](https://huggingface.co/{hf_repo}/tree/{version}) |"
|
| 46 |
+
)
|
| 47 |
+
rows.append(
|
| 48 |
+
f"| Model Card | [README](https://huggingface.co/{hf_repo}) |"
|
| 49 |
+
)
|
| 50 |
+
rows.append(
|
| 51 |
+
f"| GitHub | [Release](https://github.com/{gh_repo}/releases/tag/{version}) |"
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
section = (
|
| 55 |
+
"\n\n## ML Artifacts\n\n"
|
| 56 |
+
"| Platform | Link |\n"
|
| 57 |
+
"|----------|------|\n"
|
| 58 |
+
+ "\n".join(rows)
|
| 59 |
+
)
|
| 60 |
+
return body + section
|
src/hf_release/py.typed
ADDED
|
File without changes
|
src/hf_release/utils/__init__.py
ADDED
|
File without changes
|
src/hf_release/utils/display.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Centralized Rich display helpers."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from dataclasses import dataclass
|
| 6 |
+
|
| 7 |
+
from rich.console import Console
|
| 8 |
+
from rich.panel import Panel
|
| 9 |
+
from rich.progress import BarColumn, Progress, SpinnerColumn, TextColumn, TimeElapsedColumn
|
| 10 |
+
from rich.table import Table
|
| 11 |
+
|
| 12 |
+
# Use stderr so stdout stays clean for piped/script usage
|
| 13 |
+
console = Console(stderr=True)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
@dataclass
|
| 17 |
+
class ReleaseArtifact:
|
| 18 |
+
version: str
|
| 19 |
+
hf_repo: str | None
|
| 20 |
+
gh_repo: str | None
|
| 21 |
+
hf_url: str | None
|
| 22 |
+
gh_url: str | None
|
| 23 |
+
hf_tag: str | None
|
| 24 |
+
gh_tag: str | None
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def make_progress() -> Progress:
|
| 28 |
+
return Progress(
|
| 29 |
+
SpinnerColumn(),
|
| 30 |
+
TextColumn("[progress.description]{task.description}"),
|
| 31 |
+
BarColumn(),
|
| 32 |
+
TimeElapsedColumn(),
|
| 33 |
+
console=console,
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def print_summary_table(artifact: ReleaseArtifact) -> None:
|
| 38 |
+
table = Table(title=f"Release {artifact.version} — Summary", show_lines=True)
|
| 39 |
+
table.add_column("Platform", style="bold cyan", no_wrap=True)
|
| 40 |
+
table.add_column("Repo")
|
| 41 |
+
table.add_column("Tag", style="green")
|
| 42 |
+
table.add_column("URL", style="blue")
|
| 43 |
+
|
| 44 |
+
if artifact.hf_repo:
|
| 45 |
+
table.add_row(
|
| 46 |
+
"Hugging Face Hub",
|
| 47 |
+
artifact.hf_repo,
|
| 48 |
+
artifact.hf_tag or "-",
|
| 49 |
+
artifact.hf_url or "-",
|
| 50 |
+
)
|
| 51 |
+
if artifact.gh_repo:
|
| 52 |
+
table.add_row(
|
| 53 |
+
"GitHub",
|
| 54 |
+
artifact.gh_repo,
|
| 55 |
+
artifact.gh_tag or "-",
|
| 56 |
+
artifact.gh_url or "-",
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
console.print(table)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def print_error(exc: Exception, suggestion: str = "") -> None:
|
| 63 |
+
suggestion_text = suggestion or getattr(exc, "suggestion", "")
|
| 64 |
+
body = str(exc)
|
| 65 |
+
if suggestion_text:
|
| 66 |
+
body += f"\n\n[dim]Suggestion:[/dim] {suggestion_text}"
|
| 67 |
+
console.print(Panel(body, title="[bold red]Error[/bold red]", border_style="red"))
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def print_dry_run_panel(steps: list[str]) -> None:
|
| 71 |
+
body = "\n".join(f" • {s}" for s in steps)
|
| 72 |
+
console.print(
|
| 73 |
+
Panel(
|
| 74 |
+
f"[bold]The following actions would be performed:[/bold]\n\n{body}",
|
| 75 |
+
title="[bold blue]Dry Run[/bold blue]",
|
| 76 |
+
border_style="blue",
|
| 77 |
+
)
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def print_partial_success(completed: list[str], failed: list[str]) -> None:
|
| 82 |
+
body = ""
|
| 83 |
+
if completed:
|
| 84 |
+
body += "[green]Completed:[/green]\n" + "\n".join(f" ✓ {s}" for s in completed) + "\n\n"
|
| 85 |
+
if failed:
|
| 86 |
+
body += "[red]Failed:[/red]\n" + "\n".join(f" ✗ {s}" for s in failed)
|
| 87 |
+
console.print(Panel(body, title="[bold yellow]Partial Success[/bold yellow]", border_style="yellow"))
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def confirm_plan(steps: list[str]) -> bool:
|
| 91 |
+
"""Print a plan summary and ask for confirmation. Returns True if confirmed."""
|
| 92 |
+
body = "\n".join(f" • {s}" for s in steps)
|
| 93 |
+
console.print(
|
| 94 |
+
Panel(
|
| 95 |
+
f"[bold]About to execute:[/bold]\n\n{body}",
|
| 96 |
+
title="[bold cyan]Release Plan[/bold cyan]",
|
| 97 |
+
border_style="cyan",
|
| 98 |
+
)
|
| 99 |
+
)
|
| 100 |
+
import typer
|
| 101 |
+
return typer.confirm("Proceed?")
|
src/hf_release/utils/exceptions.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Custom exception hierarchy for hf-release."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class HFReleaseError(Exception):
|
| 7 |
+
"""Base exception for all hf-release errors."""
|
| 8 |
+
|
| 9 |
+
exit_code: int = 1
|
| 10 |
+
|
| 11 |
+
def __init__(self, message: str, suggestion: str = "") -> None:
|
| 12 |
+
super().__init__(message)
|
| 13 |
+
self.suggestion = suggestion
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class AuthenticationError(HFReleaseError):
|
| 17 |
+
"""Raised when a token is missing or invalid."""
|
| 18 |
+
|
| 19 |
+
exit_code = 2
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class RepoNotFoundError(HFReleaseError):
|
| 23 |
+
"""Raised when a HF or GitHub repo does not exist."""
|
| 24 |
+
|
| 25 |
+
exit_code = 3
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class TagConflictError(HFReleaseError):
|
| 29 |
+
"""Raised when a tag already exists with a different SHA."""
|
| 30 |
+
|
| 31 |
+
exit_code = 4
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class ModelCardSyncError(HFReleaseError):
|
| 35 |
+
"""Raised when reading or pushing a model card fails."""
|
| 36 |
+
|
| 37 |
+
exit_code = 5
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class UploadError(HFReleaseError):
|
| 41 |
+
"""Raised when uploading model weights to HF Hub fails."""
|
| 42 |
+
|
| 43 |
+
exit_code = 6
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
class ReleaseCreationError(HFReleaseError):
|
| 47 |
+
"""Raised when creating a GitHub release fails."""
|
| 48 |
+
|
| 49 |
+
exit_code = 7
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
class ValidationError(HFReleaseError):
|
| 53 |
+
"""Raised for bad input (invalid version string, missing path, etc.)."""
|
| 54 |
+
|
| 55 |
+
exit_code = 8
|
src/hf_release/utils/fs.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Local filesystem helpers."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import re
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
from .exceptions import ValidationError
|
| 9 |
+
|
| 10 |
+
_REPO_ID_RE = re.compile(r"^[a-zA-Z0-9_.\-]+/[a-zA-Z0-9_.\-]+$")
|
| 11 |
+
_VERSION_RE = re.compile(r"^v?\d+\.\d+\.\d+([.\-].+)?$")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def validate_model_dir(path: Path) -> Path:
|
| 15 |
+
if not path.exists():
|
| 16 |
+
raise ValidationError(
|
| 17 |
+
f"Model directory does not exist: {path}",
|
| 18 |
+
suggestion="Pass the correct --model path.",
|
| 19 |
+
)
|
| 20 |
+
if not path.is_dir():
|
| 21 |
+
raise ValidationError(
|
| 22 |
+
f"--model must be a directory, got a file: {path}",
|
| 23 |
+
suggestion="Point --model to the directory containing your model files.",
|
| 24 |
+
)
|
| 25 |
+
files = list(path.iterdir())
|
| 26 |
+
if not files:
|
| 27 |
+
raise ValidationError(
|
| 28 |
+
f"Model directory is empty: {path}",
|
| 29 |
+
suggestion="Make sure training has completed and the output directory is correct.",
|
| 30 |
+
)
|
| 31 |
+
return path
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def validate_version(version: str) -> str:
|
| 35 |
+
if not _VERSION_RE.match(version):
|
| 36 |
+
raise ValidationError(
|
| 37 |
+
f"Invalid version string: {version!r}",
|
| 38 |
+
suggestion="Use semantic versioning like v1.2.0 or 1.2.0.",
|
| 39 |
+
)
|
| 40 |
+
return version
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def validate_repo_id(repo_id: str, label: str = "repo") -> str:
|
| 44 |
+
if not _REPO_ID_RE.match(repo_id):
|
| 45 |
+
raise ValidationError(
|
| 46 |
+
f"Invalid {label} ID: {repo_id!r}",
|
| 47 |
+
suggestion="Repo IDs must be in 'owner/name' format.",
|
| 48 |
+
)
|
| 49 |
+
return repo_id
|
tests/__init__.py
ADDED
|
File without changes
|
tests/integration/__init__.py
ADDED
|
File without changes
|
tests/unit/__init__.py
ADDED
|
File without changes
|
tests/unit/test_fs.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Unit tests for input validation helpers."""
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
from hf_release.utils.fs import validate_repo_id, validate_version
|
| 6 |
+
from hf_release.utils.exceptions import ValidationError
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
@pytest.mark.parametrize("v", ["v1.0.0", "1.0.0", "v0.1.0-alpha", "2.3.4"])
|
| 10 |
+
def test_valid_versions(v):
|
| 11 |
+
assert validate_version(v) == v
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@pytest.mark.parametrize("v", ["", "latest", "1.0", "v1", "abc"])
|
| 15 |
+
def test_invalid_versions(v):
|
| 16 |
+
with pytest.raises(ValidationError):
|
| 17 |
+
validate_version(v)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@pytest.mark.parametrize("r", ["owner/repo", "my-org/my-model_v2", "a/b"])
|
| 21 |
+
def test_valid_repo_ids(r):
|
| 22 |
+
assert validate_repo_id(r) == r
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@pytest.mark.parametrize("r", ["", "noslash", "a/b/c", "/repo", "owner/"])
|
| 26 |
+
def test_invalid_repo_ids(r):
|
| 27 |
+
with pytest.raises(ValidationError):
|
| 28 |
+
validate_repo_id(r)
|