Skip to content

Commit 45582b9

Browse files
trickstar0301tommy
andauthored
enable compiler config by toml (kyuridenamida#243)
* enable compiler config * fix code style * revise compile-only-when-diff-detected default * add testcases for compiler Co-authored-by: tommy <anti.trickstar@gmail.com>
1 parent 8ff1461 commit 45582b9

File tree

7 files changed

+105
-5
lines changed

7 files changed

+105
-5
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ code_generator_file="~/custom_code_generator.py"
234234
[postprocess]
235235
exec_on_each_problem_dir='clang-format -i ./*.cpp'
236236
exec_on_contest_dir='touch CMakeLists.txt'
237+
[compiler]
238+
compile_command='g++ main.cpp -o main -std=c++17'
239+
compile_only_when_diff_detected=true
237240
[tester]
238241
compile_before_testing=true
239242
compile_only_when_diff_detected=true
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class CompilerConfig:
2+
3+
def __init__(self,
4+
compile_only_when_diff_detected: bool = False,
5+
compile_command: str = None
6+
):
7+
self.compile_only_when_diff_detected = compile_only_when_diff_detected
8+
self.compile_command = compile_command

atcodertools/config/config.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
from atcodertools.config.etc_config import EtcConfig
99
from atcodertools.config.postprocess_config import PostprocessConfig
1010
from atcodertools.config.tester_config import TesterConfig
11+
from atcodertools.config.compiler_config import CompilerConfig
1112

1213

1314
class ConfigType(Enum):
1415
CODESTYLE = "codestyle"
1516
POSTPROCESS = "postprocess"
1617
TESTER = "tester"
1718
ETC = "etc"
19+
COMPILER = "compiler"
1820

1921

2022
def _update_config_dict(target_dic: Dict[str, Any], update_dic: Dict[str, Any]):
@@ -44,7 +46,8 @@ def __init__(self,
4446
code_style_config: CodeStyleConfig = CodeStyleConfig(),
4547
postprocess_config: PostprocessConfig = PostprocessConfig(),
4648
tester_config: TesterConfig = TesterConfig(),
47-
etc_config: EtcConfig = EtcConfig()
49+
etc_config: EtcConfig = EtcConfig(),
50+
compiler_config: CompilerConfig = CompilerConfig()
4851
):
4952
self.code_style_config = code_style_config
5053
self.postprocess_config = postprocess_config
@@ -101,5 +104,13 @@ def load(cls, fp: TextIO, get_config_type, args: Optional[Namespace] = None, lan
101104
save_no_session_cache=args.save_no_session_cache,
102105
skip_existing_problems=args.skip_existing_problems))
103106
result.etc_config = EtcConfig(**etc_config_dic)
107+
if ConfigType.COMPILER in get_config_type:
108+
compiler_config_dic = get_config_dic(
109+
config_dic, ConfigType.COMPILER)
110+
if args:
111+
compiler_config_dic = _update_config_dict(compiler_config_dic,
112+
dict(compile_only_when_diff_detected=args.compile_only_when_diff_detected,
113+
compile_command=args.compile_command))
114+
result.compiler_config = CompilerConfig(**compiler_config_dic)
104115

105116
return result

atcodertools/tools/compiler.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
#!/usr/bin/python3
22
import argparse
3+
from os.path import expanduser
34

45
from atcodertools.executils.run_command import run_command_with_returncode
56
from atcodertools.tools.models.metadata import Metadata
67
from atcodertools.common.language import Language
78
import os
89
import pathlib
10+
from atcodertools.config.config import Config, ConfigType
11+
from atcodertools.tools import get_default_config_path
12+
13+
USER_CONFIG_PATH = os.path.join(expanduser("~"), ".atcodertools.toml")
914

1015

1116
class BadStatusCodeException(Exception):
@@ -56,13 +61,40 @@ def main(prog, args):
5661

5762
parser.add_argument('--compile-only-when-diff-detected',
5863
help='compile only when diff detected [true, false]'
59-
' [Default]: true',
64+
' [Default]: false',
6065
type=bool,
61-
default=False)
66+
default=None)
67+
68+
parser.add_argument("--config",
69+
help="File path to your config file\n{0}{1}".format("[Default (Primary)] {}\n".format(
70+
USER_CONFIG_PATH),
71+
"[Default (Secondary)] {}\n".format(
72+
get_default_config_path())),
73+
default=None)
6274

6375
args = parser.parse_args(args)
76+
if args.config is None:
77+
if os.path.exists(USER_CONFIG_PATH):
78+
args.config = USER_CONFIG_PATH
79+
else:
80+
args.config = get_default_config_path()
6481

6582
metadata = Metadata.load_from("./metadata.json")
66-
force_compile = not args.compile_only_when_diff_detected
83+
lang = metadata.lang
84+
85+
with open(args.config, "r") as f:
86+
config = Config.load(f, {ConfigType.COMPILER}, args, lang.name)
87+
88+
if args.compile_only_when_diff_detected:
89+
force_compile = not args.compile_only_when_diff_detected
90+
else:
91+
force_compile = not config.compiler_config.compile_only_when_diff_detected
92+
93+
if args.compile_command:
94+
compile_command = args.compile_command
95+
else:
96+
compile_command = config.compiler_config.compile_command
97+
if compile_command:
98+
compile_command = lang.get_compile_command("main", compile_command)
6799
compile_main_and_judge_programs(metadata.lang, force_compile=force_compile,
68-
compile_command=args.compile_command)
100+
compile_command=compile_command)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[codestyle]
2+
indent_width = 8
3+
[compiler]
4+
compile_command='g++ main.cpp -o main -std=c++17'
5+
compile_only_when_diff_detected=true

tests/test_compiler.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import os
2+
import unittest
3+
import shutil
4+
import tempfile
5+
6+
from atcodertools.tools import compiler
7+
8+
RESOURCE_DIR = os.path.abspath(os.path.join(
9+
os.path.dirname(os.path.abspath(__file__)),
10+
"./resources/test_tester/"))
11+
12+
13+
class TestTester(unittest.TestCase):
14+
def setUp(self):
15+
self.temp_dir = tempfile.mkdtemp()
16+
17+
def test_compiler(self):
18+
test_dir = os.path.join(self.temp_dir, "test")
19+
shutil.copytree(os.path.join(
20+
RESOURCE_DIR, "test_compiler_and_tester"), test_dir)
21+
os.chdir(test_dir)
22+
compiler.main(
23+
'', ["--compile-command", "touch compile-command-success"])
24+
lst = os.listdir('./')
25+
self.assertTrue("compile-command-success" in lst)
26+
27+
28+
if __name__ == '__main__':
29+
unittest.main()

tests/test_config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,18 @@ def _expect_error_when_init_config(self, **kwargs):
127127
except CodeStyleConfigInitError:
128128
pass
129129

130+
def test_load_config_compiler(self):
131+
os.chdir(RESOURCE_DIR)
132+
133+
with open(os.path.join(RESOURCE_DIR, "compiler_options.toml"), 'r') as f:
134+
config = Config.load(
135+
f, {ConfigType.CODESTYLE, ConfigType.COMPILER})
136+
137+
self.assertEqual(
138+
True, config.compiler_config.compile_only_when_diff_detected)
139+
self.assertEqual('g++ main.cpp -o main -std=c++17',
140+
config.compiler_config.compile_command)
141+
130142

131143
if __name__ == "__main__":
132144
unittest.main()

0 commit comments

Comments
 (0)