File size: 3,713 Bytes
c442f07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/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()