platyvenv should generate (and document) a run.sh helper script.

This commit is contained in:
Fabio Manganiello 2023-08-27 23:20:03 +02:00
parent 9e6430a9ac
commit 86e5f74645
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -13,6 +13,7 @@ import shutil
import subprocess import subprocess
import sys import sys
import tempfile import tempfile
import textwrap
from typing import Generator, Sequence from typing import Generator, Sequence
import venv import venv
@ -131,6 +132,35 @@ class VenvBuilder:
subprocess.call([*self._pip_cmd, *pip_deps]) subprocess.call([*self._pip_cmd, *pip_deps])
def _generate_run_sh(self) -> str:
"""
Generate a ``run.sh`` script to run the application from a newly built
virtual environment.
:return: The location of the generated ``run.sh`` script.
"""
run_sh_path = os.path.join(self.output_dir, 'bin', 'run.sh')
with open(run_sh_path, 'w') as run_sh:
run_sh.write(
textwrap.dedent(
f"""
#!/bin/bash
cd {self.output_dir}
# Activate the virtual environment
source bin/activate
# Run the application with the configuration file passed
# during build
platypush -c {self.cfgfile} $*
"""
)
)
os.chmod(run_sh_path, 0o750)
return run_sh_path
def build(self): def build(self):
""" """
Build a Dockerfile based on a configuration file. Build a Dockerfile based on a configuration file.
@ -148,9 +178,10 @@ class VenvBuilder:
self._prepare_venv() self._prepare_venv()
self._install_extra_pip_packages(deps) self._install_extra_pip_packages(deps)
run_sh_path = self._generate_run_sh()
print( print(
f'\nVirtual environment created at {self.output_dir}.\n' f'\nVirtual environment created under {self.output_dir}.\n'
f'Run source {os.path.join(self.output_dir, "bin", "activate")} to activate it.' f'You can run the application through the {run_sh_path} script.\n'
) )
@classmethod @classmethod