80 lines
2.0 KiB
Python
80 lines
2.0 KiB
Python
#!/usr/bin/env python
|
|
"""
|
|
代码格式化脚本 - 自动格式化代码
|
|
"""
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def run_command(cmd: list, description: str) -> bool:
|
|
"""运行命令"""
|
|
print(f"\n{'='*60}")
|
|
print(f"执行: {description}")
|
|
print(f"命令: {' '.join(cmd)}")
|
|
print('='*60)
|
|
|
|
try:
|
|
result = subprocess.run(cmd, check=True, capture_output=True, text=True)
|
|
print(result.stdout)
|
|
if result.stderr:
|
|
print("STDERR:", result.stderr)
|
|
return True
|
|
except subprocess.CalledProcessError as e:
|
|
print(f"错误: {e}")
|
|
print(f"STDOUT: {e.stdout}")
|
|
print(f"STDERR: {e.stderr}")
|
|
return False
|
|
|
|
|
|
def main():
|
|
"""主函数"""
|
|
project_root = Path(__file__).parent
|
|
|
|
print(f"项目根目录: {project_root}")
|
|
print("开始代码格式化...")
|
|
|
|
success = True
|
|
|
|
# 1. 格式化代码 (black)
|
|
success &= run_command(
|
|
[sys.executable, "-m", "black", str(project_root)],
|
|
"格式化代码 (black)"
|
|
)
|
|
|
|
# 2. 排序导入 (isort)
|
|
success &= run_command(
|
|
[sys.executable, "-m", "isort", str(project_root)],
|
|
"排序导入 (isort)"
|
|
)
|
|
|
|
# 3. 类型检查 (mypy)
|
|
success &= run_command(
|
|
[sys.executable, "-m", "mypy", str(project_root), "--ignore-missing-imports"],
|
|
"类型检查 (mypy)"
|
|
)
|
|
|
|
# 4. 代码检查 (flake8)
|
|
success &= run_command(
|
|
[sys.executable, "-m", "flake8", str(project_root), "--max-line-length=100"],
|
|
"代码检查 (flake8)"
|
|
)
|
|
|
|
# 5. 代码审查 (pylint)
|
|
success &= run_command(
|
|
[sys.executable, "-m", "pylint", str(project_root), "--max-line-length=100"],
|
|
"代码审查 (pylint)"
|
|
)
|
|
|
|
print("\n" + "="*60)
|
|
if success:
|
|
print("✅ 代码格式化完成")
|
|
else:
|
|
print("❌ 部分命令失败,请检查输出")
|
|
print("="*60)
|
|
|
|
return 0 if success else 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main()) |