Skip to content

Commit 07a751f

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 2119da7 + 1ec7b09 commit 07a751f

File tree

3 files changed

+62
-23
lines changed

3 files changed

+62
-23
lines changed

!src-dist/plib/publish.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def __build_addon(packet: str, addon: str, time_tag: str) -> None:
157157
squishy.write(f'Module "{file_count}" "{rel_dist_file}"\n')
158158

159159
# 调用squishy工具合并构建(使用minify full压缩级别)
160-
os.popen('lua "./!src-dist/tools/react/squish" --minify-level=full').read()
160+
utils.read_popen_output('lua "./!src-dist/tools/react/squish" --minify-level=full')
161161
# 删除临时生成的squishy配置文件
162162
os.remove("squishy")
163163

@@ -276,17 +276,17 @@ def __get_version_info(packet: str, diff_ver: Optional[str] = None) -> Dict[str,
276276
utils.exit_with_message(f"读取Base.lua文件出错:{e}")
277277

278278
# 获取当前最新提交短hash
279-
current_hash: str = os.popen('git log -n 1 --pretty=format:"%h"').read().strip()
279+
current_hash: str = utils.read_popen_output(
280+
'git log -n 1 --pretty=format:"%h"'
281+
).strip()
280282
# 获取所有包含 release 信息的提交记录(以 SUCCESS|<hash>|release: <version> 格式保存)
281-
commit_list: List[str] = (
282-
os.popen('git log --grep release: --pretty=format:"SUCCESS|%h|%s"')
283-
.read()
284-
.split("\n")
285-
)
283+
commit_list: List[str] = utils.read_popen_output(
284+
'git log --grep release: --pretty=format:"SUCCESS|%h|%s"'
285+
).split("\n")
286286
if diff_ver:
287-
extra_commit: str = os.popen(
287+
extra_commit: str = utils.read_popen_output(
288288
f'git log {diff_ver} -n 1 --pretty=format:"SUCCESS|%h|%s"'
289-
).read()
289+
)
290290
commit_list += extra_commit.split("\n")
291291
commit_list = list(filter(lambda x: x and x.startswith("SUCCESS|"), commit_list))
292292

@@ -443,8 +443,7 @@ def pathToModule(path: str) -> str:
443443
print("--------------------------------")
444444
print("文件变更列表:")
445445
filelist: List[str] = (
446-
os.popen(f"git diff {base_hash} HEAD --name-status")
447-
.read()
446+
utils.read_popen_output(f"git diff {base_hash} HEAD --name-status")
448447
.strip()
449448
.split("\n")
450449
)
@@ -519,21 +518,16 @@ def __lint(packet: str, packet_path: str, diff_version: Optional[str] = None) ->
519518
start_hash = version_info.get("previous_hash") or ""
520519
if not start_hash:
521520
# 如果没有上一版本,则获取3个月内最老的commit的hash
522-
start_hash = (
523-
os.popen(
524-
'git log --since="3 months ago" --format="%H" --reverse | head -n 1'
525-
)
526-
.read()
527-
.strip()
528-
)
521+
start_hash = utils.read_popen_output(
522+
'git log --since="3 months ago" --format="%H" --reverse | head -n 1'
523+
).strip()
529524
if not start_hash:
530525
# 如果3个月内没有commit,则获取最新的commit hash
531-
start_hash = os.popen("git rev-parse HEAD").read().strip()
526+
start_hash = utils.read_popen_output("git rev-parse HEAD").strip()
532527

533528
# 获取从起始commit到当前的所有commit信息
534529
commits = (
535-
os.popen(f'git log {start_hash}..HEAD --pretty=format:"%h|%s"')
536-
.read()
530+
utils.read_popen_output(f'git log {start_hash}..HEAD --pretty=format:"%h|%s"')
537531
.strip()
538532
.split("\n")
539533
)

!src-dist/plib/utils.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,49 @@
66
import zlib
77
import sys
88
from typing import NoReturn
9+
import subprocess
10+
11+
12+
def read_popen_output(command: str) -> str:
13+
"""
14+
执行命令并读取输出。
15+
16+
参数:
17+
command: 要执行的命令字符串
18+
返回:
19+
命令输出的字符串
20+
"""
21+
try:
22+
with subprocess.Popen(
23+
command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
24+
) as process:
25+
output, error = process.communicate()
26+
if error:
27+
raise RuntimeError(f"命令执行错误: {error.decode('utf-8')}")
28+
return output.decode("utf-8")
29+
except Exception as e:
30+
exit_with_message(f"执行命令失败: {e}")
31+
32+
33+
def read_file(
34+
file_path: str, primary_encoding: str = "gbk", fallback_encoding: str = "utf-8"
35+
) -> str:
36+
"""
37+
尝试使用主要编码读取文件,如果失败则尝试使用备用编码。
38+
39+
参数:
40+
file_path: 文件路径
41+
primary_encoding: 首选编码(默认GBK)
42+
fallback_encoding: 备用编码(默认UTF-8)
43+
返回:
44+
文件内容字符串
45+
"""
46+
try:
47+
with open(file_path, "r", encoding=primary_encoding) as f:
48+
return f.read()
49+
except UnicodeDecodeError:
50+
with open(file_path, "r", encoding=fallback_encoding) as f:
51+
return f.read()
952

1053

1154
def get_file_crc(file_name: str) -> str:

!src-dist/release.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ def __get_release_commit_list() -> List[Dict[str, object]]:
2727
"""
2828
commit_list: List[Dict[str, object]] = []
2929
# 执行 git log 命令,根据 'release:' 搜索包含版本信息的 commit
30-
output: str = os.popen('git log --grep release: --pretty=format:"%h|%at|%s"').read()
30+
output: str = utils.read_popen_output(
31+
'git log --grep release: --pretty=format:"%h|%at|%s"'
32+
)
3133
for commit in output.split("\n"):
3234
try:
3335
parts = commit.strip().split("|")
@@ -51,7 +53,7 @@ def __get_release_tag_list() -> List[Dict[str, str]]:
5153
返回字典列表,每个字典包括:版本号 version (str) 及完整标签名 name (str)
5254
"""
5355
tag_list: List[Dict[str, str]] = []
54-
tag_output: str = os.popen("git tag -l").read()
56+
tag_output: str = utils.read_popen_output("git tag -l")
5557
for tag in tag_output.split("\n"):
5658
tag = tag.strip()
5759
try:

0 commit comments

Comments
 (0)