fix: 全站 ruff 清零 — 77 errors → 0
Fixes: - F821: install.py site_url 未定义(NameError)、sync_v2.py os 未导入、script_jobs.py LOG f-string - F401: 移除 22 个未使用导入(models/__init__.py、install.py、auth.py 等) - B904: 20 个 except 块 raise 添加 from e/from None - S110: 20 个有意的 try-except-pass 添加 noqa 注释(SSH清理/DDL幂等/WS断连) - B007: 3 个未使用循环变量重命名(dirs→_dirs、server_id→_server_id) - F541: 3 个无占位符 f-string 修正 - F841: auth.py 未使用 ip_address 变量移除 - S105: auth_service.py Redis key prefix 误报 noqa - B023: script_execution_flush.py 循环变量绑定修复 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+10
-11
@@ -17,7 +17,7 @@ from urllib.parse import quote_plus
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
|
||||
from sqlalchemy.ext.asyncio import create_async_engine
|
||||
|
||||
from server.infrastructure.database.engine_compat import patch_aiomysql_do_ping
|
||||
|
||||
@@ -206,7 +206,7 @@ def _configure_guardian(install_dir: str, api_port: str) -> list[str]:
|
||||
log_dir.mkdir(parents=True, exist_ok=True)
|
||||
results.append(f"✓ 日志目录已创建 {log_dir}")
|
||||
except OSError:
|
||||
results.append(f"✗ 日志目录创建失败(需 root 权限)")
|
||||
results.append("✗ 日志目录创建失败(需 root 权限)")
|
||||
|
||||
# 2. Supervisor config
|
||||
if bt_panel:
|
||||
@@ -320,7 +320,6 @@ async def env_check():
|
||||
"""Detect environment readiness — Python, MySQL client, Redis, write permissions."""
|
||||
_reject_post_install_wizard()
|
||||
import sys
|
||||
import importlib
|
||||
|
||||
checks = []
|
||||
|
||||
@@ -406,12 +405,13 @@ async def init_db(req: InitDbRequest):
|
||||
|
||||
db_url = _make_db_url(req)
|
||||
redis_url = _build_redis_url(req)
|
||||
site_url = req.site_url.rstrip("/") if req.site_url else f"http://127.0.0.1:{req.api_port}"
|
||||
|
||||
try:
|
||||
patch_aiomysql_do_ping()
|
||||
engine = create_async_engine(db_url, pool_pre_ping=True, pool_recycle=300)
|
||||
except Exception as e:
|
||||
raise HTTPException(400, f"数据库引擎创建失败: {e}")
|
||||
raise HTTPException(400, f"数据库引擎创建失败: {e}") from e
|
||||
|
||||
try:
|
||||
async with engine.begin() as conn:
|
||||
@@ -435,7 +435,7 @@ async def init_db(req: InitDbRequest):
|
||||
for idx_sql in indexes:
|
||||
try:
|
||||
await conn.execute(text(idx_sql))
|
||||
except Exception:
|
||||
except Exception: # noqa: S110 — DDL idempotent, may already exist
|
||||
pass # Index may already exist
|
||||
|
||||
# Schema migrations for existing databases (safe — no-op if column exists)
|
||||
@@ -445,7 +445,7 @@ async def init_db(req: InitDbRequest):
|
||||
for mig_sql in migrations:
|
||||
try:
|
||||
await conn.execute(text(mig_sql))
|
||||
except Exception:
|
||||
except Exception: # noqa: S110 — DDL idempotent, may already exist
|
||||
pass # Column may already exist
|
||||
|
||||
# Calculate pool_size from max_connections
|
||||
@@ -462,7 +462,7 @@ async def init_db(req: InitDbRequest):
|
||||
secret_key = secrets.token_hex(32)
|
||||
api_key = secrets.token_hex(16)
|
||||
# Generate Fernet-compatible encryption key (32 url-safe base64 bytes)
|
||||
import base64, hashlib
|
||||
import base64
|
||||
encryption_key = base64.urlsafe_b64encode(secrets.token_bytes(32)).decode()
|
||||
|
||||
# Insert settings
|
||||
@@ -491,12 +491,11 @@ async def init_db(req: InitDbRequest):
|
||||
|
||||
except Exception as e:
|
||||
await engine.dispose()
|
||||
raise HTTPException(400, f"数据库初始化失败: {e}")
|
||||
raise HTTPException(400, f"数据库初始化失败: {e}") from e
|
||||
|
||||
await engine.dispose()
|
||||
|
||||
# Write .env
|
||||
site_url = req.site_url.rstrip("/") if req.site_url else f"http://127.0.0.1:{req.api_port}"
|
||||
_write_env(req, secret_key, api_key, encryption_key, pool_size, max_overflow, redis_url, site_url)
|
||||
|
||||
# Write config.json
|
||||
@@ -588,7 +587,7 @@ async def create_admin(req: CreateAdminRequest):
|
||||
|
||||
await engine.dispose()
|
||||
except Exception as e:
|
||||
raise HTTPException(400, f"创建管理员失败: {e}")
|
||||
raise HTTPException(400, f"创建管理员失败: {e}") from e
|
||||
|
||||
# Update config.json with brand
|
||||
if CONFIG_JSON.exists():
|
||||
@@ -597,7 +596,7 @@ async def create_admin(req: CreateAdminRequest):
|
||||
config = json.loads(CONFIG_JSON.read_text())
|
||||
config["app_name"] = req.system_name
|
||||
CONFIG_JSON.write_text(json.dumps(config, indent=2, ensure_ascii=False))
|
||||
except Exception:
|
||||
except Exception: # noqa: S110 — best-effort cleanup
|
||||
pass
|
||||
|
||||
# Update .env with brand
|
||||
|
||||
Reference in New Issue
Block a user