Class PythonRunnerTask

java.lang.Object
org.gradle.api.internal.AbstractTask
org.gradle.api.DefaultTask
name.jurgenei.gradle.python.PythonRunnerTask
All Implemented Interfaces:
Comparable<org.gradle.api.Task>, org.gradle.api.internal.DynamicObjectAware, org.gradle.api.internal.TaskInternal, org.gradle.api.Named, org.gradle.api.plugins.ExtensionAware, org.gradle.api.Task, org.gradle.util.Configurable<org.gradle.api.Task>

public class PythonRunnerTask extends org.gradle.api.DefaultTask
Gradle task that executes a Python script from a project directory.

Execution model:

  • Creates a virtual environment under .venv if it does not exist.
  • Optionally installs dependencies from requirements.txt.
  • Skips dependency installation when the requirements file hash is unchanged.
  • Runs the configured script and fails the task on non-zero exit code.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    Process result for a script execution.

    Nested classes/interfaces inherited from interface org.gradle.api.Named

    org.gradle.api.Named.Namer
  • Field Summary

    Fields inherited from interface org.gradle.api.Task

    TASK_ACTION, TASK_CONSTRUCTOR_ARGS, TASK_DEPENDS_ON, TASK_DESCRIPTION, TASK_GROUP, TASK_NAME, TASK_OVERWRITE, TASK_TYPE
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns positional arguments passed to the script.
    Returns the Python executable used to create the virtual environment.
    Returns the requirements file used for dependency installation.
    Returns the script file to execute.
    Returns the working directory used for command execution and venv storage.
    Returns the configured working directory path for incremental input tracking.
    void
    Task entry point that prepares the environment and executes the script.
    void
    Sets positional arguments passed to the script.
    void
    setPythonExecutable(String pythonExecutable)
    Sets the Python executable used to create the virtual environment.
    void
    setRequirements(File requirements)
    Sets the requirements file for dependency installation.
    void
    setScript(File script)
    Sets the script file to execute.
    void
    setWorkDir(File workDir)
    Sets the working directory used for command execution and venv storage.

    Methods inherited from class org.gradle.api.DefaultTask

    compareTo, configure, dependsOn, doFirst, doFirst, doFirst, doLast, doLast, doLast, finalizedBy, getActions, getAnt, getDependsOn, getDescription, getDestroyables, getDidWork, getEnabled, getExtensions, getFinalizedBy, getGroup, getInputs, getLocalState, getLogger, getLogging, getMustRunAfter, getName, getOutputs, getPath, getProject, getShouldRunAfter, getState, getTaskDependencies, getTemporaryDir, getTimeout, hasProperty, mustRunAfter, onlyIf, onlyIf, onlyIf, property, setActions, setDependsOn, setDescription, setDidWork, setEnabled, setFinalizedBy, setGroup, setMustRunAfter, setOnlyIf, setOnlyIf, setOnlyIf, setProperty, setShouldRunAfter, shouldRunAfter, usesService

    Methods inherited from class org.gradle.api.internal.AbstractTask

    acceptServiceReferences, appendParallelSafeAction, doNotTrackState, getAsDynamicObject, getIdentityPath, getImpliesSubProjects, getLifecycleDependencies, getOnlyIf, getReasonNotToTrackState, getReasonTaskIsIncompatibleWithConfigurationCache, getRequiredServices, getServices, getSharedResources, getStandardOutputCapture, getTaskActions, getTaskIdentity, getTemporaryDirFactory, hasTaskActions, injectIntoNewInstance, isCompatibleWithConfigurationCache, isEnabled, isHasCustomActions, notCompatibleWithConfigurationCache, prependParallelSafeAction, setImpliesSubProjects

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.gradle.api.Task

    doNotTrackState, notCompatibleWithConfigurationCache
  • Constructor Details

    • PythonRunnerTask

      public PythonRunnerTask()
  • Method Details

    • runPython

      public void runPython() throws Exception
      Task entry point that prepares the environment and executes the script.
      Throws:
      Exception - if environment setup or script execution fails
    • getScript

      @InputFile public File getScript()
      Returns the script file to execute.
      Returns:
      configured Python script
    • setScript

      public void setScript(File script)
      Sets the script file to execute.
      Parameters:
      script - Python script path
    • getRequirements

      @Optional @InputFile public File getRequirements()
      Returns the requirements file used for dependency installation.
      Returns:
      requirements file or null
    • setRequirements

      public void setRequirements(File requirements)
      Sets the requirements file for dependency installation.
      Parameters:
      requirements - requirements file path
    • getWorkDir

      @Internal public File getWorkDir()
      Returns the working directory used for command execution and venv storage.
      Returns:
      working directory or null when default resolution is used
    • getWorkDirPath

      @Optional @Input public String getWorkDirPath()
      Returns the configured working directory path for incremental input tracking.
      Returns:
      absolute path of configured workDir, or null when unset
    • setWorkDir

      public void setWorkDir(File workDir)
      Sets the working directory used for command execution and venv storage.
      Parameters:
      workDir - task working directory
    • getArgs

      @Optional @Input public List<String> getArgs()
      Returns positional arguments passed to the script.
      Returns:
      script arguments
    • setArgs

      public void setArgs(List<String> args)
      Sets positional arguments passed to the script.
      Parameters:
      args - script arguments; null is treated as empty
    • getPythonExecutable

      @Input public String getPythonExecutable()
      Returns the Python executable used to create the virtual environment.
      Returns:
      Python executable path or command
    • setPythonExecutable

      public void setPythonExecutable(String pythonExecutable)
      Sets the Python executable used to create the virtual environment.
      Parameters:
      pythonExecutable - executable path or command