SeismicX-Cont / scripts /upload_restfiles_ms.py
cangyeone's picture
Upload scripts/upload_restfiles_ms.py
c442f07 verified
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor, as_completed
from modelscope.hub.api import HubApi
# ========= 配置区 =========
repo_id = "cangyeone/SeismicX-Cont"
repo_type = "dataset"
max_workers = 1
root_dir = Path(".")
# =========================
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/hdf5/",
"data/index/",
"data/label/",
"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
# 过滤指定文件名,例如 .DS_Store
if path.name in exclude_names:
return False
# 过滤指定前缀文件名,例如 ._xxx
if path.name.startswith(exclude_name_prefixes):
return False
# 过滤后缀,例如 .pyc / .pyo
if path.suffix in exclude_suffixes:
return False
# 过滤指定目录前缀
for prefix in exclude_prefixes:
if rel.startswith(prefix):
return False
return True
def collect_files():
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 upload_one(item):
local_path, remote_path = item
api = HubApi()
print(f"[UPLOAD START] {local_path} -> {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 main():
api = HubApi()
# 如果没有命令行登录,可以取消下面这行
# api.login("your_token")
try:
api.create_repo(repo_id=repo_id, repo_type=repo_type)
print(f"[INFO] Repo created: {repo_id}")
except Exception as e:
print(f"[INFO] Repo may already exist: {repo_id}")
print(f" {e}")
files_to_upload = collect_files()
print(f"[INFO] Files to upload: {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 __name__ == "__main__":
main()