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

Commit 3f362b8

Browse files
committed
Add python script handlers
1 parent 5662c4e commit 3f362b8

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

handlers/python.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from subprocess import check_call, run
2+
from sys import exit
3+
4+
from .abc import BaseHandler
5+
6+
7+
class PythonHandler(BaseHandler):
8+
"""Run Python 3 programs."""
9+
10+
def run(self, code_file):
11+
try:
12+
check_call(["bash", "-c", 'which -s python3'])
13+
except Exception:
14+
py_str = "python3"
15+
else:
16+
py_str = "python"
17+
exit(run([py_str, code_file]).returncode)
18+
19+
def build(self, code, directory=None):
20+
with self.file("py", directory=directory) as f:
21+
f.write(code)
22+
self.run(f.name)
23+
24+
25+
class Python2Handler(PythonHandler):
26+
"""Run Python 2 programs."""
27+
28+
def run(self, code_file):
29+
try:
30+
check_call(["bash", "-c", 'which -s python2'])
31+
except Exception:
32+
py_str = "python2"
33+
else:
34+
py_str = "python"
35+
exit(run([py_str, code_file]).returncode)

0 commit comments

Comments
 (0)