|
9 | 9 | from concurrent.futures.thread import ThreadPoolExecutor |
10 | 10 | from subprocess import PIPE, STDOUT, Popen |
11 | 11 | from time import sleep |
12 | | -from typing import Any, AnyStr, Callable, Dict, Optional, Union |
| 12 | +from typing import Any, AnyStr, Callable, Dict, List, Optional, Union |
13 | 13 |
|
14 | 14 | from samcli.commands.exceptions import UserException |
15 | 15 | from samcli.lib.utils.stream_writer import StreamWriter |
16 | 16 |
|
| 17 | +# Environment variables that control library loading paths |
| 18 | +# These are set by PyInstaller and can cause conflicts with system binaries |
| 19 | +LIBRARY_PATH_VARS = [ |
| 20 | + "LD_LIBRARY_PATH", # Linux |
| 21 | + "DYLD_LIBRARY_PATH", # macOS |
| 22 | + "DYLD_FALLBACK_LIBRARY_PATH", # macOS fallback |
| 23 | + "DYLD_FRAMEWORK_PATH", # macOS frameworks |
| 24 | +] |
| 25 | + |
| 26 | +# Original library paths before cleanup (stored for debugging/restoration if needed) |
| 27 | +# Using a mutable container to avoid global statement (PLW0603) |
| 28 | +_library_path_state: Dict[str, Optional[Dict[str, str]]] = {"original_library_paths": None} |
| 29 | + |
17 | 30 | # These are the bytes that used as a prefix for a some string to color them in red to show errors. |
18 | 31 | TERRAFORM_ERROR_PREFIX = [27, 91, 51, 49] |
19 | 32 |
|
@@ -148,3 +161,163 @@ def _check_and_process_bytes(check_value: AnyStr, preserve_whitespace=False) -> |
148 | 161 | return decoded_value |
149 | 162 | return decoded_value.strip() |
150 | 163 | return check_value |
| 164 | + |
| 165 | + |
| 166 | +def is_pyinstaller_bundle() -> bool: |
| 167 | + """ |
| 168 | + Check if SAM CLI is running from a PyInstaller bundle. |
| 169 | +
|
| 170 | + PyInstaller sets the '_MEIPASS' attribute on the sys module to point |
| 171 | + to the temporary directory where bundled files are extracted. |
| 172 | +
|
| 173 | + Returns |
| 174 | + ------- |
| 175 | + bool |
| 176 | + True if running from a PyInstaller bundle, False otherwise. |
| 177 | + """ |
| 178 | + return hasattr(sys, "_MEIPASS") |
| 179 | + |
| 180 | + |
| 181 | +def get_pyinstaller_lib_path() -> Optional[str]: |
| 182 | + """ |
| 183 | + Get the PyInstaller internal library path if running from a bundle. |
| 184 | +
|
| 185 | + Returns |
| 186 | + ------- |
| 187 | + Optional[str] |
| 188 | + The path to PyInstaller's internal libraries, or None if not in a bundle. |
| 189 | + """ |
| 190 | + if is_pyinstaller_bundle(): |
| 191 | + meipass = getattr(sys, "_MEIPASS", None) |
| 192 | + if meipass: |
| 193 | + return os.path.join(meipass, "_internal") |
| 194 | + return None |
| 195 | + |
| 196 | + |
| 197 | +def _save_original_library_paths() -> None: |
| 198 | + """Save original library path values before modification.""" |
| 199 | + if _library_path_state["original_library_paths"] is None: |
| 200 | + original_paths: Dict[str, str] = {} |
| 201 | + for var in LIBRARY_PATH_VARS: |
| 202 | + if var in os.environ: |
| 203 | + original_paths[var] = os.environ[var] |
| 204 | + _library_path_state["original_library_paths"] = original_paths |
| 205 | + |
| 206 | + |
| 207 | +def _filter_pyinstaller_paths(path_value: str) -> str: |
| 208 | + """ |
| 209 | + Remove PyInstaller-related paths from a PATH-style environment variable. |
| 210 | +
|
| 211 | + Parameters |
| 212 | + ---------- |
| 213 | + path_value : str |
| 214 | + The original value of the path variable (colon or semicolon separated). |
| 215 | +
|
| 216 | + Returns |
| 217 | + ------- |
| 218 | + str |
| 219 | + The filtered path value with PyInstaller paths removed. |
| 220 | + """ |
| 221 | + meipass = getattr(sys, "_MEIPASS", None) |
| 222 | + if not meipass: |
| 223 | + return path_value |
| 224 | + |
| 225 | + separator = os.pathsep |
| 226 | + paths = path_value.split(separator) |
| 227 | + |
| 228 | + # Filter out paths that are inside the PyInstaller bundle |
| 229 | + # Using specific patterns to avoid filtering legitimate system paths |
| 230 | + filtered_paths = [ |
| 231 | + p for p in paths if not (p.startswith(meipass) or p.endswith("/_internal") or "dist/_internal" in p) |
| 232 | + ] |
| 233 | + |
| 234 | + return separator.join(filtered_paths) |
| 235 | + |
| 236 | + |
| 237 | +def isolate_library_paths_for_subprocess() -> None: |
| 238 | + """ |
| 239 | + Remove or filter PyInstaller-bundled library paths from the environment. |
| 240 | +
|
| 241 | + This function should be called early in SAM CLI initialization when running |
| 242 | + from a PyInstaller bundle. It ensures that external processes (npm, node, pip, etc.) |
| 243 | + use system libraries instead of the bundled ones. |
| 244 | +
|
| 245 | + This is safe to call because: |
| 246 | + 1. Python and its C extensions are already loaded |
| 247 | + 2. The bundled libraries have served their purpose for the main process |
| 248 | + 3. External processes need system libraries for compatibility |
| 249 | +
|
| 250 | + Note: This modifies os.environ directly, affecting all subprocess calls |
| 251 | + that inherit the environment. |
| 252 | + """ |
| 253 | + if not is_pyinstaller_bundle(): |
| 254 | + LOG.debug("Not running from PyInstaller bundle, skipping library path isolation") |
| 255 | + return |
| 256 | + |
| 257 | + _save_original_library_paths() |
| 258 | + |
| 259 | + pyinstaller_path = get_pyinstaller_lib_path() |
| 260 | + LOG.debug("Running from PyInstaller bundle at: %s", getattr(sys, "_MEIPASS", "unknown")) |
| 261 | + LOG.debug("PyInstaller internal lib path: %s", pyinstaller_path) |
| 262 | + |
| 263 | + for var in LIBRARY_PATH_VARS: |
| 264 | + if var in os.environ: |
| 265 | + original_value = os.environ[var] |
| 266 | + filtered_value = _filter_pyinstaller_paths(original_value) |
| 267 | + |
| 268 | + if filtered_value: |
| 269 | + os.environ[var] = filtered_value |
| 270 | + LOG.debug("Filtered %s: '%s' -> '%s'", var, original_value, filtered_value) |
| 271 | + else: |
| 272 | + del os.environ[var] |
| 273 | + LOG.debug("Removed %s (was: '%s')", var, original_value) |
| 274 | + |
| 275 | + |
| 276 | +def get_clean_env_for_subprocess(additional_vars_to_remove: Optional[List[str]] = None) -> Dict[str, str]: |
| 277 | + """ |
| 278 | + Get a copy of the current environment with library paths cleaned for subprocess use. |
| 279 | +
|
| 280 | + This is useful when you need to pass an explicit environment to subprocess calls |
| 281 | + rather than relying on inheritance from os.environ. |
| 282 | +
|
| 283 | + Parameters |
| 284 | + ---------- |
| 285 | + additional_vars_to_remove : Optional[List[str]] |
| 286 | + Additional environment variables to remove from the returned environment. |
| 287 | +
|
| 288 | + Returns |
| 289 | + ------- |
| 290 | + Dict[str, str] |
| 291 | + A copy of os.environ with library paths filtered/removed. |
| 292 | + """ |
| 293 | + env = os.environ.copy() |
| 294 | + |
| 295 | + if is_pyinstaller_bundle(): |
| 296 | + for var in LIBRARY_PATH_VARS: |
| 297 | + if var in env: |
| 298 | + filtered = _filter_pyinstaller_paths(env[var]) |
| 299 | + if filtered: |
| 300 | + env[var] = filtered |
| 301 | + else: |
| 302 | + del env[var] |
| 303 | + |
| 304 | + if additional_vars_to_remove: |
| 305 | + for var in additional_vars_to_remove: |
| 306 | + env.pop(var, None) |
| 307 | + |
| 308 | + return env |
| 309 | + |
| 310 | + |
| 311 | +def get_original_library_paths() -> Dict[str, str]: |
| 312 | + """ |
| 313 | + Get the original library path values before isolation was applied. |
| 314 | +
|
| 315 | + This can be useful for debugging or if restoration is ever needed. |
| 316 | +
|
| 317 | + Returns |
| 318 | + ------- |
| 319 | + Dict[str, str] |
| 320 | + Dictionary of original library path environment variables. |
| 321 | + """ |
| 322 | + original = _library_path_state["original_library_paths"] |
| 323 | + return dict(original) if original else {} |
0 commit comments