2018-01-10 15:47:25 +00:00
|
|
|
from shutil import copy, rmtree, copytree
|
2018-02-06 17:22:37 +00:00
|
|
|
from os.path import splitext, isfile, join
|
|
|
|
from glob import glob
|
2018-01-10 15:47:25 +00:00
|
|
|
|
2018-01-31 14:50:52 +00:00
|
|
|
from tfw.util import SupervisorMixin
|
|
|
|
from tfw.config import LOGIN_APP_DIR
|
|
|
|
from tfw.event_handler_base import EventHandlerBase
|
2018-01-10 15:47:25 +00:00
|
|
|
|
2018-02-06 17:22:37 +00:00
|
|
|
from tfw.config.logs import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class FileManager:
|
|
|
|
def __init__(self, source_directory, working_directory, selected_file=None):
|
|
|
|
self._sourcedir = source_directory
|
|
|
|
self._workdir = working_directory
|
|
|
|
self._reload_files()
|
|
|
|
self.filename = selected_file or self.files[0]
|
|
|
|
self.language = map_file_extension_to_language(self.filename)
|
|
|
|
|
|
|
|
def select_file(self, filename):
|
|
|
|
if not filename in self.files:
|
|
|
|
raise EnvironmentError('No such file in workdir!')
|
|
|
|
self.filename = filename
|
|
|
|
self.language = map_file_extension_to_language(self.filename)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def files(self):
|
2018-02-07 08:51:45 +00:00
|
|
|
return [file for file in glob(join(self._workdir, '**/*'), recursive=True) if isfile(file)]
|
2018-02-06 17:22:37 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def file_contents(self):
|
|
|
|
with open(self._filepath(self.filename), 'r') as ifile:
|
|
|
|
return ifile.read()
|
|
|
|
|
|
|
|
@file_contents.setter
|
|
|
|
def file_contents(self, value):
|
|
|
|
with open(self._filepath(self.filename), 'w') as ofile:
|
|
|
|
ofile.write(value)
|
|
|
|
|
|
|
|
def _filepath(self, filename):
|
|
|
|
return join(self._workdir, filename)
|
|
|
|
|
|
|
|
def _reload_files(self):
|
|
|
|
rmtree(self._workdir, ignore_errors=True)
|
|
|
|
copytree(self._sourcedir, self._workdir)
|
|
|
|
|
2018-01-10 15:47:25 +00:00
|
|
|
|
2018-01-24 11:17:15 +00:00
|
|
|
class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin):
|
2018-01-25 14:24:42 +00:00
|
|
|
def __init__(self, anchor, filename, process_name=None):
|
|
|
|
super().__init__(anchor)
|
2018-02-06 17:22:37 +00:00
|
|
|
self.filemanager = FileManager('login_projectdir', LOGIN_APP_DIR, selected_file=filename)
|
2018-01-10 15:47:25 +00:00
|
|
|
self.process_name = process_name or splitext(filename)[0]
|
2018-01-18 13:35:53 +00:00
|
|
|
self.commands = {
|
2018-02-06 17:22:37 +00:00
|
|
|
'read': self.read,
|
|
|
|
'write': self.write
|
2018-01-18 13:35:53 +00:00
|
|
|
}
|
2018-01-10 15:47:25 +00:00
|
|
|
|
2018-02-06 17:22:37 +00:00
|
|
|
# Supervisor needs these to run the login program
|
|
|
|
copy('source_code_server/server.py', LOGIN_APP_DIR)
|
|
|
|
copy('source_code_server/users.db', LOGIN_APP_DIR)
|
2018-01-17 15:33:34 +00:00
|
|
|
|
2018-02-06 17:22:37 +00:00
|
|
|
def read(self, data_json):
|
2018-01-17 13:34:27 +00:00
|
|
|
data_json['data'] = {
|
2018-02-06 17:22:37 +00:00
|
|
|
'filename': self.filemanager.filename,
|
|
|
|
'content': self.filemanager.file_contents,
|
|
|
|
'language': self.filemanager.language
|
2018-01-17 13:34:27 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 17:22:37 +00:00
|
|
|
def write(self, data_json):
|
|
|
|
self.filemanager.file_contents = data_json['data']['content']
|
2018-02-06 14:34:43 +00:00
|
|
|
self.restart_process()
|
2018-01-10 15:47:25 +00:00
|
|
|
|
2018-02-06 17:22:37 +00:00
|
|
|
def handle_event(self, anchor, data_json):
|
|
|
|
data = data_json['data']
|
|
|
|
self.commands[data['command']](data_json)
|
|
|
|
return data_json
|
2018-01-10 15:47:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def map_file_extension_to_language(filename):
|
|
|
|
language_map = {
|
|
|
|
# TODO: extend, maybe auto-generate???
|
|
|
|
'.py': 'python',
|
|
|
|
'.js': 'javascript'
|
|
|
|
}
|
|
|
|
_, extension = splitext(filename)
|
2018-02-06 17:22:37 +00:00
|
|
|
return language_map[extension]
|