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
|
2021-03-29 08:56:18 +02:00
|
|
|
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-04-08 19:30:00 +02:00
|
|
|
from cpl_cli.configuration.project_settings import ProjectSettings
|
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
|
|
|
|
2021-04-08 19:30:00 +02:00
|
|
|
def __init__(self, env: ApplicationEnvironmentABC, project_settings: ProjectSettings,
|
|
|
|
build_settings: BuildSettings):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Service for the live development server
|
2021-03-29 08:56:18 +02:00
|
|
|
:param env:
|
2021-04-08 19:30:00 +02:00
|
|
|
:param project_settings:
|
2021-03-14 16:01:15 +01:00
|
|
|
:param build_settings:
|
|
|
|
"""
|
2021-03-13 11:05:17 +01:00
|
|
|
FileSystemEventHandler.__init__(self)
|
|
|
|
|
2021-03-29 08:56:18 +02:00
|
|
|
self._env = env
|
2021-04-08 19:30:00 +02:00
|
|
|
self._project_settings = project_settings
|
2021-03-13 11:05:17 +01:00
|
|
|
self._build_settings = build_settings
|
|
|
|
|
2021-03-29 08:56:18 +02:00
|
|
|
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
|
|
|
|
|
2021-04-07 18:19:55 +02:00
|
|
|
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()
|
2021-04-08 19:30:00 +02:00
|
|
|
self._ls_thread = LiveServerThread(
|
|
|
|
self._project_settings.python_executable,
|
|
|
|
self._src_dir,
|
|
|
|
self._args,
|
|
|
|
self._env,
|
|
|
|
self._build_settings
|
|
|
|
)
|
2021-03-15 18:25:53 +01:00
|
|
|
self._ls_thread.start()
|
|
|
|
self._ls_thread.join()
|
|
|
|
Console.close()
|
|
|
|
|
2021-04-07 18:19:55 +02:00
|
|
|
def start(self, args: list[str]):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Starts the CPL live development server
|
2021-04-07 18:19:55 +02:00
|
|
|
:param args:
|
2021-03-14 16:01:15 +01:00
|
|
|
:return:
|
|
|
|
"""
|
2021-04-07 22:00:56 +02:00
|
|
|
if self._build_settings.main == '':
|
|
|
|
Console.error('Project has no entry point.')
|
|
|
|
return
|
|
|
|
|
2021-04-07 18:19:55 +02:00
|
|
|
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()
|