cangyeone commited on
Commit
e2cf0d3
·
verified ·
1 Parent(s): 3ce66b9

Upload scripts/upload_restfiles_hf.py

Browse files
Files changed (1) hide show
  1. scripts/upload_restfiles_hf.py +234 -0
scripts/upload_restfiles_hf.py ADDED
@@ -0,0 +1,234 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+
4
+ from pathlib import Path
5
+ from concurrent.futures import ThreadPoolExecutor, as_completed
6
+ from huggingface_hub import CommitOperationDelete, HfApi, create_repo
7
+
8
+ # ========= 配置区 =========
9
+ repo_id = "cangyeone/SeismicX-Cont"
10
+ repo_type = "dataset"
11
+ max_workers = 4
12
+ root_dir = Path(".")
13
+
14
+ # Keep the HuggingFace dataset repository aligned with the main release
15
+ # manifest. This avoids reintroducing local build products, manuscript files,
16
+ # figures, mini-release files, temporary picker outputs, and old root-level
17
+ # helper scripts.
18
+ manifest_path = root_dir / "manifest_sha256.txt"
19
+ delete_remote_extras = True
20
+ force_update_prefixes = (
21
+ "scripts/",
22
+ "utils/",
23
+ "notebooks/",
24
+ )
25
+ force_update_paths = {
26
+ ".gitattributes",
27
+ ".gitignore",
28
+ "LICENSE",
29
+ "README.md",
30
+ "README.zh.md",
31
+ "dataset_plot_data.json",
32
+ "logo.png",
33
+ "manifest_sha256.txt",
34
+ }
35
+ # =========================
36
+
37
+
38
+ exclude_dirs = {
39
+ ".git",
40
+ "__pycache__",
41
+ ".pytest_cache",
42
+ ".ipynb_checkpoints",
43
+ ".vscode",
44
+ ".idea",
45
+ ".cache",
46
+ "publish_mini",
47
+ "essd",
48
+ "eval_picks",
49
+ "figures",
50
+ }
51
+
52
+ exclude_names = {
53
+ ".DS_Store",
54
+ }
55
+
56
+ exclude_suffixes = {
57
+ ".pyc",
58
+ ".pyo",
59
+ }
60
+
61
+ # 过滤 macOS 在 exFAT / FAT / SMB 等文件系统上生成的 AppleDouble 文件
62
+ exclude_name_prefixes = (
63
+ "._",
64
+ )
65
+
66
+ exclude_prefixes = [
67
+ "data/picks/",
68
+ "data/hdf5_hour_subsets/",
69
+ ]
70
+
71
+
72
+ def should_upload(path: Path) -> bool:
73
+ if not path.is_file():
74
+ return False
75
+
76
+ rel = path.relative_to(root_dir).as_posix()
77
+
78
+ if any(part in exclude_dirs for part in path.parts):
79
+ return False
80
+
81
+ if path.name in exclude_names:
82
+ return False
83
+
84
+ if path.name.startswith(exclude_name_prefixes):
85
+ return False
86
+
87
+ if path.suffix in exclude_suffixes:
88
+ return False
89
+
90
+ for prefix in exclude_prefixes:
91
+ if rel.startswith(prefix):
92
+ return False
93
+
94
+ return True
95
+
96
+
97
+ def read_manifest_files():
98
+ if not manifest_path.exists():
99
+ return None
100
+ files = []
101
+ with manifest_path.open("r", encoding="utf-8") as f:
102
+ for line in f:
103
+ if " ./" not in line:
104
+ continue
105
+ rel = line.rstrip("\n").split(" ./", 1)[1]
106
+ p = root_dir / rel
107
+ if p.is_file():
108
+ files.append((p, rel))
109
+ else:
110
+ print(f"[WARN] Manifest entry missing locally: {rel}")
111
+ return files
112
+
113
+
114
+ def collect_files_from_tree():
115
+ files = []
116
+ for p in root_dir.rglob("*"):
117
+ if should_upload(p):
118
+ rel = p.relative_to(root_dir).as_posix()
119
+ files.append((p, rel))
120
+ return files
121
+
122
+
123
+ def collect_desired_files():
124
+ manifest_files = read_manifest_files()
125
+ if manifest_files is not None:
126
+ return manifest_files
127
+ return collect_files_from_tree()
128
+
129
+
130
+ def upload_one(item):
131
+ local_path, remote_path = item
132
+ api = HfApi()
133
+ size_mb = local_path.stat().st_size / 1024 / 1024
134
+
135
+ print(f"[UPLOAD START] {local_path} ({size_mb:.1f} MiB) -> {repo_id}/{remote_path}")
136
+
137
+ try:
138
+ api.upload_file(
139
+ path_or_fileobj=str(local_path),
140
+ path_in_repo=remote_path,
141
+ repo_id=repo_id,
142
+ repo_type=repo_type,
143
+ commit_message=f"Upload {remote_path}",
144
+ )
145
+ return True, str(local_path), "OK"
146
+ except Exception as e:
147
+ return False, str(local_path), str(e)
148
+
149
+
150
+ def delete_extras(api: HfApi, desired_remote_paths):
151
+ remote_files = set(api.list_repo_files(repo_id=repo_id, repo_type=repo_type))
152
+ extras = sorted(remote_files - set(desired_remote_paths))
153
+ if not extras:
154
+ print("[INFO] No remote extras to delete.")
155
+ return []
156
+
157
+ print(f"[INFO] Remote extras to delete: {len(extras)}")
158
+ for rel in extras:
159
+ print(f" - {rel}")
160
+
161
+ operations = [CommitOperationDelete(path_in_repo=rel) for rel in extras]
162
+ api.create_commit(
163
+ repo_id=repo_id,
164
+ repo_type=repo_type,
165
+ operations=operations,
166
+ commit_message="Remove files outside release manifest",
167
+ )
168
+ return extras
169
+
170
+
171
+ def needs_upload(rel, remote_files):
172
+ if rel not in remote_files:
173
+ return True
174
+ if rel in force_update_paths:
175
+ return True
176
+ return rel.startswith(force_update_prefixes)
177
+
178
+
179
+ def main():
180
+ create_repo(
181
+ repo_id=repo_id,
182
+ repo_type=repo_type,
183
+ exist_ok=True,
184
+ )
185
+
186
+ api = HfApi()
187
+ desired_files = collect_desired_files()
188
+ desired_remote_paths = [rel for _, rel in desired_files]
189
+ remote_files = set(api.list_repo_files(repo_id=repo_id, repo_type=repo_type))
190
+ files_to_upload = [(p, rel) for p, rel in desired_files if needs_upload(rel, remote_files)]
191
+
192
+ print(f"[INFO] Repo ready: {repo_id}")
193
+ print(f"[INFO] Desired release files: {len(desired_files)}")
194
+ print(f"[INFO] Remote files before upload: {len(remote_files)}")
195
+ print(f"[INFO] Files to upload/update: {len(files_to_upload)}")
196
+ for _, rel in files_to_upload:
197
+ print(f" - {rel}")
198
+
199
+ failed = []
200
+
201
+ with ThreadPoolExecutor(max_workers=max_workers) as executor:
202
+ futures = {
203
+ executor.submit(upload_one, item): item
204
+ for item in files_to_upload
205
+ }
206
+
207
+ for future in as_completed(futures):
208
+ ok, local_path, msg = future.result()
209
+
210
+ if ok:
211
+ print(f"[DONE] {local_path}")
212
+ else:
213
+ print(f"[FAILED] {local_path}")
214
+ print(f" {msg}")
215
+ failed.append((local_path, msg))
216
+
217
+ print("\n========== SUMMARY ==========")
218
+ print(f"success: {len(files_to_upload) - len(failed)}")
219
+ print(f"failed : {len(failed)}")
220
+
221
+ if failed:
222
+ print("\n失败文件:")
223
+ for local_path, msg in failed:
224
+ print(f"- {local_path}: {msg}")
225
+ else:
226
+ print("全部缺失文件上传完成。")
227
+
228
+ if delete_remote_extras and not failed:
229
+ deleted = delete_extras(api, desired_remote_paths)
230
+ print(f"[INFO] Remote extras deleted: {len(deleted)}")
231
+
232
+
233
+ if __name__ == "__main__":
234
+ main()