|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright 2025 The HuggingFace Inc. team. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +import argparse |
| 16 | +import json |
| 17 | +import os |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | +from huggingface_hub import CommitOperationAdd, HfApi |
| 21 | + |
| 22 | +DEFAULT_DATASET_ID = os.environ.get("CIRCLECI_RESULTS_DATASET_ID", "transformers-community/circleci-test-results") |
| 23 | + |
| 24 | + |
| 25 | +def parse_args() -> argparse.Namespace: |
| 26 | + parser = argparse.ArgumentParser(description="Upload CircleCI collection outputs to the Hub.") |
| 27 | + parser.add_argument("--source-dir", type=str, default="outputs", help="Directory containing summary files.") |
| 28 | + parser.add_argument("--dataset-id", type=str, default=DEFAULT_DATASET_ID, help="Target dataset ID to update.") |
| 29 | + return parser.parse_args() |
| 30 | + |
| 31 | + |
| 32 | +def _load_metadata(source_dir: Path) -> dict: |
| 33 | + metadata_path = source_dir / "metadata.json" |
| 34 | + if not metadata_path.exists(): |
| 35 | + raise FileNotFoundError(f"metadata.json missing in {source_dir}") |
| 36 | + with metadata_path.open() as fp: |
| 37 | + return json.load(fp) |
| 38 | + |
| 39 | + |
| 40 | +def _collect_files(source_dir: Path, base_dir: str) -> list[CommitOperationAdd]: |
| 41 | + filenames = [ |
| 42 | + "collection_summary.json", |
| 43 | + "failure_summary.json", |
| 44 | + "failure_summary.md", |
| 45 | + "test_summary.json", |
| 46 | + "metadata.json", |
| 47 | + ] |
| 48 | + operations = [] |
| 49 | + for filename in filenames: |
| 50 | + path = source_dir / filename |
| 51 | + if not path.exists(): |
| 52 | + continue |
| 53 | + remote = f"{base_dir}/{filename}" |
| 54 | + operations.append(CommitOperationAdd(path_in_repo=remote, path_or_fileobj=str(path))) |
| 55 | + return operations |
| 56 | + |
| 57 | + |
| 58 | +def main(): |
| 59 | + args = parse_args() |
| 60 | + source_dir = Path(args.source_dir).resolve() |
| 61 | + dataset_id = args.dataset_id |
| 62 | + if not dataset_id: |
| 63 | + raise ValueError("Dataset ID is required.") |
| 64 | + |
| 65 | + token = os.environ.get("HF_TOKEN") or os.environ.get("TRANSFORMERS_HUB_BOT_HF_TOKEN") |
| 66 | + if not token: |
| 67 | + raise RuntimeError("HF token not provided. Set HF_TOKEN or TRANSFORMERS_HUB_BOT_HF_TOKEN.") |
| 68 | + |
| 69 | + metadata = _load_metadata(source_dir) |
| 70 | + pr_number = metadata.get("pull_request_number") or "none" |
| 71 | + commit_sha = metadata.get("commit_sha") or "unknown" |
| 72 | + commit_short = commit_sha[:12] |
| 73 | + base_dir = f"pr-{pr_number}/sha-{commit_short}" |
| 74 | + |
| 75 | + operations = _collect_files(source_dir, base_dir) |
| 76 | + if not operations: |
| 77 | + raise RuntimeError(f"No summary files found in {source_dir}.") |
| 78 | + |
| 79 | + api = HfApi(token=token) |
| 80 | + api.create_repo(repo_id=dataset_id, repo_type="dataset", exist_ok=True, token=token) |
| 81 | + |
| 82 | + commit_message = f"Update CircleCI artifacts for PR {pr_number} ({commit_short})" |
| 83 | + api.create_commit( |
| 84 | + repo_id=dataset_id, |
| 85 | + repo_type="dataset", |
| 86 | + operations=operations, |
| 87 | + commit_message=commit_message, |
| 88 | + token=token, |
| 89 | + ) |
| 90 | + print(f"Uploaded {len(operations)} files to {dataset_id}:{base_dir}") |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + main() |
0 commit comments