forked from hexuustc/fpgaol_compiling_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.py
70 lines (64 loc) · 2.59 KB
/
compile.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
import time
import subprocess
import logging
import zipfile
logging.basicConfig(
format='%(asctime)s line:%(lineno)s, %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
compiler_timeout = 240
caasw_exec = os.path.join(os.getcwd(), "caas-wizard/caasw.py")
def try_compile(job, callback):
returnval = -100
try:
returnval = compile(job)
except Exception as e:
logger.warning('try_compile: job %s got exception %s!' % (str(job.id), str(e)))
job.succeeded = returnval == 0
job.timeouted = returnval == -1
job.killed = returnval == 233
callback(job.id)
def compile(job):
logger.info('\n Start compiling with %s, %s, %s',
job.jobs_dir, job.id, job.filenames)
work_root = os.path.join(job.jobs_dir, str(job.id))
os.mkdir(os.path.join(work_root, 'build'))
# Unzip the zipfile if there is one
# Otherwise, it's website uploaded with .caas.conf in the file list
hasZip = 0
for z in job.filenames:
if '.zip' in z:
try:
with zipfile.ZipFile(os.path.join(work_root, z), 'r') as zip_ref:
zip_ref.extractall(work_root)
hasZip = 1
except zipfile.BadZipFile:
print('BadZipFile!')
return -1
# Run caasw for Makefile gen: we don't let user upload Makefile
# Redirect output to build/top.log, so even if this fails (especially with Giturl),
# user still get a log to read
ret = -1
with open(os.path.join(work_root, "build", "top.log"), "w") as logf:
ret = subprocess.call([caasw_exec, "mfgen", ".caas.conf", ".", "--overwrite", "--clone"], cwd=work_root, stdout=logf, stderr=logf)
if ret != 0:
print('Error generating Makefile from project!')
return -1
# Run compilation within the unzipped directory
# Just a reminder here:
# API uploaded jobs:
# run_caas.sh and Makefile are already prepared by caasw before uploading,
# But we still run caasw mfgen on server again
# run_caas.sh runs make in the corresponding Docker container
# Website submitted jobs:
# .caas.conf is uploaded via frontend as well
# mfgen is run for the first time on server
try:
ret = subprocess.run([os.path.join(os.getcwd(), job.jobs_dir, job.id, "run_caas.sh")], cwd=work_root, timeout=compiler_timeout)
return ret.returncode
except subprocess.CalledProcessError as cpe:
print('CalledProcessError!')
return cpe.returncode
except subprocess.TimeoutExpired:
print('TimeoutExpired!')
return -1