Class: Daytona::SandboxPythonCodeToolbox

Inherits:
Object
  • Object
show all
Defined in:
lib/daytona/code_toolbox/sandbox_python_code_toolbox.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Instance Method Details

#get_run_command(code, params = nil) ⇒ String

Get the run command for executing Python code

Parameters:

  • code (String)

    The Python code to execute

  • params (Daytona::CodeRunParams, nil) (defaults to: nil)

    Optional parameters for code execution

Returns:

  • (String)

    The command to run the Python code



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/daytona/code_toolbox/sandbox_python_code_toolbox.rb', line 12

def get_run_command(code, params = nil)
  encoded_code = Base64.strict_encode64(code)

  # Override plt.show() method if matplotlib is imported
  if matplotlib_imported?(code)
    encoded_code = Base64.strict_encode64(
      Base64.decode64(PYTHON_CODE_WRAPPER).gsub('{encoded_code}', encoded_code)
    )
  end

  argv = params&.argv&.join(' ') || ''

  # Pipe the base64-encoded code via stdin to avoid OS ARG_MAX limits on large payloads
  # printf is a shell builtin that does not invoke execve(), so the base64 string bypasses the kernel ARG_MAX limit
  # Use -u flag to ensure unbuffered output for real-time error reporting
  "printf '%s' '#{encoded_code}' | base64 -d | python3 -u - #{argv}"
end