proud-dentist-22844
12/30/2024, 4:53 PMpex_binary(name="st2.pex", execution_mode="venv", include_tools=True, venv_hermetic_scripts=False)
Is there a way to configure the pex to run a shell script when executed?
It looks like entry_point
, executable
, and script
all require python.
My goal: I want to turn the pex into some kind of "self-extracting installer". So, running ./st2.pex
will actually run the pex-tools command required to build a venv from the pex:
PEX_TOOLs=1 ./st2.pex venv --system-site-packages --non-hermetic-scripts --prompt st2 /opt/stackstorm/st2
square-psychiatrist-19087
12/30/2024, 6:54 PMmakeself
backend: https://www.pantsbuild.org/stable/docs/shell/self-extractable-archivesproud-dentist-22844
12/30/2024, 7:15 PMmakeself
archive.square-psychiatrist-19087
12/30/2024, 7:25 PMproud-dentist-22844
12/30/2024, 8:49 PMpex_binary(..., dependencies=["./pex_preamble.py", ...], extra_build_args=("--preamble-file", f"source_files/{build_file_dir()}/pex_preamble.py"))
Where this is (basically) the contents of pex_preamble.py
import os
import sys
# This check makes re-exec safe by ensuring we modify env+argv once.
if os.environ.pop("ST2_PEX_EXTRACT", "0") not in ("1", "skip"):
os.environ["ST2_PEX_EXTRACT"] = "1"
st2_base_path = "/opt/stackstorm"
st2_venv = os.path.join(st2_base_path, "st2")
if os.path.exists(st2_venv):
print(f"WARNING: This will overwrite {st2_venv}.", file=sys.stderr)
# This env var and sys.argv will create a venv in the st2_venv dir.
os.environ["PEX_TOOLS"] = "1"
sys.argv[1:1] = (
"venv",
"--force", # remove and replace the venv if it exists
"--non-hermetic-scripts", # do not add -sE to python shebang
"--system-site-packages",
"--prompt=st2",
st2_venv,
)