grade.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import csv
  2. import os
  3. import pwd
  4. import stat
  5. import shutil
  6. import subprocess
  7. import sys
  8. import tempfile
  9. from decimal import Decimal, InvalidOperation
  10. SCRIPT = "/app/summarize.py"
  11. TASK_INPUT = "/tests/task_input.csv"
  12. TEST_INPUT = "/tests/input.csv"
  13. SUMMARY = "/app/summary.csv"
  14. FILTERED = "/app/over_500.csv"
  15. DETAIL_HEADER = ["日期", "姓名", "类别", "金额"]
  16. SUMMARY_HEADER = ["姓名", "报销总额"]
  17. REWARD_DIR = "/logs/verifier"
  18. REWARD = os.path.join(REWARD_DIR, "reward.txt")
  19. def read_csv(path):
  20. with open(path, encoding="utf-8-sig", newline="") as stream:
  21. return list(csv.reader(stream))
  22. def fail(message):
  23. os.makedirs(REWARD_DIR, exist_ok=True)
  24. with open(REWARD, "w", encoding="utf-8") as reward:
  25. reward.write("0\n")
  26. print(message)
  27. raise SystemExit(0)
  28. def drop_privileges(uid, gid):
  29. os.setgroups([])
  30. os.setgid(gid)
  31. os.setuid(uid)
  32. def run_untrusted_script():
  33. if os.geteuid() != 0:
  34. raise RuntimeError("Verifier must run as root to isolate the submitted program.")
  35. nobody = pwd.getpwnam("nobody")
  36. with tempfile.TemporaryDirectory(prefix="expense-summary-") as temp_dir:
  37. os.chown(temp_dir, nobody.pw_uid, nobody.pw_gid)
  38. os.chmod(temp_dir, 0o700)
  39. staged_input = os.path.join(temp_dir, "input.csv")
  40. summary_path = os.path.join(temp_dir, "summary.csv")
  41. filtered_path = os.path.join(temp_dir, "over_500.csv")
  42. shutil.copyfile(TEST_INPUT, staged_input)
  43. os.chown(staged_input, nobody.pw_uid, nobody.pw_gid)
  44. os.chmod(staged_input, 0o400)
  45. subprocess.run(
  46. [sys.executable, SCRIPT, staged_input, summary_path, filtered_path],
  47. check=True,
  48. timeout=20,
  49. stdout=subprocess.DEVNULL,
  50. stderr=subprocess.DEVNULL,
  51. preexec_fn=lambda: drop_privileges(nobody.pw_uid, nobody.pw_gid),
  52. )
  53. return read_csv(summary_path), read_csv(filtered_path)
  54. def expected_rows(source):
  55. if not source or source[0] != DETAIL_HEADER:
  56. raise ValueError("invalid verifier input")
  57. totals = {}
  58. order = []
  59. filtered = []
  60. for row in source[1:]:
  61. if len(row) != 4:
  62. raise ValueError("invalid verifier input")
  63. _, name, _, raw_amount = row
  64. amount = Decimal(raw_amount)
  65. if name not in totals:
  66. totals[name] = Decimal("0")
  67. order.append(name)
  68. totals[name] += amount
  69. if amount > Decimal("500"):
  70. filtered.append(row)
  71. summary = [SUMMARY_HEADER]
  72. summary.extend([[name, f"{totals[name]:.2f}"] for name in order])
  73. return summary, [DETAIL_HEADER, *filtered]
  74. def check_outputs(input_path, summary_path, filtered_path):
  75. source = read_csv(input_path)
  76. summary, filtered = expected_rows(source)
  77. return outputs_match(summary_path, filtered_path, summary, filtered)
  78. def outputs_match(summary_path, filtered_path, summary, filtered):
  79. if read_csv(summary_path) != summary or read_csv(filtered_path) != filtered:
  80. return False
  81. return True
  82. def main():
  83. try:
  84. if not os.path.isfile(SCRIPT):
  85. fail("Required summary program is missing.")
  86. if not check_outputs(TASK_INPUT, SUMMARY, FILTERED):
  87. fail("Outputs for the provided CSV do not match the task requirements.")
  88. test_source = read_csv(TEST_INPUT)
  89. expected_summary, expected_filtered = expected_rows(test_source)
  90. os.makedirs(REWARD_DIR, exist_ok=True)
  91. os.chmod(REWARD_DIR, 0o700)
  92. with open(REWARD, "w", encoding="utf-8") as reward:
  93. reward.write("0\n")
  94. os.chmod(REWARD, 0o600)
  95. original_mode = stat.S_IMODE(os.stat(TEST_INPUT).st_mode)
  96. os.chmod(TEST_INPUT, 0)
  97. try:
  98. try:
  99. actual_summary, actual_filtered = run_untrusted_script()
  100. except (OSError, subprocess.SubprocessError, RuntimeError):
  101. fail("Summary program could not process the verifier CSV.")
  102. finally:
  103. os.chmod(TEST_INPUT, original_mode)
  104. if (
  105. actual_summary != expected_summary
  106. or actual_filtered != expected_filtered
  107. ):
  108. fail("Summary program output does not meet the task requirements.")
  109. except (OSError, csv.Error, InvalidOperation, ValueError, KeyError):
  110. fail("Required CSV files are missing, unreadable, or invalid.")
  111. with open(REWARD, "w", encoding="utf-8") as reward:
  112. reward.write("1\n")
  113. print("Outputs meet the task requirements.")
  114. if __name__ == "__main__":
  115. main()