Skip to content

Commit 6f8cd95

Browse files
authored
Merge pull request #7 from muziing/dev
Version `0.1.11`
2 parents 634174e + 4c9d927 commit 6f8cd95

12 files changed

Lines changed: 236 additions & 150 deletions

dev_scripts/build.py

Lines changed: 15 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@
33
"""
44

55
import subprocess
6-
import tomllib
7-
import warnings
86
from pathlib import Path
97

8+
from dev_scripts.check_funcs import (
9+
check_license_statement,
10+
check_pre_commit,
11+
check_version_num,
12+
)
1013
from dev_scripts.clear_cache import clear_pycache, clear_pyinstaller_dist
11-
from py2exe_gui import Constants
12-
13-
project_root = Path("../") # 项目根目录
14-
src_path = project_root / "src" # 源码目录
15-
resources_path = src_path / "py2exe_gui" / "Resources" # 静态资源文件目录
16-
readme_file_list = [project_root / "README.md", project_root / "README_zh.md"]
17-
14+
from dev_scripts.path_constants import README_FILE_LIST, RESOURCES_PATH, SRC_PATH
1815

1916
# TODO 加入日志模块,保存构建日志
2017

@@ -28,8 +25,7 @@ def process_md_images(md_file_list: list[Path]) -> None:
2825

2926
md_uri = "docs/source/images/"
3027
github_uri = (
31-
"https://github.com/muziing/Py2exe-GUI/raw/main"
32-
+ "/docs/source/images/"
28+
"https://github.com/muziing/Py2exe-GUI/raw/main" + "/docs/source/images/"
3329
)
3430

3531
for md_file in md_file_list:
@@ -46,58 +42,14 @@ def process_md_images(md_file_list: list[Path]) -> None:
4642
# FIXME 会在文件尾部多出来莫名其妙的行
4743

4844

49-
def check_version_num() -> bool:
50-
"""
51-
检查各部分声明的版本号是否一致 \n
52-
"""
53-
54-
print("正在检查各处版本号是否一致...")
55-
56-
app_constant_version = Constants.app_constants.AppConstant.VERSION
57-
with open(project_root / "pyproject.toml", "rb") as ppj_toml_file:
58-
ppj_dict = tomllib.load(ppj_toml_file)
59-
ppj_version = ppj_dict["tool"]["poetry"]["version"]
60-
61-
if ppj_version == app_constant_version:
62-
print(f"版本号检查完毕,均为 {ppj_version} 。")
63-
return True
64-
else:
65-
warning_mes = (
66-
"""版本号不一致!\n"""
67-
+ f"""pyproject.toml................{ppj_version}\n"""
68-
+ f"""Constants.AppConstant.........{app_constant_version}\n"""
69-
)
70-
warnings.warn(warning_mes, stacklevel=1)
71-
return False
72-
73-
74-
def pre_commit_check() -> int:
75-
"""
76-
调用已有的 pre-commit 检查工具进行检查 \n
77-
:return: pre-commit 进程返回码
78-
"""
79-
80-
pre_commit_run_cmd = ["pre-commit", "run", "--all-files"]
81-
print("开始进行第一次 pre-commit 检查:")
82-
result_1 = subprocess.run(pre_commit_run_cmd)
83-
if result_1.returncode != 0:
84-
print("开始进行第二次 pre-commit 检查:")
85-
result_2 = subprocess.run(pre_commit_run_cmd)
86-
if result_2.returncode != 0:
87-
warnings.warn("pre-commit进程返回码非0,建议检查", stacklevel=1)
88-
return result_2.returncode
89-
else:
90-
return 0
91-
92-
9345
def compile_resources() -> int:
9446
"""
9547
调用 RCC 工具编译静态资源 \n
9648
:return: rcc 进程返回码
9749
"""
9850

99-
compiled_file_path = resources_path / "compiled_resources.py"
100-
qrc_file_path = resources_path / "resources.qrc"
51+
compiled_file_path = RESOURCES_PATH / "compiled_resources.py"
52+
qrc_file_path = RESOURCES_PATH / "resources.qrc"
10153
cmd = [
10254
"pyside6-rcc",
10355
"-o",
@@ -114,26 +66,24 @@ def build_py2exe_gui() -> None:
11466
构建项目的总函数 \n
11567
"""
11668

117-
if check_version_num():
69+
if check_version_num() + check_license_statement() == 0:
11870
# 准备工作
119-
clear_pyinstaller_dist(src_path)
120-
clear_pycache(src_path)
121-
process_md_images(readme_file_list)
71+
clear_pyinstaller_dist(SRC_PATH)
72+
clear_pycache(SRC_PATH)
73+
process_md_images(README_FILE_LIST)
12274
# compile_resources()
123-
print(f"pre-commit检查完毕,返回码:{pre_commit_check()}。")
75+
print(f"pre-commit检查完毕,返回码:{check_pre_commit()}。")
12476

12577
# 正式构建
12678
subprocess.run(["poetry", "build"]) # TODO 处理异常与返回值
12779

12880
# 清理
129-
process_md_images(readme_file_list)
81+
process_md_images(README_FILE_LIST)
13082
else:
13183
print("构建失败,有未通过的检查项")
13284

13385

13486
if __name__ == "__main__":
135-
# check_version_num()
136-
# print(pre_commit_check())
13787
# compile_resources()
13888
# process_md_images()
13989
build_py2exe_gui()

dev_scripts/check_funcs.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""
2+
各类检查函数
3+
"""
4+
5+
import subprocess
6+
import tomllib
7+
import warnings
8+
9+
from dev_scripts.path_constants import COMPILED_RESOURCES, PROJECT_ROOT, SRC_PATH
10+
from py2exe_gui import Constants as py2exe_gui_Constants
11+
12+
13+
def check_license_statement() -> int:
14+
"""
15+
检查源代码文件中是否都包含了许可声明
16+
:return: 0-所有源文件都包含许可声明;1-存在缺失许可声明的源文件
17+
"""
18+
19+
license_statement = "# Licensed under the GPLv3 License: https://www.gnu.org/licenses/gpl-3.0.html\n"
20+
source_file_list = list(SRC_PATH.glob("**/*.py"))
21+
source_file_list.remove(COMPILED_RESOURCES) # 排除RCC编译工具自动生成的.py文件
22+
check_pass = 0
23+
24+
print("开始检查源码中许可声明情况...")
25+
26+
for file in source_file_list:
27+
with open(file, encoding="utf-8") as f:
28+
if license_statement not in f.read():
29+
warning_mes = f"Source code file {file} lacks a license statement."
30+
warnings.warn(warning_mes, stacklevel=1)
31+
else:
32+
check_pass += 1
33+
34+
if check_pass == len(source_file_list):
35+
print("许可声明检查完毕,所有源码文件都包含许可声明。")
36+
return 0
37+
else:
38+
print("许可声明检查完毕,部分源码文件缺失许可声明,请检查。")
39+
return 1
40+
41+
42+
def check_version_num() -> int:
43+
"""
44+
检查各部分声明的版本号是否一致 \n
45+
:return: 0-各处版本一致;1-存在版本不一致情况
46+
"""
47+
48+
print("正在检查各处版本号是否一致...")
49+
50+
app_constant_version = py2exe_gui_Constants.app_constants.AppConstant.VERSION
51+
with open(PROJECT_ROOT / "pyproject.toml", "rb") as ppj_toml_file:
52+
ppj_dict = tomllib.load(ppj_toml_file)
53+
ppj_version = ppj_dict["tool"]["poetry"]["version"]
54+
55+
if ppj_version == app_constant_version:
56+
print(f"版本号检查完毕,均为 {ppj_version}。")
57+
return 0
58+
else:
59+
warning_mes = (
60+
"""版本号不一致!\n"""
61+
+ f"""pyproject.toml................{ppj_version}\n"""
62+
+ f"""Constants.AppConstant.........{app_constant_version}\n"""
63+
)
64+
warnings.warn(warning_mes, stacklevel=1)
65+
return 1
66+
67+
68+
def check_pre_commit() -> int:
69+
"""
70+
调用已有的 pre-commit 检查工具进行检查 \n
71+
:return: pre-commit 进程返回码
72+
"""
73+
74+
pre_commit_run_cmd = ["pre-commit", "run", "--all-files"]
75+
print("开始进行第一次 pre-commit 检查...")
76+
result_1 = subprocess.run(pre_commit_run_cmd)
77+
if result_1.returncode != 0:
78+
print("开始进行第二次 pre-commit 检查...")
79+
result_2 = subprocess.run(pre_commit_run_cmd)
80+
if result_2.returncode != 0:
81+
warnings.warn("pre-commit进程返回码非0,建议检查", stacklevel=1)
82+
return result_2.returncode
83+
else:
84+
print("pre-commit 检查完成,所有项目通过。")
85+
return 0
86+
87+
88+
def check_requirements() -> int:
89+
"""
90+
检查 requirements.txt 中的依赖是否最新
91+
"""
92+
93+
pass
94+
95+
96+
if __name__ == "__main__":
97+
check_license_statement()
98+
check_version_num()
99+
check_pre_commit()
100+
check_requirements()

dev_scripts/clear_cache.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from pathlib import Path
33
from shutil import rmtree
44

5-
src_dir_path = Path("../src")
5+
from dev_scripts.path_constants import SRC_PATH
66

77

88
def clear_pyinstaller_dist(src_path: Path) -> None:
@@ -37,5 +37,5 @@ def clear_pycache(src_path: Path) -> None:
3737

3838

3939
if __name__ == "__main__":
40-
clear_pyinstaller_dist(src_dir_path)
41-
clear_pycache(src_dir_path)
40+
clear_pyinstaller_dist(SRC_PATH)
41+
clear_pycache(SRC_PATH)

dev_scripts/path_constants.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
开发脚本中使用的相对路径常量
3+
所有脚本应以项目根目录为工作目录运行
4+
"""
5+
6+
from pathlib import Path
7+
8+
PROJECT_ROOT = Path("../") # 项目根目录
9+
SRC_PATH = PROJECT_ROOT / "src" # 源码目录
10+
RESOURCES_PATH = SRC_PATH / "py2exe_gui" / "Resources" # 静态资源文件目录
11+
COMPILED_RESOURCES = RESOURCES_PATH / "compiled_resources.py" # 编译静态资源文件
12+
README_FILE_LIST = [
13+
PROJECT_ROOT / "README.md",
14+
PROJECT_ROOT / "README_zh.md",
15+
] # README 文件列表

0 commit comments

Comments
 (0)