Qilan2 commited on
Commit
5339f7b
·
verified ·
1 Parent(s): 522ba5c

Update sbx/b.py

Browse files
Files changed (1) hide show
  1. sbx/b.py +107 -2
sbx/b.py CHANGED
@@ -5,6 +5,18 @@ import gc
5
  import threading
6
  import sys
7
  import urllib.parse
 
 
 
 
 
 
 
 
 
 
 
 
8
  def jupyter():
9
  os.system('jupyter lab --ip=0.0.0.0 --port=8002 --no-browser --allow-root --notebook-dir=/data --NotebookApp.token=qilan --ServerApp.disable_check_xsrf=True')
10
 
@@ -62,7 +74,97 @@ HTML_PAGE = """
62
  </body>
63
  </html>
64
  """
65
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  @app.route('/')
67
  def index():
68
  """根路由,返回 HTML 页面"""
@@ -77,7 +179,10 @@ def greet():
77
  data = request.json
78
  name = data.get('name', 'Guest')
79
  return jsonify({"greeting": f"Hello, {name}!"})
80
-
 
 
 
81
  @app.route('/log', methods=['GET'])
82
  def get_log_content():
83
  try:
 
5
  import threading
6
  import sys
7
  import urllib.parse
8
+ import subprocess
9
+ from datetime import datetime
10
+ import signal
11
+ import psutil
12
+ import glob
13
+ import re
14
+ import pytz
15
+
16
+ HF_USER1 = os.environ.get('HF_USER1', '')# HF 备份仓库的用户名
17
+ HF_REPO = os.environ.get('HF_REPO', '')#HF 备份的Models仓库名
18
+ HF_EMAIL = os.environ.get('HF_EMAIL', '') #HF的邮箱
19
+ HF_TOKEN1 = os.environ.get('HF_TOKEN1', '')#HF备份账号的TOKEN
20
  def jupyter():
21
  os.system('jupyter lab --ip=0.0.0.0 --port=8002 --no-browser --allow-root --notebook-dir=/data --NotebookApp.token=qilan --ServerApp.disable_check_xsrf=True')
22
 
 
74
  </body>
75
  </html>
76
  """
77
+ def compress_folder(folder_path, output_dir):
78
+ try:
79
+ # 确保输出目录存在
80
+ os.makedirs(output_dir, exist_ok=True)
81
+
82
+ # 使用 pytz 获取中国时区的当前时间戳(毫秒级)
83
+ import pytz
84
+ from datetime import datetime
85
+
86
+ # 设置中国时区
87
+ china_tz = pytz.timezone('Asia/Shanghai')
88
+
89
+ # 获取当前中国时间的时间戳
90
+ timestamp = str(int(datetime.now(china_tz).timestamp() * 1000))
91
+ output_path = os.path.join(output_dir, f'{timestamp}.tar.gz')
92
+
93
+ # 获取已存在的压缩包
94
+ existing_archives = glob.glob(os.path.join(output_dir, '*.tar.gz'))
95
+
96
+ # 安全地提取时间戳
97
+ def extract_timestamp(filename):
98
+ # 提取文件名中的数字部分
99
+ match = re.search(r'(\d+)\.tar\.gz$', filename)
100
+ return int(match.group(1)) if match else 0
101
+
102
+ # 如果压缩包数量超过5个,删除最旧的
103
+ if len(existing_archives) >= 5:
104
+ # 按时间戳排序
105
+ existing_archives.sort(key=extract_timestamp)
106
+
107
+ # 删除最旧的压缩包
108
+ oldest_archive = existing_archives[0]
109
+ os.remove(oldest_archive)
110
+ print(f"删除最旧的压缩包:{oldest_archive}")
111
+
112
+ # tar.gz 压缩
113
+ result = subprocess.run(
114
+ ['tar', '-czvf', output_path, folder_path],
115
+ capture_output=True,
116
+ text=True
117
+ )
118
+
119
+ if result.returncode == 0:
120
+ # 计算压缩包大小
121
+ file_size = os.path.getsize(output_path) / 1024 / 1024
122
+
123
+ # 格式化中国时区的时间
124
+ china_time = datetime.now(china_tz)
125
+ formatted_time = china_time.strftime('%Y-%m-%d %H:%M:%S')
126
+
127
+ print(f"压缩成功:{output_path}")
128
+ print(f"压缩大小:{file_size:.2f} MB")
129
+ print(f"压缩时间:{formatted_time}")
130
+
131
+ # 返回压缩包名和大小信息
132
+ return f"{os.path.basename(output_path)} MB:{file_size:.2f} MB TIME:{formatted_time}"
133
+ else:
134
+ print("压缩失败")
135
+ print("错误信息:", result.stderr)
136
+ return None
137
+
138
+ except Exception as e:
139
+ print(f"压缩出错: {e}")
140
+ return None
141
+ def github(type):
142
+ if type == 1:
143
+ os.system(f'rm -rf /data/{HF_REPO}')
144
+ if not os.path.exists(f'/data/{HF_REPO}'):
145
+ git = f"git clone https://{HF_USER1}:{HF_TOKEN1}@huggingface.co/{HF_USER1}/{HF_REPO}"
146
+ print(git)
147
+ os.system(git)
148
+ os.system(f'git config --global user.email "{HF_EMAIL}"')
149
+ os.system(f'git config --global user.name "{HF_USER1}"')
150
+ os.chdir(f'/data/{HF_REPO}')
151
+ if type == 2:
152
+ os.chdir(f'/data/{HF_REPO}')
153
+ print("开始备份上传HF")
154
+ # 清理 LFS 存储
155
+ os.system('git lfs prune')
156
+ # 备份上传仓库
157
+ new_archive_info = compress_folder('/data/dv1', f'/data/{HF_REPO}')
158
+ if new_archive_info:
159
+ new_archive, file_size_info = new_archive_info.split(' MB:')
160
+ os.system(f'git add .')
161
+ os.system(f'git commit -m "{file_size_info}"')
162
+ # os.system('git push -u origin main')
163
+ # 使用强制推送并清理
164
+ os.system('git push -f origin main')
165
+ os.system('git gc --prune=now')
166
+ else:
167
+ print("压缩失败,无法提交")
168
  @app.route('/')
169
  def index():
170
  """根路由,返回 HTML 页面"""
 
179
  data = request.json
180
  name = data.get('name', 'Guest')
181
  return jsonify({"greeting": f"Hello, {name}!"})
182
+ @app.route('/bf', methods=['GET'])
183
+ def hello():
184
+ github(2)
185
+ return jsonify({"message": "OK"})
186
  @app.route('/log', methods=['GET'])
187
  def get_log_content():
188
  try: