From 32259fa2172c5902ad8b1c3dbd2ad0c5cd2fd37b Mon Sep 17 00:00:00 2001 From: wangfiox Date: Sat, 17 Aug 2024 20:52:28 +0800 Subject: [PATCH] =?UTF-8?q?python=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + test.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 test.py diff --git a/.gitignore b/.gitignore index 023cc2f..1765a18 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ build !**/expect/output **/*.toml zci.toml +err diff --git a/test.py b/test.py new file mode 100644 index 0000000..73dd63b --- /dev/null +++ b/test.py @@ -0,0 +1,52 @@ +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} 执行超时,进程已被终止")