| 1 | #!/usr/bin/env python3 |
| 2 | """Install dependencies on the storage host; run programs and build hooks on Mac.""" |
| 3 | |
| 4 | import json |
| 5 | import os |
| 6 | from pathlib import Path |
| 7 | import re |
| 8 | import subprocess |
| 9 | import sys |
| 10 | |
| 11 | sys.path.insert(0, str(Path(__file__).resolve().parent)) |
| 12 | import client |
| 13 | |
| 14 | |
| 15 | def main(): |
| 16 | native = Path(sys.argv[0]).absolute().with_name("pnpm-real") |
| 17 | args = sys.argv[1:] |
| 18 | cwd, forwarded, command = client.pnpm_arguments(args, Path.cwd()) |
| 19 | prepare = command in ("run", "run-script", "exec", "test", "t", "tst", "start", "stop", "restart") |
| 20 | if not prepare and command and cwd.is_relative_to(client.ROOT): |
| 21 | for parent in (cwd, *cwd.parents): |
| 22 | if parent == client.ROOT: |
| 23 | break |
| 24 | manifest = parent / "package.json" |
| 25 | if manifest.is_file(): |
| 26 | prepare = command in json.loads(manifest.read_text()).get("scripts", {}) |
| 27 | break |
| 28 | if forwarded or prepare: |
| 29 | custom_environment = [key for key, value in os.environ.items() if value and ( |
| 30 | (key.lower().startswith(("npm_config_", "pnpm_config_")) |
| 31 | and key.lower() not in ("npm_config_prefix", "npm_config_cache", "npm_config_user_agent", |
| 32 | "pnpm_config_update_notifier")) |
| 33 | or key in ("VP_PACKAGE_MANAGER", "NODE_PATH") |
| 34 | or (key == "NODE_OPTIONS" and not re.fullmatch( |
| 35 | r"(?:\s*--max[-_](?:old|semi)[-_]space[-_]size=\d+)+\s*", value)))] |
| 36 | if custom_environment and cwd.is_relative_to(client.ROOT) and client.host(): |
| 37 | print("pnpm: Using Mac installation: custom environment (" + |
| 38 | ", ".join(sorted(custom_environment)) + ").", file=sys.stderr) |
| 39 | if not custom_environment and cwd.is_relative_to(client.ROOT) and client.host(): |
| 40 | client.inside(cwd) |
| 41 | request = dict(command="pnpm", cwd=str(cwd), engine=client.engine(), args=forwarded or []) |
| 42 | if os.environ.get("NODE_ENV") == "production": |
| 43 | request["node_env"] = "production" |
| 44 | if prepare: |
| 45 | request["prepare"] = True |
| 46 | else: |
| 47 | print("pnpm: Installing dependencies on the storage host.", file=sys.stderr, flush=True) |
| 48 | result = client.request(request) |
| 49 | if result.returncode: |
| 50 | return result.returncode |
| 51 | response = json.loads(result.stdout) |
| 52 | if response.get("ready") is True: |
| 53 | os.execv(native, [str(native), "--config.verify-deps-before-run=false", *args]) |
| 54 | if response.get("installed") is True: |
| 55 | if prepare or "--lockfile-only" not in forwarded: |
| 56 | client.finish_install(Path(response.get("workspace", cwd)), native, |
| 57 | ignore_scripts=not prepare and "--ignore-scripts" in forwarded, |
| 58 | modules_hash=response.get("modules_hash"), cwd=cwd, |
| 59 | args=forwarded or ()) |
| 60 | if prepare: |
| 61 | os.execv(native, [str(native), "--config.verify-deps-before-run=false", *args]) |
| 62 | return 0 |
| 63 | print("pnpm: Using Mac installation: " + response["reason"], file=sys.stderr) |
| 64 | store = response.get("store") |
| 65 | modules = cwd / "node_modules/.modules.yaml" |
| 66 | if modules.is_file(): |
| 67 | try: |
| 68 | current = json.loads(modules.read_text())["storeDir"] |
| 69 | except (ValueError, KeyError): |
| 70 | pass |
| 71 | else: |
| 72 | if isinstance(current, str) and Path(current).is_absolute(): |
| 73 | store = str(Path(current).parent) |
| 74 | if store: |
| 75 | args = [*args, "--store-dir=" + store] |
| 76 | os.execv(native, [str(native), *args]) |
| 77 | |
| 78 | |
| 79 | if __name__ == "__main__": |
| 80 | try: |
| 81 | sys.exit(main()) |
| 82 | except StopIteration: |
| 83 | os.execv(Path(sys.argv[0]).absolute().with_name("pnpm-real"), ["pnpm", *sys.argv[1:]]) |
| 84 | except KeyboardInterrupt: |
| 85 | sys.exit(130) |
| 86 | except subprocess.CalledProcessError as error: |
| 87 | sys.exit(error.returncode) |
| 88 | except (OSError, ValueError, KeyError) as error: |
| 89 | print(f"pnpm: {error}", file=sys.stderr) |
| 90 | sys.exit(1) |