Skip to content

Commit e1134fb

Browse files
Xinkai Yidengyh
authored andcommitted
fix: pre-commit 调整文件格式 --story=130029514
1 parent c068dad commit e1134fb

1 file changed

Lines changed: 15 additions & 34 deletions

File tree

bk-plugin-framework/bk_plugin_framework/services/bpf_service/management/commands/sync_apigateway_if_changed.py

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,7 @@ def handle(self, *args, **options):
8585
step_start = time.time()
8686
need_sync = force_sync or current_hash != last_hash
8787
if not need_sync:
88-
self.stdout.write(
89-
self.style.SUCCESS(
90-
"[Sync] API definition unchanged, skip sync to apigateway"
91-
)
92-
)
88+
self.stdout.write(self.style.SUCCESS("[Sync] API definition unchanged, skip sync to apigateway"))
9389
# 仍然获取公钥,确保公钥是最新的
9490
self._fetch_public_key()
9591
step_timings["4. 对比决定是否同步"] = (time.time() - step_start) * 1000
@@ -101,9 +97,7 @@ def handle(self, *args, **options):
10197
if force_sync:
10298
self.stdout.write(self.style.WARNING("[Sync] Force sync enabled"))
10399
else:
104-
self.stdout.write(
105-
self.style.WARNING("[Sync] API definition changed, start syncing...")
106-
)
100+
self.stdout.write(self.style.WARNING("[Sync] API definition changed, start syncing..."))
107101

108102
# 5. 执行同步
109103
step_start = time.time()
@@ -125,9 +119,7 @@ def handle(self, *args, **options):
125119
# 7. 更新哈希值
126120
step_start = time.time()
127121
self._save_sync_hash(current_hash, success=True)
128-
self.stdout.write(
129-
self.style.SUCCESS("[Sync] API gateway sync completed successfully")
130-
)
122+
self.stdout.write(self.style.SUCCESS("[Sync] API gateway sync completed successfully"))
131123
step_timings["7. 更新哈希值"] = (time.time() - step_start) * 1000
132124

133125
# 打印耗时统计
@@ -162,21 +154,15 @@ def _copy_support_files_resources(self):
162154

163155
# 检查 support-files/resources.yaml 是否存在
164156
if not os.path.exists(support_filepath):
165-
self.stderr.write(
166-
self.style.ERROR(f"[Sync] support-files/resources.yaml not found: {support_filepath}")
167-
)
157+
self.stderr.write(self.style.ERROR(f"[Sync] support-files/resources.yaml not found: {support_filepath}"))
168158
raise SystemExit(1)
169159

170160
try:
171161
# 直接复制文件
172162
shutil.copy2(support_filepath, target_filepath)
173-
self.stdout.write(
174-
self.style.SUCCESS(f"[Sync] Copied support-files/resources.yaml to {target_filepath}")
175-
)
163+
self.stdout.write(self.style.SUCCESS(f"[Sync] Copied support-files/resources.yaml to {target_filepath}"))
176164
except Exception as e:
177-
self.stderr.write(
178-
self.style.ERROR(f"[Sync] Failed to copy support-files/resources.yaml: {e}")
179-
)
165+
self.stderr.write(self.style.ERROR(f"[Sync] Failed to copy support-files/resources.yaml: {e}"))
180166
raise SystemExit(1)
181167

182168
def _calculate_resources_hash(self):
@@ -190,26 +176,25 @@ def _calculate_resources_hash(self):
190176
filepath = os.path.join(settings.BASE_DIR, "resources.yaml")
191177
if os.path.exists(filepath):
192178
try:
193-
with open(filepath, "r", encoding="utf-8") as f:
179+
with open(filepath, encoding="utf-8") as f:
194180
data = yaml.safe_load(f)
195181

196182
if data:
197183
# 使用 sort_keys=True 确保输出顺序一致
198184
# 这样即使原始文件中 A,B,C 和 B,C,A 的顺序不同,
199185
# 规范化后的内容也会相同,从而产生相同的 hash
200186
normalized_content = yaml.dump(
201-
data,
202-
default_flow_style=False,
203-
allow_unicode=True,
204-
sort_keys=True # 关键:排序所有 key
187+
data, default_flow_style=False, allow_unicode=True, sort_keys=True # 关键:排序所有 key
205188
)
206189
return hashlib.sha256(normalized_content.encode()).hexdigest()
207190
except Exception as e:
208191
self.stdout.write(
209-
self.style.WARNING(f"[Sync] Failed to normalize resources.yaml for hash: {e}, fallback to raw content hash")
192+
self.style.WARNING(
193+
f"[Sync] Failed to normalize resources.yaml for hash: {e}, fallback to raw content hash"
194+
)
210195
)
211196
# 回退到原始方式
212-
with open(filepath, "r", encoding="utf-8") as f:
197+
with open(filepath, encoding="utf-8") as f:
213198
content = f.read()
214199
return hashlib.sha256(content.encode()).hexdigest()
215200
return ""
@@ -224,9 +209,7 @@ def _get_last_sync_hash(self):
224209
state = APIGatewaySyncState.objects.filter(sync_success=True).first()
225210
return state.api_hash if state else ""
226211
except Exception as e:
227-
self.stdout.write(
228-
self.style.WARNING(f"[Sync] Failed to get last sync hash: {e}")
229-
)
212+
self.stdout.write(self.style.WARNING(f"[Sync] Failed to get last sync hash: {e}"))
230213
return ""
231214

232215
def _save_sync_hash(self, hash_value, success=True):
@@ -242,9 +225,7 @@ def _save_sync_hash(self, hash_value, success=True):
242225
)
243226
self.stdout.write(f"[Sync] Sync state saved (success={success})")
244227
except Exception as e:
245-
self.stdout.write(
246-
self.style.WARNING(f"[Sync] Failed to save sync hash: {e}")
247-
)
228+
self.stdout.write(self.style.WARNING(f"[Sync] Failed to save sync hash: {e}"))
248229

249230
def _fetch_public_key(self):
250231
"""获取 API 网关公钥"""
@@ -253,4 +234,4 @@ def _fetch_public_key(self):
253234
call_command("fetch_apigw_public_key")
254235
except Exception as e:
255236
self.stderr.write(self.style.ERROR(f"run fetch_apigw_public_key fail: {e}"))
256-
raise SystemExit(1)
237+
raise SystemExit(1)

0 commit comments

Comments
 (0)