Qilan2 commited on
Commit
6aec0a3
·
verified ·
1 Parent(s): 2e56659

Update ff1.py

Browse files
Files changed (1) hide show
  1. ff1.py +20 -9
ff1.py CHANGED
@@ -44,7 +44,8 @@ def get_latest_local_package(directory, pattern='*.tar.gz'):
44
  except Exception as e:
45
  print(f"获取最新包时发生错误: {e}")
46
  return None
47
- def compress_folder(folder_path, output_dir):
 
48
  try:
49
  # 确保输出目录存在
50
  os.makedirs(output_dir, exist_ok=True)
@@ -60,7 +61,7 @@ def compress_folder(folder_path, output_dir):
60
  timestamp = str(int(datetime.now(china_tz).timestamp() * 1000))
61
  output_path = os.path.join(output_dir, f'{timestamp}.tar.gz')
62
 
63
- # 获取已存在的压缩包
64
  existing_archives = glob.glob(os.path.join(output_dir, '*.tar.gz'))
65
 
66
  # 安全地提取时间戳
@@ -69,15 +70,22 @@ def compress_folder(folder_path, output_dir):
69
  match = re.search(r'(\d+)\.tar\.gz$', filename)
70
  return int(match.group(1)) if match else 0
71
 
72
- # 如果压缩包数量超过5个,删除最旧的
73
- if len(existing_archives) >= 3:
74
- # 按时间戳排序
75
  existing_archives.sort(key=extract_timestamp)
76
 
 
 
 
77
  # 删除最旧的压缩包
78
- oldest_archive = existing_archives[0]
79
- os.remove(oldest_archive)
80
- print(f"删除最旧的压缩包:{oldest_archive}")
 
 
 
 
81
 
82
  # tar.gz 压缩
83
  result = subprocess.run(
@@ -97,6 +105,7 @@ def compress_folder(folder_path, output_dir):
97
  print(f"压缩成功:{output_path}")
98
  print(f"压缩大小:{file_size:.2f} MB")
99
  print(f"压缩时间:{formatted_time}")
 
100
 
101
  # 返回压缩包名和大小信息
102
  return f"{os.path.basename(output_path)} MB:{file_size:.2f} MB TIME:{formatted_time}"
@@ -134,8 +143,9 @@ def github(type):
134
  print(f"开始备份上传HF仓库:{HF_REPO}")
135
  os.system("mkdir -p /data/f")
136
  os.system("cp -rf /data/ff /data/f")
 
137
  # 备份上传仓库
138
- new_archive_info = compress_folder('/data/f', f'/data/{HF_REPO}')
139
  if new_archive_info:
140
  new_archive, file_size_info = new_archive_info.split(' MB:')
141
  os.chdir(f'/data/{HF_REPO}')
@@ -144,6 +154,7 @@ def github(type):
144
  os.system(f'git commit -m "{file_size_info}"')
145
  # os.system('git push -u origin main')
146
  os.system('git push -f origin main')
 
147
  else:
148
  print("压缩失败,无法提交")
149
 
 
44
  except Exception as e:
45
  print(f"获取最新包时发生错误: {e}")
46
  return None
47
+
48
+ def compress_folder(folder_path, output_dir, keep_count=3):
49
  try:
50
  # 确保输出目录存在
51
  os.makedirs(output_dir, exist_ok=True)
 
61
  timestamp = str(int(datetime.now(china_tz).timestamp() * 1000))
62
  output_path = os.path.join(output_dir, f'{timestamp}.tar.gz')
63
 
64
+ # 获取所有压缩包
65
  existing_archives = glob.glob(os.path.join(output_dir, '*.tar.gz'))
66
 
67
  # 安全地提取时间戳
 
70
  match = re.search(r'(\d+)\.tar\.gz$', filename)
71
  return int(match.group(1)) if match else 0
72
 
73
+ # 如果压缩包数量超过保留数量,删除最旧的
74
+ if len(existing_archives) >= keep_count:
75
+ # 按时间戳排序(从小到大,最旧的在前面)
76
  existing_archives.sort(key=extract_timestamp)
77
 
78
+ # 计算需要删除的数量
79
+ delete_count = len(existing_archives) - keep_count + 1
80
+
81
  # 删除最旧的压缩包
82
+ for i in range(delete_count):
83
+ oldest_archive = existing_archives[i]
84
+ try:
85
+ os.remove(oldest_archive)
86
+ print(f"删除最旧的压缩包:{os.path.basename(oldest_archive)}")
87
+ except Exception as e:
88
+ print(f"删除失败 {oldest_archive}: {e}")
89
 
90
  # tar.gz 压缩
91
  result = subprocess.run(
 
105
  print(f"压缩成功:{output_path}")
106
  print(f"压缩大小:{file_size:.2f} MB")
107
  print(f"压缩时间:{formatted_time}")
108
+ print(f"保留策略:最多保留 {keep_count} 个备份包")
109
 
110
  # 返回压缩包名和大小信息
111
  return f"{os.path.basename(output_path)} MB:{file_size:.2f} MB TIME:{formatted_time}"
 
143
  print(f"开始备份上传HF仓库:{HF_REPO}")
144
  os.system("mkdir -p /data/f")
145
  os.system("cp -rf /data/ff /data/f")
146
+ os.system('git lfs prune')
147
  # 备份上传仓库
148
+ new_archive_info = compress_folder('/data/f', f'/data/{HF_REPO}', keep_count=3)
149
  if new_archive_info:
150
  new_archive, file_size_info = new_archive_info.split(' MB:')
151
  os.chdir(f'/data/{HF_REPO}')
 
154
  os.system(f'git commit -m "{file_size_info}"')
155
  # os.system('git push -u origin main')
156
  os.system('git push -f origin main')
157
+ os.system('git gc --prune=now')
158
  else:
159
  print("压缩失败,无法提交")
160