on_Binary__10_npm_install.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env python3
  2. """
  3. Install a binary using npm package manager.
  4. Usage: on_Binary__install_using_npm_provider.py --binary-id=<uuid> --machine-id=<uuid> --name=<name> [--custom-cmd=<cmd>]
  5. Output: Binary JSONL record to stdout after installation
  6. Environment variables:
  7. MACHINE_ID: Machine UUID (set by orchestrator)
  8. LIB_DIR: Library directory including machine type (e.g., data/lib/arm64-darwin) (required)
  9. """
  10. import json
  11. import os
  12. import sys
  13. from pathlib import Path
  14. import rich_click as click
  15. from abx_pkg import Binary, NpmProvider, BinProviderOverrides
  16. # Fix pydantic forward reference issue
  17. NpmProvider.model_rebuild()
  18. @click.command()
  19. @click.option('--machine-id', required=True, help="Machine UUID")
  20. @click.option('--binary-id', required=True, help="Dependency UUID")
  21. @click.option('--name', required=True, help="Binary name to install")
  22. @click.option('--binproviders', default='*', help="Allowed providers (comma-separated)")
  23. @click.option('--custom-cmd', default=None, help="Custom install command")
  24. @click.option('--overrides', default=None, help="JSON-encoded overrides dict")
  25. def main(binary_id: str, machine_id: str, name: str, binproviders: str, custom_cmd: str | None, overrides: str | None):
  26. """Install binary using npm."""
  27. if binproviders != '*' and 'npm' not in binproviders.split(','):
  28. click.echo(f"npm provider not allowed for {name}", err=True)
  29. sys.exit(0)
  30. # Get LIB_DIR from environment (required)
  31. # Note: LIB_DIR already includes machine type (e.g., data/lib/arm64-darwin)
  32. lib_dir = os.environ.get('LIB_DIR')
  33. if not lib_dir:
  34. click.echo("ERROR: LIB_DIR environment variable not set", err=True)
  35. sys.exit(1)
  36. # Structure: lib/arm64-darwin/npm (npm will create node_modules inside this)
  37. npm_prefix = Path(lib_dir) / 'npm'
  38. npm_prefix.mkdir(parents=True, exist_ok=True)
  39. # Use abx-pkg NpmProvider to install binary with custom prefix
  40. provider = NpmProvider(npm_prefix=npm_prefix)
  41. if not provider.INSTALLER_BIN:
  42. click.echo("npm not available on this system", err=True)
  43. sys.exit(1)
  44. click.echo(f"Installing {name} via npm to {npm_prefix}...", err=True)
  45. try:
  46. # Parse overrides if provided
  47. overrides_dict = None
  48. if overrides:
  49. try:
  50. overrides_dict = json.loads(overrides)
  51. click.echo(f"Using custom install overrides: {overrides_dict}", err=True)
  52. except json.JSONDecodeError:
  53. click.echo(f"Warning: Failed to parse overrides JSON: {overrides}", err=True)
  54. binary = Binary(name=name, binproviders=[provider], overrides=overrides_dict or {}).install()
  55. except Exception as e:
  56. click.echo(f"npm install failed: {e}", err=True)
  57. sys.exit(1)
  58. if not binary.abspath:
  59. click.echo(f"{name} not found after npm install", err=True)
  60. sys.exit(1)
  61. machine_id = os.environ.get('MACHINE_ID', '')
  62. # Output Binary JSONL record to stdout
  63. record = {
  64. 'type': 'Binary',
  65. 'name': name,
  66. 'abspath': str(binary.abspath),
  67. 'version': str(binary.version) if binary.version else '',
  68. 'sha256': binary.sha256 or '',
  69. 'binprovider': 'npm',
  70. 'machine_id': machine_id,
  71. 'binary_id': binary_id,
  72. }
  73. print(json.dumps(record))
  74. # Emit PATH update for npm bin dirs (node_modules/.bin preferred)
  75. npm_bin_dirs = [
  76. str(npm_prefix / 'node_modules' / '.bin'),
  77. str(npm_prefix / 'bin'),
  78. ]
  79. current_path = os.environ.get('PATH', '')
  80. path_dirs = current_path.split(':') if current_path else []
  81. new_path = current_path
  82. for npm_bin_dir in npm_bin_dirs:
  83. if npm_bin_dir and npm_bin_dir not in path_dirs:
  84. new_path = f"{npm_bin_dir}:{new_path}" if new_path else npm_bin_dir
  85. path_dirs.insert(0, npm_bin_dir)
  86. print(json.dumps({
  87. 'type': 'Machine',
  88. 'config': {
  89. 'PATH': new_path,
  90. },
  91. }))
  92. # Also emit NODE_MODULES_DIR for JS module resolution
  93. node_modules_dir = str(npm_prefix / 'node_modules')
  94. print(json.dumps({
  95. 'type': 'Machine',
  96. 'config': {
  97. 'NODE_MODULES_DIR': node_modules_dir,
  98. },
  99. }))
  100. # Log human-readable info to stderr
  101. click.echo(f"Installed {name} at {binary.abspath}", err=True)
  102. click.echo(f" version: {binary.version}", err=True)
  103. sys.exit(0)
  104. if __name__ == '__main__':
  105. main()