129 lines
3.0 KiB
Python
129 lines
3.0 KiB
Python
#!/usr/bin/env python
|
|
"""
|
|
开发工具脚本 - 提供常用开发命令
|
|
"""
|
|
import argparse
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def run_command(cmd: list, cwd: Path = None) -> int:
|
|
"""运行命令"""
|
|
print(f"执行: {' '.join(cmd)}")
|
|
try:
|
|
return subprocess.run(cmd, cwd=cwd).returncode
|
|
except Exception as e:
|
|
print(f"错误: {e}")
|
|
return 1
|
|
|
|
|
|
def format_code() -> int:
|
|
"""格式化代码"""
|
|
from scripts.format_code import main
|
|
return main()
|
|
|
|
|
|
def run_tests(coverage: bool = False) -> int:
|
|
"""运行测试"""
|
|
from scripts.run_tests import run_tests
|
|
return run_tests(coverage=coverage)
|
|
|
|
|
|
def lint_code() -> int:
|
|
"""代码检查"""
|
|
cmds = [
|
|
[sys.executable, "-m", "flake8", ".", "--max-line-length=100"],
|
|
[sys.executable, "-m", "pylint", ".", "--max-line-length=100", "--disable=C0111"],
|
|
]
|
|
|
|
for cmd in cmds:
|
|
if run_command(cmd):
|
|
print(f"命令失败: {' '.join(cmd)}")
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
def type_check() -> int:
|
|
"""类型检查"""
|
|
return run_command([
|
|
sys.executable, "-m", "mypy", ".", "--ignore-missing-imports"
|
|
])
|
|
|
|
|
|
def build_app() -> int:
|
|
"""打包应用"""
|
|
spec_file = Path("ArticleReplace.spec")
|
|
if spec_file.exists():
|
|
return run_command([
|
|
sys.executable, "-m", "PyInstaller",
|
|
str(spec_file),
|
|
"--clean"
|
|
])
|
|
else:
|
|
print(f"Spec文件不存在: {spec_file}")
|
|
return 1
|
|
|
|
|
|
def clean_build() -> int:
|
|
"""清理构建文件"""
|
|
dirs_to_remove = ["build", "dist", "__pycache__"]
|
|
for d in dirs_to_remove:
|
|
import shutil
|
|
if Path(d).exists():
|
|
shutil.rmtree(d)
|
|
print(f"已删除: {d}")
|
|
|
|
import glob
|
|
for f in glob.glob("*.spec"):
|
|
Path(f).unlink()
|
|
print(f"已删除: {f}")
|
|
|
|
return 0
|
|
|
|
|
|
def main():
|
|
"""主函数"""
|
|
parser = argparse.ArgumentParser(description="开发工具")
|
|
subparsers = parser.add_subparsers(dest="command", help="可用命令")
|
|
|
|
# format
|
|
subparsers.add_parser("format", help="格式化代码")
|
|
|
|
# test
|
|
test_parser = subparsers.add_parser("test", help="运行测试")
|
|
test_parser.add_argument("--coverage", "-c", action="store_true", help="生成覆盖率报告")
|
|
|
|
# lint
|
|
subparsers.add_parser("lint", help="代码检查")
|
|
|
|
# typecheck
|
|
subparsers.add_parser("typecheck", help="类型检查")
|
|
|
|
# build
|
|
subparsers.add_parser("build", help="打包应用")
|
|
|
|
# clean
|
|
subparsers.add_parser("clean", help="清理构建文件")
|
|
|
|
args = parser.parse_args()
|
|
|
|
if not args.command:
|
|
parser.print_help()
|
|
return 0
|
|
|
|
commands = {
|
|
"format": format_code,
|
|
"test": lambda: run_tests(coverage=getattr(args, 'coverage', False)),
|
|
"lint": lint_code,
|
|
"typecheck": type_check,
|
|
"build": build_app,
|
|
"clean": clean_build,
|
|
}
|
|
|
|
return commands[args.command]()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main()) |