import os import subprocess import shutil from pathlib import Path # 配置 gcc = "gcc" timeout = None # 设置超时时间,单位:秒 # 设置路径 base_dir = "./h-p/" # 基本路径 asm_dir = Path(base_dir) / "asm" bin_dir = Path(base_dir) / "bin" input_dir = Path(base_dir) / "input" err_dir = Path(base_dir) / "err" # 创建 bin 和 err 目录 bin_dir.mkdir(parents=True, exist_ok=True) err_dir.mkdir(parents=True, exist_ok=True) # 遍历 asm 文件夹,编译 .s 文件 for asm_file in asm_dir.glob("*.s"): executable_name = asm_file.stem executable_path = bin_dir / executable_name # 编译 .s 文件为可执行文件 compile_command = [gcc, asm_file, "lib/sylib.c", "-o", executable_path] try: subprocess.run(compile_command, check=True) except subprocess.CalledProcessError as e: print(f"编译 {asm_file} 失败: {e}") continue # 查找对应的 .in 输入文件 input_file = input_dir / f"{executable_name}.in" input_arg = input_file if input_file.exists() else None # 错误输出重定向文件 stderr_file = err_dir / f"{executable_name}.txt" # 运行可执行文件,处理输入和错误输出 try: with stderr_file.open("w") as stderr: if input_arg: with input_arg.open("r") as stdin: subprocess.run( [executable_path], stdin=stdin, stderr=stderr, timeout=timeout ) else: subprocess.run([executable_path], stderr=stderr, timeout=timeout) except subprocess.TimeoutExpired: print(f"{executable_name} 执行超时,进程已被终止")