Qilan2 commited on
Commit
e683cfc
·
verified ·
1 Parent(s): 5fd1257

Update server3.py

Browse files
Files changed (1) hide show
  1. server3.py +53 -50
server3.py CHANGED
@@ -183,70 +183,73 @@ def update_last_uploaded_size(size):
183
  global last_uploaded_size
184
  last_uploaded_size = size
185
 
 
 
 
 
 
 
 
 
 
 
186
  def compress_folder():
187
  global last_uploaded_size # 声明使用全局变量
188
-
189
  # 清理并创建目录 /a
190
  if os.path.exists('/a'):
191
  subprocess.run(['rm', '-r', '/a/'])
192
-
193
  os.makedirs('/a', exist_ok=True)
194
-
195
  with lock: # 使用锁
196
- if os.path.exists(zip_file_path):
197
- print(f"压缩文件 {zip_file_path} 已存在,跳过本次压缩。")
198
- return
199
-
200
- print('开始压缩数据')
201
- #result = subprocess.run(['7z', 'a', '-tzip', '-mx=1','-mmt=2', zip_file_path, data_folder])
202
- # 7z a -tzip -mx=1 -mmt=2 /a/sillytavern.zip /sillytavern/*
203
- # tar -cvf - /sillytavern | pigz -p 6 -1 > /a/sillytavern.tar.gz
204
- try:
205
- result = subprocess.run(
206
- f'tar -cvf - {data_folder} | pigz -p 2 -1 > {zip_file_path} 2> /dev/null',
207
- shell=True,
208
- check=True,
209
- stdout=subprocess.DEVNULL, # 将标准输出重定向到 /dev/null
210
- stderr=subprocess.DEVNULL # 将标准错误也重定向到 /dev/null
211
- )
212
- except subprocess.CalledProcessError as e:
213
- print(f"Command failed with return code {e.returncode}")
214
 
215
- print('重命名压缩后的文件')
216
- file_path = "/a/sillytavern.ctk"
217
- print(zip_file_path)
218
-
219
- subprocess.run(['mv', zip_file_path,file_path])
220
- if result.returncode != 0:
221
- print("压缩失败,请检查7z命令是否正确")
222
  return
223
 
224
- print(f"Data compressed into {file_path}")
225
-
226
- file_size = os.path.getsize(file_path) # 获取压缩包的大小
227
- print('文件大小:', file_size, '上次文件大小:', last_uploaded_size,file_size * 1024 ** 2)
228
-
229
- # 判断是否大于800MB (800MB = 800 * 1024 * 1024 字节)
230
- if file_size < (400 * 1024 ** 2):
231
- print("文件小于800MB,不进行上传。")
232
- return
233
-
234
- if last_uploaded_size == -1 or file_size != last_uploaded_size:
235
- # 判断是否为首次运行或上次上传大小为0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
  if last_uploaded_size == -1:
237
  print("首次运行,正在执行上传...")
238
- update_last_uploaded_size(file_size) # 更新为当前文件大小
239
 
240
  with open(metadata_file_path, 'w') as json_fid:
241
  json.dump(kaggle_metadata, json_fid)
242
- os.system("kaggle datasets version -m 'new version message' -p /a")
243
- #subprocess.run(['kaggle', 'datasets', 'version', '-m', 'new version message', '-p', '/a'])
244
- # kaggle datasets version -m 'new version message' -p /a
245
  last_uploaded_size = file_size
246
-
247
- elif file_size > last_uploaded_size:
248
- print(f"新文件 {zip_file_path} 大于上次上传的大小,正在执行上传...")
249
- update_last_uploaded_size(file_size) # 更新元数据中的最后一次上传大小
250
 
251
  with open(metadata_file_path, 'w') as json_fid:
252
  json.dump(kaggle_metadata, json_fid)
@@ -255,14 +258,14 @@ def compress_folder():
255
  last_uploaded_size = file_size
256
 
257
  else:
258
- print(f"新文件 {zip_file_path} 不大于上次上传的大小,跳过")
259
 
260
 
261
  def repeat_task():
262
  print('------打包线程启动-------------')
263
  while True:
264
  print('等待打包')
265
- time.sleep(1200)
266
  compress_folder()
267
  print('------打包线程结束-------------')
268
  # repeat_task()
 
183
  global last_uploaded_size
184
  last_uploaded_size = size
185
 
186
+ def get_folder_size(folder):
187
+ """使用 du 命令获取文件夹的总大小(字节数)"""
188
+ result = subprocess.run(['du', '-sb', folder], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
189
+ if result.returncode == 0:
190
+ size = result.stdout.split()[0] # 获取大小
191
+ return int(size)
192
+ else:
193
+ print(f"Error calculating size: {result.stderr.decode()}")
194
+ return 0
195
+
196
  def compress_folder():
197
  global last_uploaded_size # 声明使用全局变量
 
198
  # 清理并创建目录 /a
199
  if os.path.exists('/a'):
200
  subprocess.run(['rm', '-r', '/a/'])
 
201
  os.makedirs('/a', exist_ok=True)
 
202
  with lock: # 使用锁
203
+ # 获取当前文件夹的大小
204
+ current_folder_size = get_folder_size(data_folder)
205
+ print(f"当前文件夹大小: {current_folder_size} 字节")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
 
207
+ # 判断文件夹大小是否大于 800MB (800MB = 800 * 1024 * 1024 字节)
208
+ if current_folder_size < (800 * 1024 ** 2):
209
+ print("文件夹小于800MB,不进行上传和压缩。")
 
 
 
 
210
  return
211
 
212
+ # 如果是首次运行或者文件夹大小不同于上次上传,进行压缩
213
+ if last_uploaded_size == -1 or current_folder_size != last_uploaded_size:
214
+ print('开始压缩数据')
215
+ try:
216
+ # 执行压缩
217
+ result = subprocess.run(
218
+ f'tar -cvf - {data_folder} | pigz -p 2 -1 > {zip_file_path} 2> /dev/null',
219
+ shell=True,
220
+ check=True,
221
+ stdout=subprocess.DEVNULL, # 将标准输出重定向到 /dev/null
222
+ stderr=subprocess.DEVNULL # 将标准错误也重定向到 /dev/null
223
+ )
224
+ except subprocess.CalledProcessError as e:
225
+ print(f"命令失败,返回代码: {e.returncode}")
226
+ return
227
+
228
+ print('压缩完成')
229
+ file_path = "/a/sillytavern.ctk"
230
+ subprocess.run(['mv', zip_file_path, file_path])
231
+ print(f"数据压缩为 {file_path}")
232
+
233
+ file_size = os.path.getsize(file_path) # 获取压缩包的大小
234
+ print('文件大小:', file_size)
235
+
236
+ if file_size < (800 * 1024 ** 2): # 判断压缩后的大小是否小于 800MB
237
+ print("压缩文件小于800MB,不进行上传。")
238
+ return
239
+
240
+ # 执行上传
241
  if last_uploaded_size == -1:
242
  print("首次运行,正在执行上传...")
243
+ update_last_uploaded_size(file_size)
244
 
245
  with open(metadata_file_path, 'w') as json_fid:
246
  json.dump(kaggle_metadata, json_fid)
247
+
248
+ subprocess.run(['kaggle', 'datasets', 'version', '-m', 'new version message', '-p', '/a'])
 
249
  last_uploaded_size = file_size
250
+ else:
251
+ print(f"新文件大小 {file_size} 大于上次上传的大小,正在执行上传...")
252
+ update_last_uploaded_size(file_size)
 
253
 
254
  with open(metadata_file_path, 'w') as json_fid:
255
  json.dump(kaggle_metadata, json_fid)
 
258
  last_uploaded_size = file_size
259
 
260
  else:
261
+ print(f"新文件夹大小 {current_folder_size} 不大于上次上传的大小,跳过")
262
 
263
 
264
  def repeat_task():
265
  print('------打包线程启动-------------')
266
  while True:
267
  print('等待打包')
268
+ time.sleep(3600)
269
  compress_folder()
270
  print('------打包线程结束-------------')
271
  # repeat_task()