Skip to content

Commit

Permalink
Merge pull request #5 from CCNU-ACM-Official/dev
Browse files Browse the repository at this point in the history
[fix] format
  • Loading branch information
jixxiong authored Apr 13, 2024
2 parents 28c8e71 + f5f6f40 commit e302fbb
Show file tree
Hide file tree
Showing 7 changed files with 303 additions and 153 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
.idea
__pycache__
*.log
*.egg-info
dist
build
.ipynb_checkpoints
examples/exec
examples/tmp
examples/zip
.venv
venv
6 changes: 6 additions & 0 deletions ccnuacm_datamocker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ def set_seed(seed_):
def set_work_dir(work_dir):
from .common import context
context().work_dir = work_dir


def set_compiler(compiler_path):
from .common import context
context().CXX = compiler_path
context().logger.info(f"Setting compiler path to `{compiler_path}`")
16 changes: 16 additions & 0 deletions ccnuacm_datamocker/common/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .random_generator import RandomGenerator
import shutil
import os
import subprocess

__all__ = ["context"]
__author__ = "JixiangXiong"
Expand Down Expand Up @@ -89,6 +90,21 @@ def exec_dir(self):
os.makedirs(exec_dir_)
return exec_dir_

@property
def CXX(self):
return self._CXX

@CXX.setter
def CXX(self, path):
context().logger.info(f"Setting CXX to `{path}`.")
result = subprocess.run([path, "-dumpversion"], capture_output=True)
if result.returncode != 0:
raise ValueError(
f'Invalid compiler path: `{path}`. Failed to get version. Errmsg:\n{result.stdout.decode("utf-8")}\n'
f'{result.stderr.decode("utf-8")}')
context().logger.info(f'compiler version: {result.stdout.decode("utf-8").strip()}')
self._CXX = path

def clear_tmp(self):
self.clear_dir(self.tmp_dir)

Expand Down
18 changes: 13 additions & 5 deletions ccnuacm_datamocker/data_model/helper/dataset.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import subprocess

from ccnuacm_datamocker.data_model.base import DataModel
import os
from ccnuacm_datamocker.common import context
Expand Down Expand Up @@ -56,7 +58,7 @@ def add(self, *case):

def run(self):
std_path = self.std_path
exe_path = os.path.join(context().exec_dir, self._zip_name)
exe_path = os.path.join(context().exec_dir, self._zip_name + ".exe")
out_dir = self._dir
cxx = context().get_compile_options("CXX")
cxxflags = context().get_compile_options("CXXFLAGS")
Expand All @@ -66,8 +68,12 @@ def run(self):
os.remove(exe_path)
context().logger.info(
f"Compiling `{std_path}` with command: `{cxx} -o {exe_path} {cxxflags} {ldflags} {std_path}`.")
if os.system(f"{cxx} -o {exe_path} {cxxflags} {ldflags} {std_path}"):
raise ValueError(f"Failed to compile `{std_path}`.")
compile_result = subprocess.run(f"{cxx} -o {exe_path} -O2 -std=c++17 {cxxflags} {ldflags} {std_path}",
capture_output=True)
if compile_result.returncode:
raise ValueError(
f'Failed to compile `{std_path}`.\nerrmsg:\n{compile_result.stdout.decode("utf-8")}\n'
f'{compile_result.stderr.decode("utf-8")}')
context().logger.info(f"Compiling `{std_path}` succeed.")

context().clear_dir(out_dir)
Expand All @@ -81,8 +87,10 @@ def run(self):
f.write(str(case))
context().logger.info(f"Generating infile `{in_file}` succeed.")
context().logger.info(f"Generating outfile using command: `{exe_path} < {in_file} > {out_file}`.")
if os.system(f"{exe_path} < {in_file} > {out_file}"):
raise ValueError(f"Failed to exec command: `{exe_path} < {in_file} > {out_file}`.")
exec_result = subprocess.run(f"{exe_path} < {in_file} > {out_file}", capture_output=True)
if exec_result.returncode:
raise ValueError(f"Failed to exec command: `{exe_path} < {in_file} > {out_file}`.\nerrmsg:\n"
f'{exec_result.stdout.decode("utf-8")}\n{exec_result.stderr.decode("utf-8")}')
context().logger.info(f"Generating outfile `{out_file}` succeed.")
context().logger.info(f"All cases generated. Start zipping `{self._zip_name}.zip`...")

Expand Down
Loading

0 comments on commit e302fbb

Please sign in to comment.