fix: auto-fix code issues (cron)
- 修复重复导入/字段 - 修复异常处理 - 修复PEP8格式问题 - 添加类型注解 自动修复统计: - 修复了1177个格式问题 - 删除了多余的空行 - 清理了行尾空格 - 移除了重复导入和未使用的导入
This commit is contained in:
@@ -15,7 +15,6 @@ PROJECT_PATH = Path("/root/.openclaw/workspace/projects/insightflow")
|
||||
# 修复报告
|
||||
report = {"fixed": [], "manual_review": [], "errors": []}
|
||||
|
||||
|
||||
def find_python_files() -> list[Path]:
|
||||
"""查找所有 Python 文件"""
|
||||
py_files = []
|
||||
@@ -24,7 +23,6 @@ def find_python_files() -> list[Path]:
|
||||
py_files.append(py_file)
|
||||
return py_files
|
||||
|
||||
|
||||
def check_duplicate_imports(content: str, file_path: Path) -> list[dict]:
|
||||
"""检查重复导入"""
|
||||
issues = []
|
||||
@@ -47,7 +45,6 @@ def check_duplicate_imports(content: str, file_path: Path) -> list[dict]:
|
||||
imports[line_stripped] = i
|
||||
return issues
|
||||
|
||||
|
||||
def check_bare_excepts(content: str, file_path: Path) -> list[dict]:
|
||||
"""检查裸异常捕获"""
|
||||
issues = []
|
||||
@@ -60,7 +57,6 @@ def check_bare_excepts(content: str, file_path: Path) -> list[dict]:
|
||||
issues.append({"line": i, "type": "bare_except", "content": stripped})
|
||||
return issues
|
||||
|
||||
|
||||
def check_line_length(content: str, file_path: Path) -> list[dict]:
|
||||
"""检查行长度(PEP8: 79字符,这里放宽到 100)"""
|
||||
issues = []
|
||||
@@ -78,7 +74,6 @@ def check_line_length(content: str, file_path: Path) -> list[dict]:
|
||||
)
|
||||
return issues
|
||||
|
||||
|
||||
def check_unused_imports(content: str, file_path: Path) -> list[dict]:
|
||||
"""检查未使用的导入"""
|
||||
issues = []
|
||||
@@ -108,7 +103,6 @@ def check_unused_imports(content: str, file_path: Path) -> list[dict]:
|
||||
pass
|
||||
return issues
|
||||
|
||||
|
||||
def check_string_formatting(content: str, file_path: Path) -> list[dict]:
|
||||
"""检查混合字符串格式化(建议使用 f-string)"""
|
||||
issues = []
|
||||
@@ -133,7 +127,6 @@ def check_string_formatting(content: str, file_path: Path) -> list[dict]:
|
||||
)
|
||||
return issues
|
||||
|
||||
|
||||
def check_magic_numbers(content: str, file_path: Path) -> list[dict]:
|
||||
"""检查魔法数字"""
|
||||
issues = []
|
||||
@@ -176,7 +169,6 @@ def check_magic_numbers(content: str, file_path: Path) -> list[dict]:
|
||||
)
|
||||
return issues
|
||||
|
||||
|
||||
def check_sql_injection(content: str, file_path: Path) -> list[dict]:
|
||||
"""检查 SQL 注入风险"""
|
||||
issues = []
|
||||
@@ -203,7 +195,6 @@ def check_sql_injection(content: str, file_path: Path) -> list[dict]:
|
||||
)
|
||||
return issues
|
||||
|
||||
|
||||
def check_cors_config(content: str, file_path: Path) -> list[dict]:
|
||||
"""检查 CORS 配置"""
|
||||
issues = []
|
||||
@@ -221,7 +212,6 @@ def check_cors_config(content: str, file_path: Path) -> list[dict]:
|
||||
)
|
||||
return issues
|
||||
|
||||
|
||||
def fix_bare_excepts(content: str) -> str:
|
||||
"""修复裸异常捕获"""
|
||||
lines = content.split("\n")
|
||||
@@ -239,7 +229,6 @@ def fix_bare_excepts(content: str) -> str:
|
||||
|
||||
return "\n".join(new_lines)
|
||||
|
||||
|
||||
def fix_line_length(content: str) -> str:
|
||||
"""修复行长度问题(简单折行)"""
|
||||
lines = content.split("\n")
|
||||
@@ -258,7 +247,6 @@ def fix_line_length(content: str) -> str:
|
||||
|
||||
return "\n".join(new_lines)
|
||||
|
||||
|
||||
def analyze_file(file_path: Path) -> dict:
|
||||
"""分析单个文件"""
|
||||
try:
|
||||
@@ -279,7 +267,6 @@ def analyze_file(file_path: Path) -> dict:
|
||||
|
||||
return issues
|
||||
|
||||
|
||||
def fix_file(file_path: Path, issues: dict) -> bool:
|
||||
"""自动修复文件问题"""
|
||||
try:
|
||||
@@ -299,7 +286,6 @@ def fix_file(file_path: Path, issues: dict) -> bool:
|
||||
report["errors"].append(f"{file_path}: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def generate_report(all_issues: dict) -> str:
|
||||
"""生成修复报告"""
|
||||
lines = []
|
||||
@@ -368,7 +354,6 @@ def generate_report(all_issues: dict) -> str:
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def git_commit_and_push() -> None:
|
||||
"""提交并推送代码"""
|
||||
try:
|
||||
@@ -410,7 +395,6 @@ def git_commit_and_push() -> None:
|
||||
except Exception as e:
|
||||
return f"❌ 错误: {e}"
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""主函数"""
|
||||
print("🔍 开始代码审查...")
|
||||
@@ -448,6 +432,5 @@ def main() -> None:
|
||||
print("\n✅ 代码审查完成!")
|
||||
return report_content
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user