-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.py
47 lines (37 loc) · 1.22 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""Helper functions to implement per-language calls"""
import os
import re
from subprocess import check_output, STDOUT
WINDOWS = os.name == "windows"
def implemented(index, extension):
return os.path.isfile(source(index, extension))
def implemented_indices(extension):
pattern = re.compile(r"euler(\d+)" + extension + "$")
for filename in os.listdir("src"):
match = pattern.match(filename)
if match is not None:
index = int(match.group(1))
yield index
def setup(compiled=True):
if compiled:
os.makedirs('target', exist_ok=True)
def clean(compiled=True):
if compiled:
os.rmdir('target')
def source(index, extension):
return os.path.join("src", "euler" + str(index) + extension)
def target(index):
extension = ".exe" if WINDOWS else ""
return os.path.join("target", "euler" + str(index) + extension)
def prepare(index, logger, extension=None, get_argv=None):
src = source(index, extension)
argv = get_argv(index, src)
stdout_err = check_output(argv, stderr=STDOUT)
return stdout_err.decode('utf8')
def run(index, logger, debug=False, get_argv=None):
if get_argv is None:
argv = target(index)
else:
argv = get_argv(index, debug)
stdout_err = check_output(argv, stderr=STDOUT)
return stdout_err.decode('utf8')