This repository was archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
196 lines (164 loc) · 5.37 KB
/
main.py
File metadata and controls
196 lines (164 loc) · 5.37 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#coding: utf-8
import re
import os
import subprocess
import sys
def echo(string):
sys.stdout.write(str(string));
sys.stdout.write('\n')
sys.stdout.flush()
def error_echo(string):
sys.stderr.write(str(string));
sys.stderr.write('\n')
sys.stderr.flush()
class Code(object):
def __init__(self, code, result, input):
self._code = code
self._result = result
self._input = input
def compile(self):
open('test.cpp', 'w').write(self.code)
subprocess.check_call([
'g++',
'-std=c++11',
'-otest.exe',
'-lpthread',
'test.cpp'])
# subprocess.check_call([
# '/usr/local/gcc-head/bin/g++',
# '-I/usr/local/gcc-head/include/c++/4.9.0',
# '-L/usr/local/gcc-head/lib64',
# '-Wl,-rpath,/usr/local/gcc-head/lib64',
# '-std=c++11',
# '-otest.exe',
# '-lpthread',
# 'test.cpp'])
#subprocess.check_call(
#["/usr/local/llvm-3.3/bin/clang++",
# "-otest.exe",
# "-std=c++11",
# "-stdlib=libc++",
# "-I/usr/local/libcxx-3.3/include/c++/v1",
# "-L/usr/local/libcxx-3.3/lib",
# "-Wl,-rpath,/usr/local/libcxx-3.3/lib",
# "-nostdinc++",
# "-lpthread",
# "test.cpp"])
def run(self):
if self.input is None:
return subprocess.check_output(['./test.exe'], stderr=subprocess.STDOUT)
else:
pin = subprocess.Popen(['./test.exe'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
return pin.communicate(input=self.input)[0]
@property
def code(self):
return self._code
@property
def result(self):
return self._result
@property
def input(self):
return self._input
FENCED_BLOCK_RE = re.compile(
r'((?P<title>^#+[^\n]*?$)[ ]*\n)?(?P<fence>^(?:~{3,}|`{3,}))[ ]*(\{?\.?(?P<lang>[a-zA-Z0-9_+-]*)\}?)?[ ]*\n(?P<code>.*?)(?<=\n)(?P=fence)[ ]*$',
re.MULTILINE|re.DOTALL
)
OUTPUT_RE = re.compile('#+出力')
INPUT_RE = re.compile('#+入力')
# コードブロックがサンプルコードであるかどうかを判断する
def is_sample_code(code, lang):
# 最初の5行以内に #include を含んでたらサンプルコードだと判断する
first_lines = code.split('\n')[:5]
return any(line.find('#include') >= 0 for line in first_lines)
def is_output(code):
return OUTPUT_RE.match(code)
def is_input(code):
return INPUT_RE.match(code)
# コードブロックと出力を探して返す
def get_codes(md):
codes = []
class NextFencedBlock(object):
def __init__(self, md):
self.md = md
def __call__(self):
m = FENCED_BLOCK_RE.search(self.md)
if m:
self.md = self.md[:m.start()] + self.md[m.end():]
return m
next_fenced_block = NextFencedBlock(md)
current_code = None
result = None
input = None
while True:
m = next_fenced_block()
if not m:
if current_code is not None:
codes.append(Code(current_code, result, input))
break
code = m.group('code')
lang = m.group('lang')
if is_sample_code(code, lang):
if current_code is not None:
codes.append(Code(current_code, result, input))
result = None
input = None
current_code = code
continue
title = m.group('title')
if title is not None:
if is_output(title):
result = code
continue
if is_input(title):
input = code
continue
return codes
# 全mdファイル列挙
def all_contents():
for path,dirs,files in os.walk("site"):
for file in files:
if (file[-3:] == '.md'):
yield os.path.join(path, file)
def skip_list():
return open('ignore_list').read().strip().split('\n')
def main():
skips = set(skip_list())
for path in all_contents():
if path in skips:
continue
echo('#' * 32)
echo('checking {}'.format(path))
md = open(path).read()
codes = get_codes(md)
for code in codes:
try:
code.compile()
except Exception, e:
echo('COMPILE ERROR: {}'.format(e))
echo('---- compiled code ----')
echo(code.code)
echo('---- expected result ----')
echo(code.result)
continue
try:
result = code.run()
except Exception, e:
echo('RUNTIME ERROR: {}'.format(e))
echo('---- compiled code ----')
echo(code.code)
echo('---- expected result ----')
echo(code.result)
continue
expected = code.result.strip() if code.result is not None else None
actual = result.strip();
if expected != actual:
echo('---- compiled code ----')
echo(code.code)
echo('---- expected result ----')
echo(expected)
echo('---- actual result ----')
echo(actual)
else:
echo('...OK')
if __name__ == '__main__':
main()