-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava.py
More file actions
35 lines (26 loc) · 841 Bytes
/
java.py
File metadata and controls
35 lines (26 loc) · 841 Bytes
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
import subprocess
import os
class File:
currentPath = os.path.dirname(os.path.realpath(__file__))
def __init__(self, name, source):
self.name = name
self.source = source
def create(self):
filePath = 'output/%s.java' % (self.name)
f = open(filePath,'w')
f.write(self.source)
f.close()
def copile(self):
cmd = """cd %s/output; javac %s.java""" % (self.currentPath, self.name)
output = self.execute(cmd)
def run(self):
cmd = """cd %s/output; $JRE_HOME/bin/java Test %s""" % (self.currentPath, self.name)
return self.execute(cmd)
def execute(self, cmd):
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, errput = process.communicate()
if self.hasErrors(errput):
raise Exception(errput)
return output
def hasErrors(self, err):
return len(err)>0