-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSConstruct
205 lines (167 loc) · 6.66 KB
/
SConstruct
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
197
198
199
200
201
202
203
204
205
# pylint: skip-file
import re
import json
from time import sleep
from SCons.Script import *
from peetcode import Leetcode, readall, writeall, LANGS, Project
EnsurePythonVersion(3, 6)
# Functions
def generate_code(target, source, env):
pro = env['PROJECT']
code = source[0].get_text_contents()
code = lc.playgroundcode(
pro.id, pro.lang, pro.url, code)
writeall(str(target[0]), code)
def extract_code(target, source, env):
pro = env['PROJECT']
code = source[0].get_text_contents()
uploadzone = re.search(
r'{}\s*(.+)\s*{}'.format(pro.lang.beginmark, pro.lang.endmark), code, re.S)
writeall(str(target[0]), uploadzone[1] if uploadzone else code)
def submit_code(target, source, env):
pro = env['PROJECT']
subid = lc.submit(id=pro.id, lang=pro.lang,
code=source[0].get_text_contents())
if not subid:
return "Submit fail"
result = None
for i in range(int(env['MAXSUBRETRY'])):
result = lc.check(subid)
if result:
break
print(env.subst('Waiting result %d/$MAXSUBRETRY') % (i + 1))
sleep(1)
if not result:
return 'Timeout'
print('Test result:', result['status_msg'])
print('Passed: {}/{}'.format(
result['total_correct'], result['total_testcases']))
status = result['status_code']
if status == 10:
return
elif status == 11:
print('For input: {}, Expected: {}, Got: {}'.format(
result['input'], result['expected_output'], result['code_output']))
writeall(os.path.join(pro.testdir, str(subid) + '.in'),
result['input'])
writeall(os.path.join(pro.testdir, str(subid) + '.exp'),
result['expected_output'])
elif status == 15:
print('Got runtime error `{}` with last input: {}'.format(
result['runtime_error'], result['last_testcase']))
writeall(os.path.join(pro.testdir, str(subid) + '.in'),
result['last_testcase'])
else:
for k, v in result.items():
print('{} = {}'.format(k, v))
return result['status_msg']
def create_project(target, source, env):
proj: Project = env['PROJECT']
pro = lc.getproblem(proj.url)
dirpath = proj.dir
if os.path.exists(dirpath):
return "problem already exists!"
os.mkdir(dirpath, 0o777)
defaultcode = next((
lang['defaultCode'] for lang in json.loads(pro['data']['question']['codeDefinition'])
if lang['value'] == proj.lang.name
))
content = pro['data']['question']['content']
content = re.sub(r'(\r\n)+', '\n', content)
defaultcode = proj.lang.beginmark + '\n' + \
re.sub(r'(\r\n)+', '\n', defaultcode) + '\n' + proj.lang.endmark
template = env.File('$TEMPLATE${PROJECT.lang.ext}', 'templates')
print(template)
if template.exists():
print('applying template')
defaultcode = re.sub(r"\$DEFAULTCODE", defaultcode,
template.get_text_contents())
writeall(proj.srcpath, defaultcode)
writeall(os.path.join(dirpath, 'README.html'), content)
os.mkdir(proj.testdir, 0o777)
writeall(os.path.join(proj.testdir, 'sample.in'),
pro['data']['question']['sampleTestCase'])
# Builders
test = Builder(action=Action('time -f "Time: %Us" $PROGRAM < $SOURCE | tee $TARGET | echo "Output: $$(cat -)"',
'Testing $SOURCE ...'),
suffix='.out', src_suffix='.in')
debug = Builder(action=Action('$GDB $PROGRAM -ex "set args < $SOURCE"',
'Lauching GDB ...'), src_suffix='.in')
diff = Builder(action=Action('diff -w $SOURCE ${SOURCE.base}.out && echo Accepted',
'Comparing $SOURCE and ${SOURCE.base}.out ...'),
suffix='.diff', src_suffix='.exp')
# Actions
login = Action(lambda target, source, env: lc.login(
env['user'], env['pass']) and None, "Login Leetcode with username = $user")
submit = Action(submit_code, "Submitting $SOURCE ...")
extract = Action(extract_code, "Extracting upload zone in $SOURCE ..")
lc = Leetcode()
# Variables
vars = Variables()
vars.Add('user', 'The username of your Leetcode account')
vars.Add('pass', 'The password of your Leetcode account')
vars.Add(EnumVariable('lang', 'The prject language', 'cpp',
allowed_values=LANGS.keys()))
vars.Add('id', 'The problem id', 'PROBLEMID')
# Environment
env = Environment(CXXFLAGS='--std=c++11 -g', variables=vars)
env.Append(BUILDERS={
'Test': test,
'Diff': diff,
'Debug': debug
})
env.SetDefault(GDB='gdb', TEMPLATE='template', MAXSUBRETRY=10)
# env.Help(vars.GenerateHelpText(env))
# Cache Update
meta = env.Command('lc.json', None, lambda target, source, env: lc.update())
env.AlwaysBuild(meta)
env.Alias('update', meta)
# Login
cookies = env.Command('.cookies', None, login)
Alias('login', cookies)
AlwaysBuild(Alias('login'))
# Problems
for p in lc.allproblems.values():
proj = env.Clone(PROJECT=Project(p, env['lang']))
create = proj.Command('${PROJECT.id}-create', None, Action(
create_project, 'Creating new ${PROJECT.lang} project "${PROJECT.title}" ...'))
proj.Pseudo(create)
# Projects
for p in lc.allprojects:
proj = env.Clone(PROJECT=p, id=p.id, lang=p.lang)
src = proj.File(p.srcpath)
# Generate compilable code
gen = proj.Command(p.genpath, src, Action(
generate_code, "Generating compilable code for $SOURCE .."))
proj.Precious(gen)
proj.Alias('$id-$lang-gen', gen)
proj.Alias('$id-gen', gen)
# Extract uploadable code
upload = proj.Command(p.uploadpath, src, extract)
proj.Alias('$id-$lang-upload', upload)
proj.Alias('$id-upload'.format(p.id), upload)
program = proj.Program(p.execpath, gen)
proj.Alias('$id-$lang', program)
proj.Alias('$id'.format(p.id), program)
testdir = proj.Dir('test', p.dir)
testin = testdir.glob('*.in')
testexp = testdir.glob('*.exp')
testout = [proj.Test(t, PROGRAM=program) for t in testin]
debug = [proj.Debug(
'$id-$lang-${SOURCE.filebase}-debug', t, PROGRAM=program) for t in testin]
testdiff = [proj.Diff(t) for t in testexp]
proj.AlwaysBuild(testout)
proj.Depends(testout, program)
proj.Depends(testdiff, testout)
proj.Depends(debug, testout)
proj.Pseudo(testdiff)
proj.Pseudo(debug)
proj.Alias('$id-$lang-test', [testout, testdiff])
proj.Alias('$id-test', [testout, testdiff])
proj_submit = proj.Command('$id-$lang-submit', upload, submit)
proj.Depends(proj_submit, cookies)
proj.Pseudo(proj_submit)
Alias('create', env.Alias('$id-create'))
Alias('test', env.Alias('$id-$lang-test'))
Alias('submit', env.Alias('$id-$lang-submit'))
Default(env.Alias('$id-$lang'))