添加目录结构校验 (#2128)

不允许json文件和目录节点同级
This commit is contained in:
躁动的氨气
2025-10-12 18:47:08 +08:00
committed by GitHub
parent 8eaffd7e8e
commit 28cd47b7a1
2 changed files with 95 additions and 2 deletions

View File

@@ -290,9 +290,42 @@ jobs:
# 使用base64解码文件列表
CHANGED_FILES=$(echo "$CHANGED_FILES_B64" | base64 --decode)
echo "进行地图追踪目录结构校验"
# 地图追踪目录结构校验
echo "🔍 开始地图追踪目录结构校验..."
: > validation_output.log
set +e
python build/validate.py "repo/pathing" --fix 2>&1 | tee -a validation_output.log
PY_EXIT=$?
set -e
# 检查目录结构校验结果
if [ $PY_EXIT -ne 0 ]; then
echo "❌ pathing目录结构校验失败"
VALIDATION_FAILED=true
elif grep -E "❌.*目录结构错误" -q validation_output.log; then
echo "❌ 检测到地图追踪目录结构错误"
VALIDATION_FAILED=true
else
echo "✅ 地图追踪目录结构校验通过"
VALIDATION_FAILED=false
fi
# 如果目录结构校验失败,直接退出
if [ "$VALIDATION_FAILED" = true ]; then
echo "检测到地图追踪目录结构错误,生成校验说明并标记失败"
{
echo "### ❌ 地图追踪目录结构校验失败"
echo "请前往 Actions 查看报错详情(运行日志)。"
} > validation_notes.md
exit 1
fi
# 目录结构校验通过后继续验证修改的JSON文件
echo "PR 触发模式,验证修改的 JSON 文件"
if [ -z "$CHANGED_FILES" ]; then
echo "没有找到修改的 JSON 文件,跳过验证"
echo "没有找到修改的 JSON 文件,跳过JSON内容验证"
exit 0
fi
@@ -300,7 +333,7 @@ jobs:
echo "Python编码设置:"
python -c "import sys; print(sys.getdefaultencoding())"
# 初始化日志文件
# 重新初始化日志文件用于JSON内容校验
: > validation_output.log
VALIDATION_FAILED=false

View File

@@ -641,6 +641,51 @@ def process_json_authors(input_path, verbose=False):
if verbose:
print(f"\n🎉 处理完成:共 {result['total_files']} 个 JSON 文件,修改了 {result['modified_files']}")
# ==================== 目录结构校验 ====================
def validate_directory_structure(dir_path, parent_folders=None):
"""校验目录结构检测JSON文件和目录同级的情况"""
if parent_folders is None:
parent_folders = []
errors = []
try:
items = os.listdir(dir_path)
files = []
directories = []
# 分类文件和目录
for item in items:
item_path = os.path.join(dir_path, item)
if os.path.isfile(item_path):
files.append(item)
elif os.path.isdir(item_path):
directories.append(item)
# 检查是否有JSON文件和目录同级
json_files = [f for f in files if f.lower().endswith('.json')]
if json_files and directories:
relative_path = '/'.join(parent_folders + [os.path.basename(dir_path)]) if parent_folders else os.path.basename(dir_path)
error_msg = f"❌ 目录结构错误: 在目录 \"{relative_path}\" 中发现JSON文件和子目录同级存在。JSON文件: {json_files}, 子目录: {directories}"
errors.append(error_msg)
print(error_msg)
# 递归检查子目录
for dir_name in directories:
sub_dir_path = os.path.join(dir_path, dir_name)
sub_errors = validate_directory_structure(sub_dir_path, parent_folders + [os.path.basename(dir_path)])
errors.extend(sub_errors)
except Exception as error:
relative_path = '/'.join(parent_folders + [os.path.basename(dir_path)]) if parent_folders else os.path.basename(dir_path)
error_msg = f"❌ 无法访问目录 \"{relative_path}\": {str(error)}"
errors.append(error_msg)
print(error_msg)
return errors
# ==================== 主验证逻辑 ====================
def initialize_data(data, file_path):
@@ -840,6 +885,21 @@ def main():
auto_fix = args.fix
all_notices = [] # 初始化 all_notices 变量
# 首先执行目录结构校验
if os.path.isdir(path):
print("🔍 开始目录结构校验...")
structure_errors = validate_directory_structure(path)
if structure_errors:
print("\n❌ 目录结构校验失败,发现以下错误:")
for error in structure_errors:
print(f"- {error}")
print("\n请修复上述目录结构问题后重新提交。")
print("\n目录结构规范说明:")
print("- 不允许JSON文件和子目录在同一个目录下共存")
print("- 建议将JSON文件移动到专门的子目录中")
exit(1)
print("✅ 目录结构校验通过")
if os.path.isfile(path) and path.endswith('.json'):
scan_and_convert(path)
process_json_authors(path)