#!/usr/bin/env python3 # -*- coding: utf-8 -*- from pathlib import Path from concurrent.futures import ThreadPoolExecutor, as_completed from huggingface_hub import CommitOperationDelete, HfApi, create_repo # ========= 配置区 ========= repo_id = "cangyeone/SeismicX-Cont" repo_type = "dataset" max_workers = 4 root_dir = Path(".") # Keep the HuggingFace dataset repository aligned with the main release # manifest. This avoids reintroducing local build products, manuscript files, # figures, mini-release files, temporary picker outputs, and old root-level # helper scripts. manifest_path = root_dir / "manifest_sha256.txt" delete_remote_extras = True force_update_prefixes = ( "scripts/", "utils/", "notebooks/", ) force_update_paths = { ".gitattributes", ".gitignore", "LICENSE", "README.md", "README.zh.md", "dataset_plot_data.json", "logo.png", "manifest_sha256.txt", } # ========================= exclude_dirs = { ".git", "__pycache__", ".pytest_cache", ".ipynb_checkpoints", ".vscode", ".idea", ".cache", "publish_mini", "essd", "eval_picks", "figures", } exclude_names = { ".DS_Store", } exclude_suffixes = { ".pyc", ".pyo", } # 过滤 macOS 在 exFAT / FAT / SMB 等文件系统上生成的 AppleDouble 文件 exclude_name_prefixes = ( "._", ) exclude_prefixes = [ "data/picks/", "data/hdf5_hour_subsets/", ] def should_upload(path: Path) -> bool: if not path.is_file(): return False rel = path.relative_to(root_dir).as_posix() if any(part in exclude_dirs for part in path.parts): return False if path.name in exclude_names: return False if path.name.startswith(exclude_name_prefixes): return False if path.suffix in exclude_suffixes: return False for prefix in exclude_prefixes: if rel.startswith(prefix): return False return True def read_manifest_files(): if not manifest_path.exists(): return None files = [] files.append((manifest_path, manifest_path.as_posix())) with manifest_path.open("r", encoding="utf-8") as f: for line in f: if " ./" not in line: continue rel = line.rstrip("\n").split(" ./", 1)[1] p = root_dir / rel if p.is_file(): files.append((p, rel)) else: print(f"[WARN] Manifest entry missing locally: {rel}") return files def collect_files_from_tree(): files = [] for p in root_dir.rglob("*"): if should_upload(p): rel = p.relative_to(root_dir).as_posix() files.append((p, rel)) return files def collect_desired_files(): manifest_files = read_manifest_files() if manifest_files is not None: return manifest_files return collect_files_from_tree() def upload_one(item): local_path, remote_path = item api = HfApi() size_mb = local_path.stat().st_size / 1024 / 1024 print(f"[UPLOAD START] {local_path} ({size_mb:.1f} MiB) -> {repo_id}/{remote_path}") try: api.upload_file( path_or_fileobj=str(local_path), path_in_repo=remote_path, repo_id=repo_id, repo_type=repo_type, commit_message=f"Upload {remote_path}", ) return True, str(local_path), "OK" except Exception as e: return False, str(local_path), str(e) def delete_extras(api: HfApi, desired_remote_paths): remote_files = set(api.list_repo_files(repo_id=repo_id, repo_type=repo_type)) extras = sorted(remote_files - set(desired_remote_paths)) if not extras: print("[INFO] No remote extras to delete.") return [] print(f"[INFO] Remote extras to delete: {len(extras)}") for rel in extras: print(f" - {rel}") operations = [CommitOperationDelete(path_in_repo=rel) for rel in extras] api.create_commit( repo_id=repo_id, repo_type=repo_type, operations=operations, commit_message="Remove files outside release manifest", ) return extras def needs_upload(rel, remote_files): if rel not in remote_files: return True if rel in force_update_paths: return True return rel.startswith(force_update_prefixes) def main(): create_repo( repo_id=repo_id, repo_type=repo_type, exist_ok=True, ) api = HfApi() desired_files = collect_desired_files() desired_remote_paths = [rel for _, rel in desired_files] remote_files = set(api.list_repo_files(repo_id=repo_id, repo_type=repo_type)) files_to_upload = [(p, rel) for p, rel in desired_files if needs_upload(rel, remote_files)] print(f"[INFO] Repo ready: {repo_id}") print(f"[INFO] Desired release files: {len(desired_files)}") print(f"[INFO] Remote files before upload: {len(remote_files)}") print(f"[INFO] Files to upload/update: {len(files_to_upload)}") for _, rel in files_to_upload: print(f" - {rel}") failed = [] with ThreadPoolExecutor(max_workers=max_workers) as executor: futures = { executor.submit(upload_one, item): item for item in files_to_upload } for future in as_completed(futures): ok, local_path, msg = future.result() if ok: print(f"[DONE] {local_path}") else: print(f"[FAILED] {local_path}") print(f" {msg}") failed.append((local_path, msg)) print("\n========== SUMMARY ==========") print(f"success: {len(files_to_upload) - len(failed)}") print(f"failed : {len(failed)}") if failed: print("\n失败文件:") for local_path, msg in failed: print(f"- {local_path}: {msg}") else: print("全部缺失文件上传完成。") if delete_remote_extras and not failed: deleted = delete_extras(api, desired_remote_paths) print(f"[INFO] Remote extras deleted: {len(deleted)}") if __name__ == "__main__": main()