File size: 3,876 Bytes
6cda6d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
 * Resolve an explicit Python runtime for a sandboxed render.
 *
 * The renderer must come from an explicit executable or the project's exact
 * `.venv`; a local venv is mounted read-only as one directory, never its parent.
 */

import fs from 'node:fs'
import path from 'node:path'

export interface PythonRuntime {
  executable: string
  /** Exact directories that must be mounted read-only inside the sandbox. */
  mountDirectories: string[]
  /** PATH entries that make the runtime and its sibling tools available. */
  pathEntries: string[]
}

function isExecutableFile(candidate: string): boolean {
  try {
    const stat = fs.statSync(candidate)
    if (!stat.isFile()) return false
    fs.accessSync(candidate, fs.constants.X_OK)
    return true
  } catch {
    return false
  }
}

function resolveFromPath(command: string): string | undefined {
  if (command.includes('/') || command.includes('\\')) {
    const resolved = path.resolve(command)
    return isExecutableFile(resolved) ? resolved : undefined
  }
  for (const entry of (process.env.PATH || '').split(path.delimiter).filter(Boolean)) {
    const candidate = path.join(entry, command)
    if (isExecutableFile(candidate)) return path.resolve(candidate)
  }
  return undefined
}

function venvRootForExecutable(executable: string): string | undefined {
  const binDirectory = path.dirname(executable)
  const binName = path.basename(binDirectory)
  if (binName !== 'bin' && binName !== 'Scripts') return undefined
  const root = path.dirname(binDirectory)
  try {
    if (fs.statSync(path.join(root, 'pyvenv.cfg')).isFile()) return root
  } catch {
    // Not a virtual environment; the system runtime paths still apply.
  }
  return undefined
}

function runtimeForExecutable(executable: string): PythonRuntime {
  const resolved = path.resolve(executable)
  if (!isExecutableFile(resolved)) {
    throw new Error(`Configured Python runtime is missing or not executable: ${resolved}`)
  }

  const mountDirectories: string[] = []
  const venvRoot = venvRootForExecutable(resolved)
  if (venvRoot) {
    // Exact venv only. Mounting its parent would expose unrelated files.
    mountDirectories.push(venvRoot)
  }

  return {
    executable: resolved,
    mountDirectories: Array.from(new Set(mountDirectories)),
    pathEntries: Array.from(new Set([path.dirname(resolved), '/usr/local/bin', '/usr/bin', '/bin'])),
  }
}

/**
 * Resolution order: explicit option, named environment variable, project venv,
 * then an absolute PATH lookup. The result is always executed inside bwrap.
 */
export function resolvePythonRuntime(
  options: {
    explicit?: string
    envName?: string
    env?: NodeJS.ProcessEnv
  } = {}
): PythonRuntime {
  const env = options.env ?? process.env
  const explicit = options.explicit?.trim()
  if (explicit) {
    const resolved = resolveFromPath(explicit)
    if (!resolved) {
      throw new Error(`Configured Python runtime is missing or not executable: ${explicit}`)
    }
    return runtimeForExecutable(resolved)
  }

  if (options.envName) {
    const configured = env[options.envName]?.trim()
    if (configured) {
      const resolved = resolveFromPath(configured)
      if (!resolved) {
        throw new Error(
          `${options.envName} points to a missing or non-executable Python runtime: ${configured}`
        )
      }
      return runtimeForExecutable(resolved)
    }
  }

  const localPython = path.resolve(
    '.venv',
    process.platform === 'win32' ? 'Scripts/python.exe' : 'bin/python'
  )
  if (isExecutableFile(localPython)) {
    return runtimeForExecutable(localPython)
  }

  for (const candidate of ['python3', 'python']) {
    const resolved = resolveFromPath(candidate)
    if (resolved) return runtimeForExecutable(resolved)
  }

  throw new Error('No Python runtime found. Set MANIMGX_PYTHON or create .venv with requirements.txt.')
}