62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""检查 底盘/云台 两份 robot_param.h 中"公共参数段"是否一致。
|
|
|
|
背景:为避免 Simba-OmniInfantry 根目录出现 common 目录,两份板级
|
|
robot_param.h 各自内联了一段"公共参数"(CToC ID/掉线时间/IMU 温控/蜂鸣器等),
|
|
用标记包裹:
|
|
/* ===== 公共参数段(...由 ...check_param_common.py 校验) ===== */
|
|
...
|
|
/* ===== 公共参数段结束 ===== */
|
|
本脚本比较两段文本(忽略行尾空白),不一致时返回非 0,可用于 CI/预构建检查。
|
|
|
|
用法:
|
|
python tools\\check_param_common.py
|
|
"""
|
|
import os
|
|
import re
|
|
import sys
|
|
|
|
BASE = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # .../module_test
|
|
SIMBA = os.path.dirname(BASE) # .../Simba-OmniInfantry
|
|
FILES = [
|
|
os.path.join(SIMBA, '底盘', 'Application', 'robot_param.h'),
|
|
os.path.join(SIMBA, '云台', 'Application', 'robot_param.h'),
|
|
]
|
|
|
|
START = '公共参数段'
|
|
END = '公共参数段结束'
|
|
|
|
|
|
def extract(path):
|
|
# 优先按 utf-8 读取,兼容历史 gbk / gb18030 编码
|
|
for enc in ('utf-8', 'gbk', 'gb18030'):
|
|
try:
|
|
text = open(path, encoding=enc).read()
|
|
break
|
|
except UnicodeDecodeError:
|
|
continue
|
|
else:
|
|
raise SystemExit('无法读取: %s' % path)
|
|
m = re.search(re.escape(START) + r'.*?\*/(.*?)/\* ===== ' + re.escape(END), text, re.S)
|
|
if not m:
|
|
raise SystemExit('找不到公共参数段标记: %s' % path)
|
|
lines = [ln.rstrip() for ln in m.group(1).splitlines() if ln.strip()]
|
|
return lines
|
|
|
|
|
|
def main():
|
|
a = extract(FILES[0])
|
|
b = extract(FILES[1])
|
|
if a == b:
|
|
print('公共参数段一致: %d 行' % len(a))
|
|
return 0
|
|
print('公共参数段不一致!')
|
|
import difflib
|
|
for ln in difflib.unified_diff(a, b, fromfile='底盘', tofile='云台', lineterm=''):
|
|
print(ln)
|
|
return 1
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|