Skip to content

Instantly share code, notes, and snippets.

@junhua
Created December 30, 2015 04:57
Show Gist options
  • Save junhua/1b77776aa6313a734efa to your computer and use it in GitHub Desktop.
Save junhua/1b77776aa6313a734efa to your computer and use it in GitHub Desktop.
# Not all import used
from __future__ import absolute_import
from celery import shared_task, current_task
from apps.common.util import kill_process
from django.conf import settings
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
import subprocess as sub
import os
def compile_file(username, title, code):
directory = "submissions/%s" % username
file_dir = "%s/%s.java" % (directory, title)
class_dir = "%s/%s.class" % (directory, title)
#path = default_storage.save(file_dir, ContentFile(code))
if not os.path.exists(directory):
try:
os.makedirs(directory)
except OSError as exception:
print "Exception creating dir: " + exception.strerror
path = default_storage.save(file_dir, ContentFile(code))
command = [
'javac',
str('%s/%s' % (settings.MEDIA_ROOT, path))
]
#result = sub.Popen(
# command, stdout=sub.PIPE, stderr=sub.STDOUT).stdout.read()
result = kill_process.RunCmd(command, 60).Run().stdout.read()
# result is none when timeout occurs
if result is None:
return "Time out"
if (result != ''):
try:
default_storage.delete(path)
default_storage.delete(class_dir)
except:
pass
start_index = result.find("error:")
result = result[start_index:]
return result
# current_task.request.hostname
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment