From c74c00c6180113e2d707ae6235176b0987d3930a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Tue, 6 Feb 2018 15:34:43 +0100 Subject: [PATCH 01/17] Extract process managing logic to SupervisorMixin from event handlers --- lib/tfw/util.py | 13 +++++++++++++ src/event_handlers/source_code_event_handler.py | 11 +++-------- src/event_handlers/terminado_event_handler.py | 2 +- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/tfw/util.py b/lib/tfw/util.py index 4785ed4..82f4169 100644 --- a/lib/tfw/util.py +++ b/lib/tfw/util.py @@ -1,4 +1,6 @@ import xmlrpc.client, zmq +from contextlib import suppress +from xmlrpc.client import Fault as SupervisorFault from .config.envvars import SUPERVISOR_HTTP_URI @@ -14,6 +16,17 @@ def create_source_code_response_data(filename, content, language): class SupervisorMixin: supervisor = xmlrpc.client.ServerProxy(SUPERVISOR_HTTP_URI).supervisor + def stop_process(self): + with suppress(SupervisorFault): + self.supervisor.stopProcess(self.process_name) + + def start_process(self): + self.supervisor.startProcess(self.process_name) + + def restart_process(self): + self.stop_process() + self.start_process() + class ZMQConnectorBase: def __init__(self, zmq_context=None): diff --git a/src/event_handlers/source_code_event_handler.py b/src/event_handlers/source_code_event_handler.py index 4620729..6ba7b78 100644 --- a/src/event_handlers/source_code_event_handler.py +++ b/src/event_handlers/source_code_event_handler.py @@ -1,7 +1,5 @@ from shutil import copy, rmtree, copytree from os.path import splitext -from contextlib import suppress -from xmlrpc.client import Fault as SupervisorFault from tfw.util import SupervisorMixin from tfw.config import LOGIN_APP_DIR @@ -43,19 +41,16 @@ class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): data = data_json['data'] with open(self.file, 'w') as ofile: ofile.write(data['content']) - self.supervisor.stopProcess(self.process_name) - self.supervisor.startProcess(self.process_name) + self.restart_process() def create_initial_state(self): - with suppress(SupervisorFault): - self.supervisor.stopProcess(self.process_name) + self.stop_process() rmtree(self.working_directory, ignore_errors=True) copytree('source_code_server/', self.working_directory) file = copy(self.filename, self.working_directory) - self.supervisor.startProcess(self.process_name) - + self.start_process() return file diff --git a/src/event_handlers/terminado_event_handler.py b/src/event_handlers/terminado_event_handler.py index 463af7e..4554d28 100644 --- a/src/event_handlers/terminado_event_handler.py +++ b/src/event_handlers/terminado_event_handler.py @@ -17,7 +17,7 @@ class TerminadoEventHandler(EventHandlerBase, SupervisorMixin): def setup_terminado_server(self): rmtree(self.working_directory, ignore_errors=True) copytree('terminado_server/', self.working_directory) - self.supervisor.startProcess(self.process_name) + self.start_process() def handle_event(self, anchor, data_json): log.debug('TerminadoEventHandler received event for anchor {}'.format(anchor)) From 5cc1ab2c8df78de6c3adb503400d556c03e36acd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Tue, 6 Feb 2018 18:22:37 +0100 Subject: [PATCH 02/17] Refactor handling of files in SourceCodeEventHandler --- .../{ => login_projectdir}/login_component.py | 0 .../source_code_event_handler.py | 95 ++++++++++++------- 2 files changed, 59 insertions(+), 36 deletions(-) rename src/event_handlers/{ => login_projectdir}/login_component.py (100%) diff --git a/src/event_handlers/login_component.py b/src/event_handlers/login_projectdir/login_component.py similarity index 100% rename from src/event_handlers/login_component.py rename to src/event_handlers/login_projectdir/login_component.py diff --git a/src/event_handlers/source_code_event_handler.py b/src/event_handlers/source_code_event_handler.py index 6ba7b78..f5b2445 100644 --- a/src/event_handlers/source_code_event_handler.py +++ b/src/event_handlers/source_code_event_handler.py @@ -1,58 +1,81 @@ from shutil import copy, rmtree, copytree -from os.path import splitext +from os.path import splitext, isfile, join +from glob import glob from tfw.util import SupervisorMixin from tfw.config import LOGIN_APP_DIR from tfw.event_handler_base import EventHandlerBase +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): + return [file for file in glob(self._workdir) if isfile(file)] + + @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) + class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): def __init__(self, anchor, filename, process_name=None): super().__init__(anchor) - self.working_directory = LOGIN_APP_DIR - self.filename = filename - self.language = map_file_extension_to_language(filename) + self.filemanager = FileManager('login_projectdir', LOGIN_APP_DIR, selected_file=filename) self.process_name = process_name or splitext(filename)[0] self.commands = { - 'read': self.read_file, - 'write': self.write_file + 'read': self.read, + 'write': self.write } - self.file = self.create_initial_state() + # 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) + + def read(self, data_json): + data_json['data'] = { + 'filename': self.filemanager.filename, + 'content': self.filemanager.file_contents, + 'language': self.filemanager.language + } + + def write(self, data_json): + self.filemanager.file_contents = data_json['data']['content'] + self.restart_process() def handle_event(self, anchor, data_json): data = data_json['data'] self.commands[data['command']](data_json) return data_json - def handle_reset(self, data_json): - self.create_initial_state() - - def read_file(self, data_json): - with open(self.file, 'r') as ifile: - content = ifile.read() - data_json['data'] = { - 'filename': self.filename, - 'content': content, - 'language': self.language - } - - def write_file(self, data_json): - data = data_json['data'] - with open(self.file, 'w') as ofile: - ofile.write(data['content']) - self.restart_process() - - def create_initial_state(self): - self.stop_process() - - rmtree(self.working_directory, ignore_errors=True) - copytree('source_code_server/', self.working_directory) - file = copy(self.filename, self.working_directory) - - self.start_process() - return file - def map_file_extension_to_language(filename): language_map = { @@ -61,4 +84,4 @@ def map_file_extension_to_language(filename): '.js': 'javascript' } _, extension = splitext(filename) - return language_map[extension] + return language_map[extension] \ No newline at end of file From 944066d6992bad2a879425ae18f888a239d34eae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 7 Feb 2018 09:51:45 +0100 Subject: [PATCH 03/17] Implement recursive file lookup in FileManager.files --- src/event_handlers/source_code_event_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/event_handlers/source_code_event_handler.py b/src/event_handlers/source_code_event_handler.py index f5b2445..345ee20 100644 --- a/src/event_handlers/source_code_event_handler.py +++ b/src/event_handlers/source_code_event_handler.py @@ -26,7 +26,7 @@ class FileManager: @property def files(self): - return [file for file in glob(self._workdir) if isfile(file)] + return [file for file in glob(join(self._workdir, '**/*'), recursive=True) if isfile(file)] @property def file_contents(self): From 71dcb58c92373c72ce88036ef4ee5301729a3304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 7 Feb 2018 12:01:57 +0100 Subject: [PATCH 04/17] Change loglevel back to DEBUG (CRITICAL was committed accidentally) --- lib/tfw/config/logs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tfw/config/logs.py b/lib/tfw/config/logs.py index 94412b9..b9f9ca8 100644 --- a/lib/tfw/config/logs.py +++ b/lib/tfw/config/logs.py @@ -1,3 +1,3 @@ import logging -logging.basicConfig(level=logging.CRITICAL) +logging.basicConfig(level=logging.DEBUG) From eb344de7a297ee6c2d019f78676b523c9b22bf4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 7 Feb 2018 12:02:53 +0100 Subject: [PATCH 05/17] Implement SourceCodeEventHandler support for multiple files --- src/event_handlers/event_handler_main.py | 2 +- .../source_code_event_handler.py | 29 +++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/event_handlers/event_handler_main.py b/src/event_handlers/event_handler_main.py index add1bb1..23aa5e5 100644 --- a/src/event_handlers/event_handler_main.py +++ b/src/event_handlers/event_handler_main.py @@ -4,6 +4,6 @@ from tornado.ioloop import IOLoop if __name__ == '__main__': - anchor_webide = SourceCodeEventHandler('anchor_webide', 'login_component.py', 'login') + anchor_webide = SourceCodeEventHandler('anchor_webide', 'login_projectdir', 'login') anchor_terminado = TerminadoEventHandler('anchor_terminado', 'terminado') IOLoop.instance().start() diff --git a/src/event_handlers/source_code_event_handler.py b/src/event_handlers/source_code_event_handler.py index 345ee20..0972079 100644 --- a/src/event_handlers/source_code_event_handler.py +++ b/src/event_handlers/source_code_event_handler.py @@ -1,5 +1,5 @@ from shutil import copy, rmtree, copytree -from os.path import splitext, isfile, join +from os.path import splitext, isfile, join, basename from glob import glob from tfw.util import SupervisorMixin @@ -15,12 +15,12 @@ class FileManager: self._sourcedir = source_directory self._workdir = working_directory self._reload_files() - self.filename = selected_file or self.files[0] + self.filename = selected_file or basename(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!') + if not self._filepath(filename) in self.files: + raise EnvironmentError('No such file in workdir!') self.filename = filename self.language = map_file_extension_to_language(self.filename) @@ -47,13 +47,15 @@ class FileManager: class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): - def __init__(self, anchor, filename, process_name=None): + def __init__(self, anchor, directory, process_name=None, selected_file=None): super().__init__(anchor) - self.filemanager = FileManager('login_projectdir', LOGIN_APP_DIR, selected_file=filename) - self.process_name = process_name or splitext(filename)[0] + self.filemanager = FileManager(directory, LOGIN_APP_DIR, selected_file=selected_file) + self.process_name = process_name + self.commands = { 'read': self.read, - 'write': self.write + 'write': self.write, + 'select': self.select } # Supervisor needs these to run the login program @@ -71,6 +73,12 @@ class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): self.filemanager.file_contents = data_json['data']['content'] self.restart_process() + def select(self, data_json): + try: + self.filemanager.select_file(data_json['data']['filename']) + except EnvironmentError: + log.critical('Failed to select file "{}"'.format(data_json['data']['filename'])) + def handle_event(self, anchor, data_json): data = data_json['data'] self.commands[data['command']](data_json) @@ -84,4 +92,7 @@ def map_file_extension_to_language(filename): '.js': 'javascript' } _, extension = splitext(filename) - return language_map[extension] \ No newline at end of file + language = 'javascript' + try: language = language_map[extension] + except KeyError: log.debug('No such extension in list, falling back to default: "{}".'.format(language)) + return language \ No newline at end of file From fc168f47ef272d3fd9212d43c62abce154a0bbcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 7 Feb 2018 14:50:29 +0100 Subject: [PATCH 06/17] Add support for tab switching on backend SourceCodeEventHandler --- src/event_handlers/source_code_event_handler.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/event_handlers/source_code_event_handler.py b/src/event_handlers/source_code_event_handler.py index 0972079..17ca059 100644 --- a/src/event_handlers/source_code_event_handler.py +++ b/src/event_handlers/source_code_event_handler.py @@ -19,14 +19,14 @@ class FileManager: self.language = map_file_extension_to_language(self.filename) def select_file(self, filename): - if not self._filepath(filename) in self.files: + 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): - return [file for file in glob(join(self._workdir, '**/*'), recursive=True) if isfile(file)] + return [basename(file) for file in glob(join(self._workdir, '**/*'), recursive=True) if isfile(file)] @property def file_contents(self): @@ -66,7 +66,8 @@ class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): data_json['data'] = { 'filename': self.filemanager.filename, 'content': self.filemanager.file_contents, - 'language': self.filemanager.language + 'language': self.filemanager.language, + 'files': self.filemanager.files } def write(self, data_json): From d0a04240b67c840cd5395132f59de10443395368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 8 Feb 2018 14:13:14 +0100 Subject: [PATCH 07/17] Fix non-unicode files kill whole webide backend --- src/event_handlers/source_code_event_handler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/event_handlers/source_code_event_handler.py b/src/event_handlers/source_code_event_handler.py index 17ca059..24ed9a4 100644 --- a/src/event_handlers/source_code_event_handler.py +++ b/src/event_handlers/source_code_event_handler.py @@ -30,12 +30,12 @@ class FileManager: @property def file_contents(self): - with open(self._filepath(self.filename), 'r') as ifile: + with open(self._filepath(self.filename), 'r', errors='surrogateescape') as ifile: return ifile.read() @file_contents.setter def file_contents(self, value): - with open(self._filepath(self.filename), 'w') as ofile: + with open(self._filepath(self.filename), 'w', errors='surrogateescape') as ofile: ofile.write(value) def _filepath(self, filename): From c2c5353f984846056839d9f4a10d065725f62c7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 8 Feb 2018 14:45:07 +0100 Subject: [PATCH 08/17] Fix files in subdirectories killing everything I stand for --- src/event_handlers/source_code_event_handler.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/event_handlers/source_code_event_handler.py b/src/event_handlers/source_code_event_handler.py index 24ed9a4..2b86766 100644 --- a/src/event_handlers/source_code_event_handler.py +++ b/src/event_handlers/source_code_event_handler.py @@ -1,5 +1,5 @@ from shutil import copy, rmtree, copytree -from os.path import splitext, isfile, join, basename +from os.path import splitext, isfile, join, relpath from glob import glob from tfw.util import SupervisorMixin @@ -15,7 +15,7 @@ class FileManager: self._sourcedir = source_directory self._workdir = working_directory self._reload_files() - self.filename = selected_file or basename(self.files[0]) + self.filename = selected_file or self._relpath(self.files[0]) self.language = map_file_extension_to_language(self.filename) def select_file(self, filename): @@ -26,7 +26,7 @@ class FileManager: @property def files(self): - return [basename(file) for file in glob(join(self._workdir, '**/*'), recursive=True) if isfile(file)] + return [self._relpath(file) for file in glob(join(self._workdir, '**/*'), recursive=True) if isfile(file)] @property def file_contents(self): @@ -41,6 +41,9 @@ class FileManager: def _filepath(self, filename): return join(self._workdir, filename) + def _relpath(self, filename): + return relpath(self._filepath(filename), start=self._workdir) + def _reload_files(self): rmtree(self._workdir, ignore_errors=True) copytree(self._sourcedir, self._workdir) From 6283b825d67a89a2cdb745a5d079eca8831653d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 8 Feb 2018 14:54:00 +0100 Subject: [PATCH 09/17] =?UTF-8?q?Fix=20file=20list=20disappearing=20from?= =?UTF-8?q?=20json=20after=20write=20command=20=E2=80=93=20webide.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/event_handlers/source_code_event_handler.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/event_handlers/source_code_event_handler.py b/src/event_handlers/source_code_event_handler.py index 2b86766..942a9e1 100644 --- a/src/event_handlers/source_code_event_handler.py +++ b/src/event_handlers/source_code_event_handler.py @@ -75,6 +75,7 @@ class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): def write(self, data_json): self.filemanager.file_contents = data_json['data']['content'] + data_json['data']['files'] = self.filemanager.files self.restart_process() def select(self, data_json): From 792981742c97144f201a4e2c15cb236e34af20e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 8 Feb 2018 15:10:37 +0100 Subject: [PATCH 10/17] Make SourceCodeEventHandler command handlers data_json['data'] based --- .../source_code_event_handler.py | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/event_handlers/source_code_event_handler.py b/src/event_handlers/source_code_event_handler.py index 942a9e1..51cff20 100644 --- a/src/event_handlers/source_code_event_handler.py +++ b/src/event_handlers/source_code_event_handler.py @@ -65,28 +65,27 @@ class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): copy('source_code_server/server.py', LOGIN_APP_DIR) copy('source_code_server/users.db', LOGIN_APP_DIR) - def read(self, data_json): - data_json['data'] = { - 'filename': self.filemanager.filename, - 'content': self.filemanager.file_contents, - 'language': self.filemanager.language, - 'files': self.filemanager.files - } + def read(self, data): + data['filename'] = self.filemanager.filename + data['content'] = self.filemanager.file_contents + data['language'] = self.filemanager.language + data['files'] = self.filemanager.files + return data - def write(self, data_json): - self.filemanager.file_contents = data_json['data']['content'] - data_json['data']['files'] = self.filemanager.files + def write(self, data): + self.filemanager.file_contents = data['content'] + data['files'] = self.filemanager.files self.restart_process() + return data - def select(self, data_json): - try: - self.filemanager.select_file(data_json['data']['filename']) - except EnvironmentError: - log.critical('Failed to select file "{}"'.format(data_json['data']['filename'])) + def select(self, data): + try: self.filemanager.select_file(data['filename']) + except EnvironmentError: log.critical('Failed to select file "{}"'.format(data['filename'])) + return data def handle_event(self, anchor, data_json): data = data_json['data'] - self.commands[data['command']](data_json) + data_json['data'] = self.commands[data['command']](data) return data_json From 89694097cc6039fc40a6eaf8449938b0dbe1cafb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 8 Feb 2018 15:17:30 +0100 Subject: [PATCH 11/17] Temporary extension of ACE editor language map, make 'text' default --- src/event_handlers/source_code_event_handler.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/event_handlers/source_code_event_handler.py b/src/event_handlers/source_code_event_handler.py index 51cff20..b1328e0 100644 --- a/src/event_handlers/source_code_event_handler.py +++ b/src/event_handlers/source_code_event_handler.py @@ -91,12 +91,19 @@ class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): def map_file_extension_to_language(filename): language_map = { - # TODO: extend, maybe auto-generate??? '.py': 'python', - '.js': 'javascript' + '.js': 'javascript', + '.html': 'html', + '.css': 'css', + '.java': 'java', + '.cpp': 'c_cpp', + '.hpp': 'c_cpp', + '.c': 'c_cpp', + '.h': 'c_cpp', + '.cs': 'csharp' } _, extension = splitext(filename) - language = 'javascript' + language = 'text' try: language = language_map[extension] except KeyError: log.debug('No such extension in list, falling back to default: "{}".'.format(language)) return language \ No newline at end of file From dceb3b947fcc47f456516ceb8e9d50f3f9275851 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 8 Feb 2018 16:49:01 +0100 Subject: [PATCH 12/17] Refactor handling of working directory for SourceCodeEventHandler --- Dockerfile | 9 ++++++++- lib/tfw/config/envvars.py | 1 + src/event_handlers/event_handler_main.py | 4 +++- src/event_handlers/source_code_event_handler.py | 16 ++-------------- .../login_component.py | 0 src/event_handlers/source_code_server/server.py | 7 ++++--- 6 files changed, 18 insertions(+), 19 deletions(-) rename src/event_handlers/{login_projectdir => source_code_server}/login_component.py (100%) diff --git a/Dockerfile b/Dockerfile index 20788e8..86703e1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -51,7 +51,8 @@ ENV TFW_APP_DIR="/srv/app" ENV TFW_FRONTEND_DIR="/srv/frontend" ENV TFW_LOGIN_APP_DIR="/tmp/source_code_server" ENV TFW_TERMINADO_DIR="/tmp/terminado_server" -ENV TFW_TERMINADO_WD="/home/${AVATAO_USER}" +ENV TFW_WEBIDE_WD="/home/${AVATAO_USER}/workdir" +ENV TFW_TERMINADO_WD=${TFW_WEBIDE_WD} ENV TFW_LIB_DIR="/usr/local/lib/" ENV TFW_SUPERVISORD_CONF="/etc/supervisor/supervisord.conf" ENV TFW_SUPERVISORD_COMPONENTS="/etc/supervisor/conf" @@ -75,6 +76,12 @@ COPY src/event_handlers ${TFW_EVENT_HANDLERS_DIR} RUN mv /data/dist ${TFW_FRONTEND_DIR} +COPY src/event_handlers/source_code_server/server.py ${TFW_LOGIN_APP_DIR}/ +COPY src/event_handlers/source_code_server/users.db ${TFW_LOGIN_APP_DIR}/ +COPY src/event_handlers/source_code_server/login_component.py ${TFW_WEBIDE_WD}/ + +RUN chown -R ${AVATAO_USER} ${TFW_WEBIDE_WD} + USER ${AVATAO_USER} WORKDIR /home/${AVATAO_USER} diff --git a/lib/tfw/config/envvars.py b/lib/tfw/config/envvars.py index 297c4bd..002dbc3 100644 --- a/lib/tfw/config/envvars.py +++ b/lib/tfw/config/envvars.py @@ -7,6 +7,7 @@ SUPERVISOR_HTTP_PORT = os.getenv('TFW_SUPERVISOR_PORT', 9001) LOGIN_APP_PORT = os.getenv('TFW_LOGIN_APP_PORT', 6666) TERMINADO_PORT = os.getenv('TFW_TERMINADO_PORT', 9999) TERMINADO_WD = os.getenv('TFW_TERMINADO_WD') +WEBIDE_WD = os.getenv('TFW_WEBIDE_WD') SUPERVISOR_HTTP_URI = 'http://localhost:{}'.format(SUPERVISOR_HTTP_PORT) diff --git a/src/event_handlers/event_handler_main.py b/src/event_handlers/event_handler_main.py index 23aa5e5..327dedb 100644 --- a/src/event_handlers/event_handler_main.py +++ b/src/event_handlers/event_handler_main.py @@ -2,8 +2,10 @@ from source_code_event_handler import SourceCodeEventHandler from terminado_event_handler import TerminadoEventHandler from tornado.ioloop import IOLoop +from tfw.config.envvars import WEBIDE_WD + if __name__ == '__main__': - anchor_webide = SourceCodeEventHandler('anchor_webide', 'login_projectdir', 'login') + anchor_webide = SourceCodeEventHandler('anchor_webide', WEBIDE_WD, 'login') anchor_terminado = TerminadoEventHandler('anchor_terminado', 'terminado') IOLoop.instance().start() diff --git a/src/event_handlers/source_code_event_handler.py b/src/event_handlers/source_code_event_handler.py index b1328e0..8109504 100644 --- a/src/event_handlers/source_code_event_handler.py +++ b/src/event_handlers/source_code_event_handler.py @@ -1,9 +1,7 @@ -from shutil import copy, rmtree, copytree from os.path import splitext, isfile, join, relpath from glob import glob from tfw.util import SupervisorMixin -from tfw.config import LOGIN_APP_DIR from tfw.event_handler_base import EventHandlerBase from tfw.config.logs import logging @@ -11,10 +9,8 @@ log = logging.getLogger(__name__) class FileManager: - def __init__(self, source_directory, working_directory, selected_file=None): - self._sourcedir = source_directory + def __init__(self, working_directory, selected_file=None): self._workdir = working_directory - self._reload_files() self.filename = selected_file or self._relpath(self.files[0]) self.language = map_file_extension_to_language(self.filename) @@ -44,15 +40,11 @@ class FileManager: def _relpath(self, filename): return relpath(self._filepath(filename), start=self._workdir) - def _reload_files(self): - rmtree(self._workdir, ignore_errors=True) - copytree(self._sourcedir, self._workdir) - class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): def __init__(self, anchor, directory, process_name=None, selected_file=None): super().__init__(anchor) - self.filemanager = FileManager(directory, LOGIN_APP_DIR, selected_file=selected_file) + self.filemanager = FileManager(directory, selected_file=selected_file) self.process_name = process_name self.commands = { @@ -61,10 +53,6 @@ class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): 'select': self.select } - # 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) - def read(self, data): data['filename'] = self.filemanager.filename data['content'] = self.filemanager.file_contents diff --git a/src/event_handlers/login_projectdir/login_component.py b/src/event_handlers/source_code_server/login_component.py similarity index 100% rename from src/event_handlers/login_projectdir/login_component.py rename to src/event_handlers/source_code_server/login_component.py diff --git a/src/event_handlers/source_code_server/server.py b/src/event_handlers/source_code_server/server.py index 96afb6a..e9b0b25 100644 --- a/src/event_handlers/source_code_server/server.py +++ b/src/event_handlers/source_code_server/server.py @@ -1,9 +1,10 @@ -import json - +import json, sys from tornado.ioloop import IOLoop from tornado.web import RequestHandler, Application -from tfw.config import LOGIN_APP_PORT +from tfw.config import LOGIN_APP_PORT, WEBIDE_WD + +sys.path.append(WEBIDE_WD) from login_component import authorize_login From 8c492df646e1435a4d7932b213b3cbc54d369dc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 8 Feb 2018 16:56:30 +0100 Subject: [PATCH 13/17] Implement exclude list in FileManager --- src/event_handlers/source_code_event_handler.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/event_handlers/source_code_event_handler.py b/src/event_handlers/source_code_event_handler.py index 8109504..b92cc80 100644 --- a/src/event_handlers/source_code_event_handler.py +++ b/src/event_handlers/source_code_event_handler.py @@ -10,6 +10,7 @@ log = logging.getLogger(__name__) class FileManager: def __init__(self, working_directory, selected_file=None): + self.exclude = ['__pycache__'] self._workdir = working_directory self.filename = selected_file or self._relpath(self.files[0]) self.language = map_file_extension_to_language(self.filename) @@ -22,7 +23,9 @@ class FileManager: @property def files(self): - return [self._relpath(file) for file in glob(join(self._workdir, '**/*'), recursive=True) if isfile(file)] + return [self._relpath(file) for file in glob(join(self._workdir, '**/*'), recursive=True) + if isfile(file) and + not any(word in file for word in self.exclude)] @property def file_contents(self): From 557318af4e992089b11a4d65074bd1a9d7cc19f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 8 Feb 2018 17:10:58 +0100 Subject: [PATCH 14/17] Implement robust error handling in SourceCodeEventHandler --- src/event_handlers/source_code_event_handler.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/event_handlers/source_code_event_handler.py b/src/event_handlers/source_code_event_handler.py index b92cc80..3f128cf 100644 --- a/src/event_handlers/source_code_event_handler.py +++ b/src/event_handlers/source_code_event_handler.py @@ -57,21 +57,23 @@ class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): } def read(self, data): + try: data['content'] = self.filemanager.file_contents + except PermissionError: data['content'] = 'You have no permission to open that file :(' data['filename'] = self.filemanager.filename - data['content'] = self.filemanager.file_contents data['language'] = self.filemanager.language data['files'] = self.filemanager.files return data def write(self, data): - self.filemanager.file_contents = data['content'] + try: self.filemanager.file_contents = data['content'] + except Exception: log.exception('Error reading file!') data['files'] = self.filemanager.files self.restart_process() return data def select(self, data): try: self.filemanager.select_file(data['filename']) - except EnvironmentError: log.critical('Failed to select file "{}"'.format(data['filename'])) + except EnvironmentError: log.exception('Failed to select file "{}"'.format(data['filename'])) return data def handle_event(self, anchor, data_json): From be244217aa3409b59dd73b1f1a08e44701410423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 8 Feb 2018 17:39:59 +0100 Subject: [PATCH 15/17] Refactor trivial piece of code overcomplicated --- src/event_handlers/source_code_event_handler.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/event_handlers/source_code_event_handler.py b/src/event_handlers/source_code_event_handler.py index 3f128cf..aa57777 100644 --- a/src/event_handlers/source_code_event_handler.py +++ b/src/event_handlers/source_code_event_handler.py @@ -96,7 +96,4 @@ def map_file_extension_to_language(filename): '.cs': 'csharp' } _, extension = splitext(filename) - language = 'text' - try: language = language_map[extension] - except KeyError: log.debug('No such extension in list, falling back to default: "{}".'.format(language)) - return language \ No newline at end of file + return language_map.get(extension, 'text') \ No newline at end of file From 3da263d8cdf25ef7c93812d2f1b012837223c78a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 8 Feb 2018 17:42:00 +0100 Subject: [PATCH 16/17] Remove leftover default value --- src/event_handlers/source_code_event_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/event_handlers/source_code_event_handler.py b/src/event_handlers/source_code_event_handler.py index aa57777..b426f4a 100644 --- a/src/event_handlers/source_code_event_handler.py +++ b/src/event_handlers/source_code_event_handler.py @@ -45,7 +45,7 @@ class FileManager: class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): - def __init__(self, anchor, directory, process_name=None, selected_file=None): + def __init__(self, anchor, directory, process_name, selected_file=None): super().__init__(anchor) self.filemanager = FileManager(directory, selected_file=selected_file) self.process_name = process_name From c0f533c692ee7bc768ad48c333e7159bcf4479c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 8 Feb 2018 17:48:48 +0100 Subject: [PATCH 17/17] Update frontend --- src/frontend | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/frontend b/src/frontend index 7e4bc12..a6ee391 160000 --- a/src/frontend +++ b/src/frontend @@ -1 +1 @@ -Subproject commit 7e4bc12616026c10829c5b50984f6053e31d3b82 +Subproject commit a6ee39156b49b003a23cce27e9be25a5e6494a1f