tasks/python.tasks.ps1

$PythonPoetryProject = ""
$PythonPublishUser = "user"
$PythonRepositoryName = "ci-python-feed"
$PythonPackageRepoUrl = ""      # e.g. https://pkgs.dev.azure.com/myOrg/Project/_packaging/myfeed/pypi/upload


task InstallPythonPoetry {
    $poetry = Get-Command poetry -ErrorAction SilentlyContinue
    if (!$poetry) {
        Write-Build White "Installing Poetry"
        Invoke-WebRequest -Uri https://install.python-poetry.org/ -OutFile get-poetry.py
        python get-poetry.py --yes
        Remove-Item get-poetry.py
    }
    else {
        Write-Build Green "Poetry already installed"
    }
}

task InitialisePythonPoetry -If { $PythonPoetryProject -ne "" } InstallPythonPoetry,{

    if (!(Test-Path (Join-Path $PythonPoetryProject "pyproject.toml"))) {
        throw "pyproject.toml not found in $PythonPoetryProject"
    }

    $env:POETRY_VIRTUALENVS_IN_PROJECT = "true"     # review whether we want to keep this?

    # Define the global poetry arguments we will use for all poetry commands
    $script:poetryGlobalArgs = @(
        "--no-interaction"
        "--directory=$PythonPoetryProject"
        "-v"
    )
    Write-Build White "poetryGlobalArgs: $poetryGlobalArgs"

    exec { & poetry install @poetryGlobalArgs --with dev,test }
}

task RunFlake8 -If { $PythonPoetryProject -ne "" } InitialisePythonPoetry,{
    Write-Build White "Running flake8"
    Push-Location $PythonPoetryProject
    exec { & poetry run --no-interaction -v flake8 src -vv }
    Pop-Location
}

task RunPyTests -If { $PythonPoetryProject -ne "" } InitialisePythonPoetry,{
    Push-Location $PythonPoetryProject
    exec {
        & poetry run --no-interaction -v `
            pytest `
            --cov=src `
            --cov-report=xml:$CoverageDir/coverage.xml `
            --cov-report=term-missing `
            --junitxml=$here/pytest-test-results.xml
    }
    Pop-Location
}

task BuildPythonPackages -If { $PythonPoetryProject -ne "" } GitVersion,InitialisePythonPoetry,{
    if (Test-Path (Join-Path $PythonPoetryProject "dist")) {
        Remove-Item (Join-Path $PythonPoetryProject "dist") -Recurse -Force
    }
    exec { & poetry version @poetryGlobalArgs $GitVersion.MajorMinorPatch }
    # For the moment we accept that wheel files will be created in the 'dist' directory,
    # rather than ending up in $PackagesDir
    exec { & poetry build @poetryGlobalArgs --format=wheel }
}

task PublishPythonPackages -If { $PythonPoetryProject -ne "" } InitialisePythonPoetry,{
    if (!$PythonPackageRepoUrl) {
        Write-Warning "PythonPackageRepoUrl build variable not set, skipping publish"
    }
    elseif (!$env:PYTHON_PACKAGE_REPOSITORY_KEY) {
        Write-Warning "PYTHON_PACKAGE_REPOSITORY_KEY environment variable not set, skipping publish"
    }
    else {
        exec {
            Write-Build White "Registering Python repository $PythonRepositoryName -> $PythonPackageRepoUrl"
            & poetry config @poetryGlobalArgs repositories.$PythonRepositoryName $PythonPackageRepoUrl
        }

        exec {
            Write-Build White "Publishing Python packages to $PythonRepositoryName"
            & poetry publish @poetryGlobalArgs -u $PythonPublishUser -p $env:PYTHON_PACKAGE_REPOSITORY_KEY -r $PythonRepositoryName
        }
    }
}