2022-05-25 13:43:16 +02:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from cpl_core.console.console import Console
|
|
|
|
from cpl_core.console.foreground_color_enum import ForegroundColorEnum
|
|
|
|
from cpl_core.environment.application_environment_abc import ApplicationEnvironmentABC
|
2022-12-10 21:43:46 +01:00
|
|
|
from cpl_cli.configuration.build_settings import BuildSettings
|
2022-05-25 13:43:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
class StartExecutable:
|
|
|
|
def __init__(self, env: ApplicationEnvironmentABC, build_settings: BuildSettings):
|
|
|
|
"""
|
|
|
|
Service to start the CPL project for the live development server
|
|
|
|
:param env:
|
|
|
|
:param build_settings:
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._executable = None
|
|
|
|
|
|
|
|
self._env = env
|
|
|
|
self._build_settings = build_settings
|
|
|
|
|
2023-02-20 15:55:20 +01:00
|
|
|
self._main = ""
|
2022-05-25 13:43:16 +02:00
|
|
|
self._command = []
|
|
|
|
self._env_vars = os.environ
|
|
|
|
|
|
|
|
self._set_venv()
|
|
|
|
|
|
|
|
def _set_venv(self):
|
|
|
|
if self._executable is None or self._executable == sys.executable:
|
|
|
|
return
|
|
|
|
|
|
|
|
path = os.path.abspath(os.path.dirname(os.path.dirname(self._executable)))
|
2023-02-20 15:55:20 +01:00
|
|
|
if sys.platform == "win32":
|
|
|
|
self._env_vars["PATH"] = f"{path}\\bin" + os.pathsep + os.environ.get("PATH", "")
|
2022-05-25 13:43:16 +02:00
|
|
|
else:
|
2023-02-20 15:55:20 +01:00
|
|
|
self._env_vars["PATH"] = f"{path}/bin" + os.pathsep + os.environ.get("PATH", "")
|
2022-05-25 13:43:16 +02:00
|
|
|
|
2023-02-20 15:55:20 +01:00
|
|
|
self._env_vars["VIRTUAL_ENV"] = path
|
2022-05-25 13:43:16 +02:00
|
|
|
|
|
|
|
def run(self, args: list[str], executable: str, path: str, output=True):
|
2022-12-10 21:43:46 +01:00
|
|
|
self._executable = os.path.abspath(os.path.join(self._env.working_directory, executable))
|
|
|
|
if not os.path.exists(self._executable):
|
2023-02-20 15:55:20 +01:00
|
|
|
Console.error(f"Executable not found")
|
2022-12-10 21:43:46 +01:00
|
|
|
return
|
2022-05-25 13:43:16 +02:00
|
|
|
|
|
|
|
main = self._build_settings.main
|
2023-02-20 15:55:20 +01:00
|
|
|
if "." in self._build_settings.main:
|
|
|
|
length = len(self._build_settings.main.split(".")) - 1
|
|
|
|
main = self._build_settings.main.split(".")[length]
|
2022-05-25 13:43:16 +02:00
|
|
|
|
2023-02-20 15:55:20 +01:00
|
|
|
self._main = os.path.join(path, f"{main}.py")
|
2022-05-25 13:43:16 +02:00
|
|
|
if not os.path.isfile(self._main):
|
2023-02-20 15:55:20 +01:00
|
|
|
Console.error("Entry point main.py not found")
|
2022-05-25 13:43:16 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
# set cwd to src/
|
|
|
|
self._env.set_working_directory(os.path.abspath(os.path.join(path)))
|
2023-02-20 15:55:20 +01:00
|
|
|
src_cwd = os.path.abspath(os.path.join(path, "../"))
|
|
|
|
if sys.platform == "win32":
|
|
|
|
self._env_vars["PYTHONPATH"] = (
|
|
|
|
f"{src_cwd};" f"{os.path.join(self._env.working_directory, self._build_settings.source_path)}"
|
|
|
|
)
|
2022-05-25 13:43:16 +02:00
|
|
|
else:
|
2023-02-20 15:55:20 +01:00
|
|
|
self._env_vars["PYTHONPATH"] = (
|
|
|
|
f"{src_cwd}:" f"{os.path.join(self._env.working_directory, self._build_settings.source_path)}"
|
|
|
|
)
|
2022-05-25 13:43:16 +02:00
|
|
|
|
|
|
|
if output:
|
|
|
|
Console.set_foreground_color(ForegroundColorEnum.green)
|
2023-02-20 15:55:20 +01:00
|
|
|
Console.write_line("Read successfully")
|
2022-05-25 13:43:16 +02:00
|
|
|
Console.set_foreground_color(ForegroundColorEnum.cyan)
|
|
|
|
Console.write_line(f'Started at {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}\n\n')
|
|
|
|
Console.set_foreground_color(ForegroundColorEnum.default)
|
|
|
|
|
|
|
|
self._command = [self._executable, self._main]
|
|
|
|
# if len(self._args) > 0:
|
|
|
|
# self._command.append(' '.join(self._args))
|
|
|
|
for arg in args:
|
|
|
|
self._command.append(arg)
|
|
|
|
|
|
|
|
subprocess.run(self._command, env=self._env_vars)
|