AI_Homework_Correcttion/source/check_ymls.py

70 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import yaml
import openpyxl
from grade_assignments import ASSIGNMENT_DIR, find_assignment_yml, EXCEL_PATH
def check_all_ymls():
"""检查Excel中记录的YML文件是否存在且有效"""
print(f"正在检查Excel: {EXCEL_PATH}")
# 读取Excel获取学生名单
try:
wb = openpyxl.load_workbook(EXCEL_PATH)
ws = wb.active
# 获取标题行确定列索引
headers = [cell.value for cell in ws[1]]
name_col = headers.index('填写人')
students = []
for row in ws.iter_rows(min_row=2, values_only=True):
if len(row) > name_col and row[name_col]: # 确保有足够列且姓名不为空
students.append(row[name_col])
print(f"找到 {len(students)} 条学生记录")
except Exception as e:
print(f"读取Excel失败: {str(e)}")
return False
# 检查每个学生的YML文件
missing_files = []
invalid_files = []
valid_count = 0
for student in students:
yml_path = find_assignment_yml(student)
if not yml_path:
missing_files.append(student)
continue
try:
with open(yml_path, 'r', encoding='utf-8') as f:
yaml.safe_load(f)
valid_count += 1
print(f"[✓] {os.path.basename(yml_path)} - 有效")
except Exception as e:
invalid_files.append(student)
print(f"[×] {os.path.basename(yml_path)} - 无效: {str(e)}")
# 输出汇总报告
print("\n检查完成:")
print(f"- 有效文件: {valid_count}/{len(students)}")
if missing_files:
print("\n以下学生缺少YML文件:")
for name in missing_files:
print(f" - {name}")
if invalid_files:
print("\n以下学生的YML文件无效:")
for name in invalid_files:
print(f" - {name}")
return not (missing_files or invalid_files)
if __name__ == "__main__":
if check_all_ymls():
print("\n所有YML文件检查通过")
else:
print("\n发现无效YML文件请修复后再运行")