|
| 1 | +# Copyright 2025 The StackStorm Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# NOTE: In this script, all 3rd party deps are available thanks to pex. |
| 16 | +# Do not import any st2 code to avoid polluting the pex with extra files |
| 17 | +# (Pants uses dependency inference to add sources beyond our wheels). |
| 18 | + |
| 19 | +import os |
| 20 | +import sys |
| 21 | +import subprocess |
| 22 | + |
| 23 | +from pathlib import Path |
| 24 | +from typing import List, Optional |
| 25 | + |
| 26 | +from oslo_config import cfg |
| 27 | + |
| 28 | + |
| 29 | +def get_pex_path() -> str: |
| 30 | + return os.environ.get("PEX", sys.argv[0]) |
| 31 | + |
| 32 | + |
| 33 | +def get_st2_base_path(args: Optional[List[str]] = None) -> Path: |
| 34 | + st2_config_path = ( |
| 35 | + os.environ.get("ST2_CONFIG_PATH", os.environ.get("ST2_CONF")) |
| 36 | + or "/etc/st2/st2.conf" |
| 37 | + ) |
| 38 | + |
| 39 | + cfg.CONF.register_opts( |
| 40 | + [cfg.StrOpt("base_path", default="/opt/stackstorm")], group="system" |
| 41 | + ) |
| 42 | + |
| 43 | + try: |
| 44 | + cfg.CONF(args=args, default_config_files=[st2_config_path], use_env=False) |
| 45 | + except cfg.ConfigFilesNotFoundError: |
| 46 | + pass |
| 47 | + |
| 48 | + st2_base_path = os.environ.get( |
| 49 | + "ST2_SYSTEM__BASE_PATH", cfg.CONF["system"]["base_path"] |
| 50 | + ) |
| 51 | + return Path(st2_base_path) |
| 52 | + |
| 53 | + |
| 54 | +def unpack_venv(st2_venv_path: Path) -> int: |
| 55 | + if st2_venv_path.exists(): |
| 56 | + print(f"WARNING: This will overwrite {st2_venv_path}", file=sys.stderr) |
| 57 | + |
| 58 | + env = {"PEX_TOOLS": "1"} |
| 59 | + cmd = [ |
| 60 | + get_pex_path(), |
| 61 | + "venv", |
| 62 | + "--force", # remove and replace the venv if it exists |
| 63 | + "--non-hermetic-scripts", # do not add -sE to python shebang |
| 64 | + # st2-packages has a note about python symlinks breaking pack install. |
| 65 | + # uncomment this if that proves to still be an issue. |
| 66 | + # "--copies", # pack install follows python symlinks to find bin dir |
| 67 | + "--system-site-packages", |
| 68 | + "--compile", # pre-compile all pyc files |
| 69 | + "--prompt=st2", |
| 70 | + str(st2_venv_path), |
| 71 | + ] |
| 72 | + pretty_cmd = "".join(k + "=" + v + " " for k, v in env.items()) + " ".join(cmd) |
| 73 | + print(f"Now running: {pretty_cmd}", file=sys.stderr) |
| 74 | + |
| 75 | + result = subprocess.call(cmd, env=env) |
| 76 | + |
| 77 | + if result == 0: |
| 78 | + print(f"Successfully unpacked venv to {st2_venv_path}", file=sys.stderr) |
| 79 | + else: |
| 80 | + print( |
| 81 | + f"Encountered an error unpacking venv to {st2_venv_path}", file=sys.stderr |
| 82 | + ) |
| 83 | + |
| 84 | + return result |
| 85 | + |
| 86 | + |
| 87 | +def tidy_venv(st2_venv_path: Path) -> None: |
| 88 | + """Clean up and remove this script from the venv. |
| 89 | +
|
| 90 | + Unfortunately, the way pants uses pex, this script ends up in the venv. |
| 91 | + """ |
| 92 | + for path in (st2_venv_path / "lib").glob("python*"): |
| 93 | + script_path = path / "site-packages" / "packaging" / "build_st2_venv.py" |
| 94 | + if script_path.exists(): |
| 95 | + script_path.unlink() |
| 96 | + |
| 97 | + script_path = path / "site-packages" / "__pex_executable__.py" |
| 98 | + if script_path.exists(): |
| 99 | + script_path.unlink() |
| 100 | + |
| 101 | + # and remove the reference to this script |
| 102 | + main_path = st2_venv_path / "__main__.py" |
| 103 | + main_path.write_text(main_path.read_text().replace("__pex_executable__", "")) |
| 104 | + |
| 105 | + |
| 106 | +def main() -> int: |
| 107 | + st2_base_path = get_st2_base_path(sys.argv[1:]) |
| 108 | + st2_venv_path = st2_base_path / "st2" |
| 109 | + |
| 110 | + if not os.access(st2_base_path, os.W_OK): |
| 111 | + print( |
| 112 | + f"ERROR: venv parent directory is not writable: {st2_base_path}", |
| 113 | + file=sys.stderr, |
| 114 | + ) |
| 115 | + return 1 |
| 116 | + |
| 117 | + venv_result = unpack_venv(st2_venv_path) |
| 118 | + tidy_venv(st2_venv_path) |
| 119 | + |
| 120 | + return venv_result |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + sys.exit(main()) |
0 commit comments