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:
- Check runtime:
pythonCli.CheckInstalled(ctx) → hard error with ErrorWithSuggestion + pythonCli.InstallUrl() if missing.
- Discover project:
DiscoverProjectFile(filepath.Dir(scriptPath), projectRoot, PythonProjectFiles).
- No project file found → skip venv and dependency install (standalone script is valid).
- 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:
- Prepare — runtime not installed → error with install suggestion
- Prepare — with
requirements.txt → venv + install called
- Prepare — with
pyproject.toml → venv + InstallProject called
- Prepare — both files →
pyproject.toml preferred
- Prepare — standalone (no project files) → no venv, no install
- Prepare — venv already exists → skip
CreateVirtualEnv, still run install (idempotent)
- Execute — with venv →
python.Cli.Run() called correctly
- Execute — standalone → Python called directly
- Execute — env vars passed through
Files: pkg/tools/language/python_executor.go (NEW), pkg/tools/language/python_executor_test.go (NEW)
Acceptance Criteria
References
Python Script Executor
Part of #7435 — Multi-Language Hook Support
User Story
As a template consumer running
azd upon 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):Prepare(ctx, scriptPath)method:pythonCli.CheckInstalled(ctx)→ hard error withErrorWithSuggestion+pythonCli.InstallUrl()if missing.DiscoverProjectFile(filepath.Dir(scriptPath), projectRoot, PythonProjectFiles)..venvin project dir if not exists viapythonCli.CreateVirtualEnv().pyproject.tomlexists →pythonCli.InstallProject().requirements.txtexists →pythonCli.InstallRequirements().projectDirandvenvNameforExecute().Execute(ctx, scriptPath, options)method:pythonCli.Run(ctx, projectDir, ".venv", envVars, scriptPath).exec.RunArgswithpython scriptPath, execute viacommandRunner.Run().options.Interactivecontrols stdin attachment.Create
pkg/tools/language/python_executor_test.go(NEW) — mockCommandRunner:requirements.txt→ venv + install calledpyproject.toml→ venv +InstallProjectcalledpyproject.tomlpreferredCreateVirtualEnv, still run install (idempotent)python.Cli.Run()called correctlyFiles:
pkg/tools/language/python_executor.go(NEW),pkg/tools/language/python_executor_test.go(NEW)Acceptance Criteria
python.Climethods — no reimplementation of venv activationReferences