Improved live dev server to use venv

This commit is contained in:
Sven Heidemann 2021-04-09 20:42:15 +02:00
parent 98117ed7ad
commit 4bfc647b91

View File

@ -24,7 +24,8 @@ class LiveServerThread(threading.Thread):
""" """
threading.Thread.__init__(self) threading.Thread.__init__(self)
self._executable = executable self._executable = os.path.abspath(executable)
self._path = path self._path = path
self._args = args self._args = args
self._env = env self._env = env
@ -32,6 +33,9 @@ class LiveServerThread(threading.Thread):
self._main = '' self._main = ''
self._command = [] self._command = []
self._env_vars = os.environ
self._set_venv()
@property @property
def command(self) -> list[str]: def command(self) -> list[str]:
@ -41,6 +45,16 @@ class LiveServerThread(threading.Thread):
def main(self) -> str: def main(self) -> str:
return self._main return self._main
def _set_venv(self):
if self._executable != sys.executable:
path = os.path.abspath(os.path.dirname(os.path.dirname(self._executable)))
if sys.platform == 'win32':
self._env_vars['PATH'] = f'{path}\\bin' + os.pathsep + os.environ.get('PATH', '')
else:
self._env_vars['PATH'] = f'{path}/bin' + os.pathsep + os.environ.get('PATH', '')
self._env_vars['VIRTUAL_ENV'] = path
def run(self): def run(self):
""" """
Starts the CPL project Starts the CPL project
@ -56,12 +70,11 @@ class LiveServerThread(threading.Thread):
Console.error('Entry point main.py not found') Console.error('Entry point main.py not found')
return return
env_vars = os.environ
if sys.platform == 'win32': if sys.platform == 'win32':
env_vars['PYTHONPATH'] = f'{self._env.working_directory};' \ self._env_vars['PYTHONPATH'] = f'{self._env.working_directory};' \
f'{os.path.join(self._env.working_directory, self._build_settings.source_path)}' f'{os.path.join(self._env.working_directory, self._build_settings.source_path)}'
else: else:
env_vars['PYTHONPATH'] = f'{self._env.working_directory}:' \ self._env_vars['PYTHONPATH'] = f'{self._env.working_directory}:' \
f'{os.path.join(self._env.working_directory, self._build_settings.source_path)}' f'{os.path.join(self._env.working_directory, self._build_settings.source_path)}'
Console.set_foreground_color(ForegroundColorEnum.green) Console.set_foreground_color(ForegroundColorEnum.green)
@ -73,4 +86,4 @@ class LiveServerThread(threading.Thread):
os.chdir(self._env.working_directory) os.chdir(self._env.working_directory)
self._command = [self._executable, self._main, ''.join(self._args)] self._command = [self._executable, self._main, ''.join(self._args)]
subprocess.run(self._command, env=env_vars) subprocess.run(self._command, env=self._env_vars)