sh_cpl/src/cpl_cli/live_server/live_server_service.py

93 lines
2.6 KiB
Python
Raw Normal View History

2021-03-13 11:05:17 +01:00
import os
import time
from contextlib import suppress
import psutil as psutil
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
from cpl.console.console import Console
from cpl.environment.application_environment_abc import ApplicationEnvironmentABC
2021-03-16 19:04:30 +01:00
from cpl_cli.configuration.build_settings import BuildSettings
2021-03-13 11:05:17 +01:00
from cpl_cli.live_server.live_server_thread import LiveServerThread
2021-03-30 09:27:45 +02:00
class LiveServerService(FileSystemEventHandler):
2021-03-13 11:05:17 +01:00
def __init__(self, env: ApplicationEnvironmentABC, build_settings: BuildSettings):
2021-03-14 16:01:15 +01:00
"""
Service for the live development server
:param env:
2021-03-14 16:01:15 +01:00
:param build_settings:
"""
2021-03-13 11:05:17 +01:00
FileSystemEventHandler.__init__(self)
self._env = env
2021-03-13 11:05:17 +01:00
self._build_settings = build_settings
self._src_dir = os.path.join(self._env.working_directory, self._build_settings.source_path)
2021-03-15 18:25:53 +01:00
self._ls_thread = None
2021-03-13 11:05:17 +01:00
self._observer = None
self._args: list[str] = []
2021-03-13 11:05:17 +01:00
def _start_observer(self):
2021-03-14 16:01:15 +01:00
"""
Starts the file changes observer
:return:
"""
2021-03-13 11:05:17 +01:00
self._observer = Observer()
self._observer.schedule(self, path=self._src_dir, recursive=True)
self._observer.start()
def _restart(self):
2021-03-14 16:01:15 +01:00
"""
Restarts the CPL project
:return:
"""
2021-03-13 11:05:17 +01:00
for proc in psutil.process_iter():
with suppress(Exception):
2021-03-13 11:15:15 +01:00
if proc.cmdline() == self._ls_thread.command:
2021-03-16 22:29:30 +01:00
proc.kill()
2021-03-13 11:05:17 +01:00
Console.write_line('Restart\n')
2021-03-13 11:15:15 +01:00
while self._ls_thread.is_alive():
2021-03-13 11:05:17 +01:00
time.sleep(1)
2021-03-15 18:25:53 +01:00
self._start()
2021-03-13 11:05:17 +01:00
def on_modified(self, event):
2021-03-14 16:01:15 +01:00
"""
Triggers when source file is modified
:param event:
:return:
"""
2021-03-13 11:05:17 +01:00
if event.is_directory:
return None
# Event is modified, you can process it now
if str(event.src_path).endswith('.py'):
self._observer.stop()
self._restart()
2021-03-15 18:25:53 +01:00
def _start(self):
self._start_observer()
self._ls_thread = LiveServerThread(self._src_dir, self._build_settings, self._args)
2021-03-15 18:25:53 +01:00
self._ls_thread.start()
self._ls_thread.join()
Console.close()
def start(self, args: list[str]):
2021-03-14 16:01:15 +01:00
"""
Starts the CPL live development server
:param args:
2021-03-14 16:01:15 +01:00
:return:
"""
if self._build_settings.main == '':
Console.error('Project has no entry point.')
return
self._args = args
2021-03-13 11:05:17 +01:00
Console.write_line('** CPL live development server is running **')
2021-03-15 18:25:53 +01:00
self._start()