Skip to content
This repository was archived by the owner on Apr 25, 2023. It is now read-only.

Commit 9f725f0

Browse files
committed
Add c script handlers
1 parent 3f362b8 commit 9f725f0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

handlers/c.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from subprocess import check_call, run
2+
from sys import exit
3+
4+
from .abc import BaseHandler
5+
6+
7+
class CHandler(BaseHandler):
8+
"""Compile and run C programs."""
9+
10+
def run(self, code_file):
11+
try:
12+
check_call(["bash", "-c", 'which -s clang'])
13+
except Exception:
14+
compiler = "gcc"
15+
else:
16+
compiler = "clang"
17+
comp = run([compiler, code_file, "-o", "script"])
18+
if comp.returncode != 0:
19+
exit(comp.returncode)
20+
else:
21+
exit(run(["bash", "-c", "./script"]).returncode)
22+
23+
def build(self, code, directory=None):
24+
with self.file("c", directory=directory) as f:
25+
f.write(code)
26+
self.run(f.name)

0 commit comments

Comments
 (0)