yifan0sun commited on
Commit
6626b82
Β·
1 Parent(s): 665e88b

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +20 -27
server.py CHANGED
@@ -62,17 +62,8 @@ def ping():
62
  return {"message": "pong"}
63
 
64
 
65
- def zip_if_needed(src_dir, zip_path):
66
- if os.path.exists(zip_path):
67
- return # already zipped
68
- print(f"πŸ“¦ Zipping {src_dir} β†’ {zip_path}")
69
- with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zf:
70
- for root, _, files in os.walk(src_dir):
71
- for file in files:
72
- full_path = os.path.join(root, file)
73
- rel_path = os.path.relpath(full_path, src_dir)
74
- zf.write(full_path, arcname=rel_path)
75
- print(f"βœ… Created zip: {zip_path}")
76
 
77
  def extract_zip_if_needed(zip_path, dest_dir):
78
  if os.path.exists(dest_dir):
@@ -94,9 +85,9 @@ def print_directory_tree(path):
94
  printstr += indent
95
  printstr += f
96
  return printstr
 
97
 
98
-
99
- def copy_zip_extract_and_report():
100
  src_base = "./hf_cache"
101
  dst_base = "/data/hf_cache"
102
 
@@ -110,23 +101,27 @@ def copy_zip_extract_and_report():
110
  os.makedirs(dst_dir, exist_ok=True)
111
 
112
  for name in os.listdir(src_dir):
113
- full_path = os.path.join(src_dir, name)
114
- if not os.path.isdir(full_path):
115
  continue
116
 
117
- zip_name = f"{name}.zip"
118
- local_zip = os.path.join(src_dir, zip_name)
119
- dst_zip = os.path.join(dst_dir, zip_name)
120
- extract_path = os.path.join(dst_dir, name)
121
 
122
- zip_if_needed(full_path, local_zip)
123
-
124
- # Copy zip to /data
125
  if not os.path.exists(dst_zip):
126
- shutil.copy(local_zip, dst_zip)
127
  print(f"πŸ“€ Copied zip to: {dst_zip}")
128
 
129
- extract_zip_if_needed(dst_zip, extract_path)
 
 
 
 
 
 
 
 
 
130
 
131
  print("\nπŸ“¦ Local hf_cache structure:")
132
  printstr1 = print_directory_tree("./hf_cache")
@@ -139,10 +134,8 @@ def copy_zip_extract_and_report():
139
 
140
  @app.get("/copy_and_extract")
141
  def copy_and_extract():
142
- import io
143
- from contextlib import redirect_stdout
144
 
145
- printstr = copy_zip_extract_and_report()
146
 
147
  return {"message": "done", "log": printstr}
148
 
 
62
  return {"message": "pong"}
63
 
64
 
65
+
66
+
 
 
 
 
 
 
 
 
 
67
 
68
  def extract_zip_if_needed(zip_path, dest_dir):
69
  if os.path.exists(dest_dir):
 
85
  printstr += indent
86
  printstr += f
87
  return printstr
88
+
89
 
90
+ def copy_extract_and_report():
 
91
  src_base = "./hf_cache"
92
  dst_base = "/data/hf_cache"
93
 
 
101
  os.makedirs(dst_dir, exist_ok=True)
102
 
103
  for name in os.listdir(src_dir):
104
+ if not name.endswith(".zip"):
 
105
  continue
106
 
107
+ src_zip = os.path.join(src_dir, name)
108
+ dst_zip = os.path.join(dst_dir, name)
 
 
109
 
110
+ # Copy zip to /data if not already present
 
 
111
  if not os.path.exists(dst_zip):
112
+ shutil.copy(src_zip, dst_zip)
113
  print(f"πŸ“€ Copied zip to: {dst_zip}")
114
 
115
+ # Determine the extract folder name (strip ".zip" from the filename)
116
+ extract_folder = os.path.splitext(name)[0]
117
+ extract_path = os.path.join(dst_dir, extract_folder)
118
+
119
+ # Extract if not already extracted
120
+ if not os.path.exists(extract_path):
121
+ os.makedirs(extract_path, exist_ok=True)
122
+ with zipfile.ZipFile(dst_zip, 'r') as zip_ref:
123
+ zip_ref.extractall(extract_path)
124
+ print(f"πŸ“¦ Extracted zip to: {extract_path}")
125
 
126
  print("\nπŸ“¦ Local hf_cache structure:")
127
  printstr1 = print_directory_tree("./hf_cache")
 
134
 
135
  @app.get("/copy_and_extract")
136
  def copy_and_extract():
 
 
137
 
138
+ printstr = copy_extract_and_report()
139
 
140
  return {"message": "done", "log": printstr}
141