2021-03-12 22:53:02 +01:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import threading
|
|
|
|
from datetime import datetime
|
|
|
|
|
2021-03-15 18:25:53 +01:00
|
|
|
from cpl.console.console import Console
|
|
|
|
from cpl.console.foreground_color_enum import ForegroundColorEnum
|
2021-04-08 19:30:00 +02:00
|
|
|
from cpl.environment.application_environment_abc import ApplicationEnvironmentABC
|
2021-03-31 10:44:54 +02:00
|
|
|
from cpl_cli.configuration import BuildSettings
|
2021-03-12 22:53:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
class LiveServerThread(threading.Thread):
|
|
|
|
|
2021-04-08 19:30:00 +02:00
|
|
|
def __init__(self, executable: str, path: str, args: list[str], env: ApplicationEnvironmentABC,
|
|
|
|
build_settings: BuildSettings):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Thread to start the CPL project for the live development server
|
2021-04-08 19:30:00 +02:00
|
|
|
:param executable:
|
2021-03-14 16:01:15 +01:00
|
|
|
:param path:
|
2021-04-08 19:30:00 +02:00
|
|
|
:param args:
|
|
|
|
:param env:
|
|
|
|
:param build_settings:
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
2021-03-12 22:53:02 +01:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
|
2021-04-08 19:30:00 +02:00
|
|
|
self._executable = executable
|
2021-03-12 22:53:02 +01:00
|
|
|
self._path = path
|
2021-04-07 18:19:55 +02:00
|
|
|
self._args = args
|
2021-04-08 19:30:00 +02:00
|
|
|
self._env = env
|
|
|
|
self._build_settings = build_settings
|
2021-03-31 10:44:54 +02:00
|
|
|
|
2021-03-12 22:53:02 +01:00
|
|
|
self._main = ''
|
|
|
|
self._command = []
|
2021-03-31 10:44:54 +02:00
|
|
|
|
2021-03-12 22:53:02 +01:00
|
|
|
@property
|
|
|
|
def command(self) -> list[str]:
|
|
|
|
return self._command
|
2021-03-31 10:44:54 +02:00
|
|
|
|
2021-03-12 22:53:02 +01:00
|
|
|
@property
|
|
|
|
def main(self) -> str:
|
|
|
|
return self._main
|
|
|
|
|
|
|
|
def run(self):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Starts the CPL project
|
|
|
|
:return:
|
|
|
|
"""
|
2021-04-07 22:00:56 +02:00
|
|
|
main = self._build_settings.main
|
|
|
|
if '.' in self._build_settings.main:
|
|
|
|
length = len(self._build_settings.main.split('.')) - 1
|
|
|
|
main = self._build_settings.main.split('.')[length]
|
|
|
|
|
2021-03-31 10:44:54 +02:00
|
|
|
self._main = os.path.join(self._path, f'{main}.py')
|
2021-03-12 22:53:02 +01:00
|
|
|
if not os.path.isfile(self._main):
|
2021-03-13 11:05:17 +01:00
|
|
|
Console.error('Entry point main.py not found')
|
2021-03-12 22:53:02 +01:00
|
|
|
return
|
|
|
|
|
2021-03-31 10:44:54 +02:00
|
|
|
env_vars = os.environ
|
2021-03-31 11:41:16 +02:00
|
|
|
if sys.platform == 'win32':
|
2021-04-08 19:30:00 +02:00
|
|
|
env_vars['PYTHONPATH'] = f'{self._env.working_directory};' \
|
|
|
|
f'{os.path.join(self._env.working_directory, self._build_settings.source_path)}'
|
2021-03-31 11:41:16 +02:00
|
|
|
else:
|
2021-04-08 19:30:00 +02:00
|
|
|
env_vars['PYTHONPATH'] = f'{self._env.working_directory}:' \
|
|
|
|
f'{os.path.join(self._env.working_directory, self._build_settings.source_path)}'
|
2021-03-31 10:44:54 +02:00
|
|
|
|
2021-04-07 22:11:22 +02:00
|
|
|
Console.set_foreground_color(ForegroundColorEnum.green)
|
|
|
|
Console.write_line('Read successfully')
|
|
|
|
Console.set_foreground_color(ForegroundColorEnum.cyan)
|
|
|
|
now = datetime.now()
|
|
|
|
Console.write_line(f'Started at {now.strftime("%Y-%m-%d %H:%M:%S")}\n\n')
|
|
|
|
Console.set_foreground_color(ForegroundColorEnum.default)
|
|
|
|
|
2021-04-08 19:30:00 +02:00
|
|
|
os.chdir(self._env.working_directory)
|
|
|
|
self._command = [self._executable, self._main, ''.join(self._args)]
|
2021-03-31 10:44:54 +02:00
|
|
|
subprocess.run(self._command, env=env_vars)
|