filestorage
This commit is contained in:
@@ -4,10 +4,10 @@ from shutil import copyfile, rmtree
|
||||
from subprocess import call, TimeoutExpired
|
||||
|
||||
from Main.management.commands.bot import bot
|
||||
from Main.models import ExtraFile
|
||||
from Main.models import ExtraFile, SolutionFile
|
||||
from Main.models.progress import Progress
|
||||
from Sprint.settings import CONSTS
|
||||
from SprintLib.utils import copy_content
|
||||
from SprintLib.utils import get_bytes
|
||||
|
||||
|
||||
class TestException(Exception):
|
||||
@@ -56,11 +56,15 @@ class BaseTester:
|
||||
self.solution = solution
|
||||
|
||||
def execute(self):
|
||||
if not exists(self.solution.testing_directory):
|
||||
mkdir(self.solution.testing_directory)
|
||||
copy_content(
|
||||
self.solution.directory, self.solution.testing_directory, ("test_dir",)
|
||||
)
|
||||
mkdir("solution")
|
||||
for file in SolutionFile.objects.filter(solution=self.solution):
|
||||
dirs = file.path.split('/')
|
||||
for i in range(len(dirs) - 1):
|
||||
name = join("solution", '/'.join(dirs[:i + 1]))
|
||||
if not exists(name):
|
||||
mkdir(name)
|
||||
with open(file.path, 'wb') as fs:
|
||||
fs.write(get_bytes(file.fs_id))
|
||||
self.solution.result = CONSTS["testing_status"]
|
||||
self.solution.save()
|
||||
docker_command = f"docker run --name solution_{self.solution.id} --volume={self.solution.volume_directory}:/{self.working_directory} -t -d {self.solution.language.image}"
|
||||
@@ -68,7 +72,8 @@ class BaseTester:
|
||||
call(docker_command, shell=True)
|
||||
print("Container created")
|
||||
for file in ExtraFile.objects.filter(task=self.solution.task):
|
||||
copyfile(file.path, join(self.solution.testing_directory, file.filename))
|
||||
with open(join("solution", file.filename), 'wb') as fs:
|
||||
fs.write(get_bytes(file.fs_id))
|
||||
print("Files copied")
|
||||
try:
|
||||
self.before_test()
|
||||
@@ -98,7 +103,7 @@ class BaseTester:
|
||||
print(str(e))
|
||||
self.solution.save()
|
||||
call(f"docker rm --force solution_{self.solution.id}", shell=True)
|
||||
rmtree(self.solution.testing_directory)
|
||||
rmtree("solution")
|
||||
self.solution.user.userinfo.refresh_from_db()
|
||||
if self.solution.user.userinfo.notification_solution_result:
|
||||
bot.send_message(
|
||||
|
@@ -1,12 +1,21 @@
|
||||
from os import listdir
|
||||
from os.path import isfile, join
|
||||
from shutil import copyfile, copytree
|
||||
from requests import get, post
|
||||
|
||||
from Sprint import settings
|
||||
|
||||
|
||||
def copy_content(from_dir, to_dir, exc=()):
|
||||
for file in listdir(from_dir):
|
||||
if file in exc:
|
||||
continue
|
||||
full_path = join(from_dir, file)
|
||||
func = copyfile if isfile(full_path) else copytree
|
||||
func(full_path, join(to_dir, file))
|
||||
def write_bytes(data):
|
||||
url = settings.FS_HOST + ":" + str(settings.FS_PORT) + "/upload_file"
|
||||
print(url)
|
||||
return post(url, data=data).json()['id']
|
||||
|
||||
|
||||
def get_bytes(num):
|
||||
url = settings.FS_HOST + ":" + str(settings.FS_PORT) + "/get_file?id=" + str(num)
|
||||
print(url)
|
||||
return get(url).content
|
||||
|
||||
|
||||
def delete_file(num):
|
||||
url = settings.FS_HOST + ":" + str(settings.FS_PORT) + "/delete_file?id=" + str(num)
|
||||
print(url)
|
||||
post(url)
|
||||
|
Reference in New Issue
Block a user