Skip to content

Python script executor #7440

Description

@wbreza

Python Script Executor

Part of #7435 — Multi-Language Hook Support

User Story

As a template consumer running azd up on a Python-based template, I want Python hook scripts to automatically install their dependencies and execute correctly so I don't need to manually set up virtual environments or run pip install.

Solution Approach

Create pkg/tools/language/python_executor.go (NEW):

type pythonExecutor struct {
    pythonCli     *python.Cli
    commandRunner exec.CommandRunner
    console       input.Console
    projectRoot   string
}

Prepare(ctx, scriptPath) method:

  1. Check runtime: pythonCli.CheckInstalled(ctx) → hard error with ErrorWithSuggestion + pythonCli.InstallUrl() if missing.
  2. Discover project: DiscoverProjectFile(filepath.Dir(scriptPath), projectRoot, PythonProjectFiles).
  3. No project file found → skip venv and dependency install (standalone script is valid).
  4. Project file found:
    • Create .venv in project dir if not exists via pythonCli.CreateVirtualEnv().
    • If pyproject.toml exists → pythonCli.InstallProject().
    • Else if requirements.txt exists → pythonCli.InstallRequirements().
    • Store projectDir and venvName for Execute().

Execute(ctx, scriptPath, options) method:

  • With venv: pythonCli.Run(ctx, projectDir, ".venv", envVars, scriptPath).
  • Standalone: construct exec.RunArgs with python scriptPath, execute via commandRunner.Run().
  • Environment variables from hook options passed through to Python process.
  • options.Interactive controls stdin attachment.

Create pkg/tools/language/python_executor_test.go (NEW) — mock CommandRunner:

  1. Prepare — runtime not installed → error with install suggestion
  2. Prepare — with requirements.txt → venv + install called
  3. Prepare — with pyproject.toml → venv + InstallProject called
  4. Prepare — both files → pyproject.toml preferred
  5. Prepare — standalone (no project files) → no venv, no install
  6. Prepare — venv already exists → skip CreateVirtualEnv, still run install (idempotent)
  7. Execute — with venv → python.Cli.Run() called correctly
  8. Execute — standalone → Python called directly
  9. Execute — env vars passed through

Files: pkg/tools/language/python_executor.go (NEW), pkg/tools/language/python_executor_test.go (NEW)

Acceptance Criteria

  • All unit tests pass
  • Missing Python runtime → clear, actionable error message with install URL
  • Dependencies installed before script execution
  • Virtual environment created in project directory (not temp)
  • Standalone scripts (no requirements.txt/pyproject.toml) work without venv
  • Reuses existing python.Cli methods — no reimplementation of venv activation

References

Metadata

Metadata

Assignees

Labels

area/hooksLifecycle hooksarea/pythonPython language supportarea/toolsExternal tools (Docker, npm, Python)featureFeature request

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions