Update dev scripts

更新开发辅助脚本:
将所有用到的相对路径存储至统一位置;
将所有检查函数的名称与返回值行为统一;
添加 `check_license_statement()` 函数;
This commit is contained in:
muzing 2023-12-04 21:54:06 +08:00
parent 634174eeeb
commit 72990a0680
4 changed files with 133 additions and 68 deletions

View File

@ -3,18 +3,15 @@
""" """
import subprocess import subprocess
import tomllib
import warnings
from pathlib import Path from pathlib import Path
from dev_scripts.check_funcs import (
check_license_statement,
check_pre_commit,
check_version_num,
)
from dev_scripts.clear_cache import clear_pycache, clear_pyinstaller_dist from dev_scripts.clear_cache import clear_pycache, clear_pyinstaller_dist
from py2exe_gui import Constants from dev_scripts.path_constants import README_FILE_LIST, RESOURCES_PATH, SRC_PATH
project_root = Path("../") # 项目根目录
src_path = project_root / "src" # 源码目录
resources_path = src_path / "py2exe_gui" / "Resources" # 静态资源文件目录
readme_file_list = [project_root / "README.md", project_root / "README_zh.md"]
# TODO 加入日志模块,保存构建日志 # TODO 加入日志模块,保存构建日志
@ -28,8 +25,7 @@ def process_md_images(md_file_list: list[Path]) -> None:
md_uri = "docs/source/images/" md_uri = "docs/source/images/"
github_uri = ( github_uri = (
"https://github.com/muziing/Py2exe-GUI/raw/main" "https://github.com/muziing/Py2exe-GUI/raw/main" + "/docs/source/images/"
+ "/docs/source/images/"
) )
for md_file in md_file_list: for md_file in md_file_list:
@ -46,58 +42,14 @@ def process_md_images(md_file_list: list[Path]) -> None:
# FIXME 会在文件尾部多出来莫名其妙的行 # FIXME 会在文件尾部多出来莫名其妙的行
def check_version_num() -> bool:
"""
检查各部分声明的版本号是否一致 \n
"""
print("正在检查各处版本号是否一致...")
app_constant_version = Constants.app_constants.AppConstant.VERSION
with open(project_root / "pyproject.toml", "rb") as ppj_toml_file:
ppj_dict = tomllib.load(ppj_toml_file)
ppj_version = ppj_dict["tool"]["poetry"]["version"]
if ppj_version == app_constant_version:
print(f"版本号检查完毕,均为 {ppj_version}")
return True
else:
warning_mes = (
"""版本号不一致!\n"""
+ f"""pyproject.toml................{ppj_version}\n"""
+ f"""Constants.AppConstant.........{app_constant_version}\n"""
)
warnings.warn(warning_mes, stacklevel=1)
return False
def pre_commit_check() -> int:
"""
调用已有的 pre-commit 检查工具进行检查 \n
:return: pre-commit 进程返回码
"""
pre_commit_run_cmd = ["pre-commit", "run", "--all-files"]
print("开始进行第一次 pre-commit 检查:")
result_1 = subprocess.run(pre_commit_run_cmd)
if result_1.returncode != 0:
print("开始进行第二次 pre-commit 检查:")
result_2 = subprocess.run(pre_commit_run_cmd)
if result_2.returncode != 0:
warnings.warn("pre-commit进程返回码非0建议检查", stacklevel=1)
return result_2.returncode
else:
return 0
def compile_resources() -> int: def compile_resources() -> int:
""" """
调用 RCC 工具编译静态资源 \n 调用 RCC 工具编译静态资源 \n
:return: rcc 进程返回码 :return: rcc 进程返回码
""" """
compiled_file_path = resources_path / "compiled_resources.py" compiled_file_path = RESOURCES_PATH / "compiled_resources.py"
qrc_file_path = resources_path / "resources.qrc" qrc_file_path = RESOURCES_PATH / "resources.qrc"
cmd = [ cmd = [
"pyside6-rcc", "pyside6-rcc",
"-o", "-o",
@ -114,26 +66,24 @@ def build_py2exe_gui() -> None:
构建项目的总函数 \n 构建项目的总函数 \n
""" """
if check_version_num(): if check_version_num() + check_license_statement() == 0:
# 准备工作 # 准备工作
clear_pyinstaller_dist(src_path) clear_pyinstaller_dist(SRC_PATH)
clear_pycache(src_path) clear_pycache(SRC_PATH)
process_md_images(readme_file_list) process_md_images(README_FILE_LIST)
# compile_resources() # compile_resources()
print(f"pre-commit检查完毕返回码{pre_commit_check()}") print(f"pre-commit检查完毕返回码{check_pre_commit()}")
# 正式构建 # 正式构建
subprocess.run(["poetry", "build"]) # TODO 处理异常与返回值 subprocess.run(["poetry", "build"]) # TODO 处理异常与返回值
# 清理 # 清理
process_md_images(readme_file_list) process_md_images(README_FILE_LIST)
else: else:
print("构建失败,有未通过的检查项") print("构建失败,有未通过的检查项")
if __name__ == "__main__": if __name__ == "__main__":
# check_version_num()
# print(pre_commit_check())
# compile_resources() # compile_resources()
# process_md_images() # process_md_images()
build_py2exe_gui() build_py2exe_gui()

100
dev_scripts/check_funcs.py Normal file
View File

@ -0,0 +1,100 @@
"""
各类检查函数
"""
import subprocess
import tomllib
import warnings
from dev_scripts.path_constants import COMPILED_RESOURCES, PROJECT_ROOT, SRC_PATH
from py2exe_gui import Constants as py2exe_gui_Constants
def check_license_statement() -> int:
"""
检查源代码文件中是否都包含了许可声明
:return: 0-所有源文件都包含许可声明1-存在缺失许可声明的源文件
"""
license_statement = "# Licensed under the GPLv3 License: https://www.gnu.org/licenses/gpl-3.0.html\n"
source_file_list = list(SRC_PATH.glob("**/*.py"))
source_file_list.remove(COMPILED_RESOURCES) # 排除RCC编译工具自动生成的.py文件
check_pass = 0
print("开始检查源码中许可声明情况...")
for file in source_file_list:
with open(file, encoding="utf-8") as f:
if license_statement not in f.read():
warning_mes = f"Source code file {file} lacks a license statement."
warnings.warn(warning_mes, stacklevel=1)
else:
check_pass += 1
if check_pass == len(source_file_list):
print("许可声明检查完毕,所有源码文件都包含许可声明。")
return 0
else:
print("许可声明检查完毕,部分源码文件缺失许可声明,请检查。")
return 1
def check_version_num() -> int:
"""
检查各部分声明的版本号是否一致 \n
:return: 0-各处版本一致1-存在版本不一致情况
"""
print("正在检查各处版本号是否一致...")
app_constant_version = py2exe_gui_Constants.app_constants.AppConstant.VERSION
with open(PROJECT_ROOT / "pyproject.toml", "rb") as ppj_toml_file:
ppj_dict = tomllib.load(ppj_toml_file)
ppj_version = ppj_dict["tool"]["poetry"]["version"]
if ppj_version == app_constant_version:
print(f"版本号检查完毕,均为 {ppj_version}")
return 0
else:
warning_mes = (
"""版本号不一致!\n"""
+ f"""pyproject.toml................{ppj_version}\n"""
+ f"""Constants.AppConstant.........{app_constant_version}\n"""
)
warnings.warn(warning_mes, stacklevel=1)
return 1
def check_pre_commit() -> int:
"""
调用已有的 pre-commit 检查工具进行检查 \n
:return: pre-commit 进程返回码
"""
pre_commit_run_cmd = ["pre-commit", "run", "--all-files"]
print("开始进行第一次 pre-commit 检查...")
result_1 = subprocess.run(pre_commit_run_cmd)
if result_1.returncode != 0:
print("开始进行第二次 pre-commit 检查...")
result_2 = subprocess.run(pre_commit_run_cmd)
if result_2.returncode != 0:
warnings.warn("pre-commit进程返回码非0建议检查", stacklevel=1)
return result_2.returncode
else:
print("pre-commit 检查完成,所有项目通过。")
return 0
def check_requirements() -> int:
"""
检查 requirements.txt 中的依赖是否最新
"""
pass
if __name__ == "__main__":
check_license_statement()
check_version_num()
check_pre_commit()
check_requirements()

View File

@ -2,7 +2,7 @@ import os
from pathlib import Path from pathlib import Path
from shutil import rmtree from shutil import rmtree
src_dir_path = Path("../src") from dev_scripts.path_constants import SRC_PATH
def clear_pyinstaller_dist(src_path: Path) -> None: def clear_pyinstaller_dist(src_path: Path) -> None:
@ -37,5 +37,5 @@ def clear_pycache(src_path: Path) -> None:
if __name__ == "__main__": if __name__ == "__main__":
clear_pyinstaller_dist(src_dir_path) clear_pyinstaller_dist(SRC_PATH)
clear_pycache(src_dir_path) clear_pycache(SRC_PATH)

View File

@ -0,0 +1,15 @@
"""
开发脚本中使用的相对路径常量
所有脚本应以项目根目录为工作目录运行
"""
from pathlib import Path
PROJECT_ROOT = Path("../") # 项目根目录
SRC_PATH = PROJECT_ROOT / "src" # 源码目录
RESOURCES_PATH = SRC_PATH / "py2exe_gui" / "Resources" # 静态资源文件目录
COMPILED_RESOURCES = RESOURCES_PATH / "compiled_resources.py" # 编译静态资源文件
README_FILE_LIST = [
PROJECT_ROOT / "README.md",
PROJECT_ROOT / "README_zh.md",
] # README 文件列表