microsoft/hve-core
Publicmirrored fromhttps://github.com/microsoft/hve-coreAvailable
scripts/linting/Modules/PythonLintHelpers.psm1
139lines · modecode
| 1 | # Copyright (c) Microsoft Corporation. |
| 2 | # SPDX-License-Identifier: MIT |
| 3 | |
| 4 | # PythonLintHelpers.psm1 |
| 5 | # |
| 6 | # Purpose: Shared helper functions for Python lint and lint-fix wrappers |
| 7 | # Author: HVE Core Team |
| 8 | |
| 9 | #Requires -Version 7.0 |
| 10 | |
| 11 | Import-Module (Join-Path $PSScriptRoot "../../lib/Modules/CIHelpers.psm1") -Force |
| 12 | |
| 13 | function Get-PythonSkill { |
| 14 | <# |
| 15 | .SYNOPSIS |
| 16 | Discovers Python skill directories by locating pyproject.toml files. |
| 17 | |
| 18 | .DESCRIPTION |
| 19 | Recursively scans the repository for pyproject.toml files, excluding |
| 20 | node_modules, and returns the parent directory of each match. |
| 21 | |
| 22 | .PARAMETER RepoRoot |
| 23 | Repository root to scan. |
| 24 | |
| 25 | .OUTPUTS |
| 26 | Array of full directory paths containing pyproject.toml. |
| 27 | #> |
| 28 | [CmdletBinding()] |
| 29 | [OutputType([string[]])] |
| 30 | param( |
| 31 | [Parameter(Mandatory = $true)] |
| 32 | [string]$RepoRoot |
| 33 | ) |
| 34 | |
| 35 | Push-Location $RepoRoot |
| 36 | try { |
| 37 | $skills = Get-ChildItem -Path . -Filter 'pyproject.toml' -Recurse -Force -File | |
| 38 | Where-Object { $_.FullName -notmatch 'node_modules' } | |
| 39 | ForEach-Object { $_.Directory.FullName } |
| 40 | return @($skills) |
| 41 | } finally { |
| 42 | Pop-Location |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | function Resolve-RuffCommand { |
| 47 | <# |
| 48 | .SYNOPSIS |
| 49 | Resolves the ruff command to use for a given skill directory. |
| 50 | |
| 51 | .DESCRIPTION |
| 52 | Prefers the skill's own .venv ruff binary (Linux or Windows path), then |
| 53 | falls back to a globally installed ruff. Returns $null when neither is |
| 54 | available. |
| 55 | |
| 56 | .PARAMETER SkillPath |
| 57 | Skill directory to inspect. |
| 58 | |
| 59 | .PARAMETER GlobalRuffAvailable |
| 60 | Whether ruff is available on PATH. |
| 61 | |
| 62 | .OUTPUTS |
| 63 | String path or 'ruff', or $null when ruff is not available. |
| 64 | #> |
| 65 | [CmdletBinding()] |
| 66 | [OutputType([string])] |
| 67 | param( |
| 68 | [Parameter(Mandatory = $true)] |
| 69 | [string]$SkillPath, |
| 70 | |
| 71 | [Parameter(Mandatory = $true)] |
| 72 | [bool]$GlobalRuffAvailable |
| 73 | ) |
| 74 | |
| 75 | $venvRuff = Join-Path $SkillPath '.venv/bin/ruff' |
| 76 | $venvRuffWin = Join-Path $SkillPath '.venv/Scripts/ruff.exe' |
| 77 | |
| 78 | if (Test-Path $venvRuff) { return $venvRuff } |
| 79 | if (Test-Path $venvRuffWin) { return $venvRuffWin } |
| 80 | if ($GlobalRuffAvailable) { return 'ruff' } |
| 81 | return $null |
| 82 | } |
| 83 | |
| 84 | function Write-PythonLintResults { |
| 85 | <# |
| 86 | .SYNOPSIS |
| 87 | Writes Python lint results to a JSON file, ensuring the parent directory exists. |
| 88 | |
| 89 | .DESCRIPTION |
| 90 | Resolves the output path (defaulting to logs/<DefaultFileName> under |
| 91 | RepoRoot when OutputPath is empty), creates the parent directory if |
| 92 | missing, then writes results as JSON. |
| 93 | |
| 94 | .PARAMETER Results |
| 95 | Hashtable of results to serialize. |
| 96 | |
| 97 | .PARAMETER RepoRoot |
| 98 | Repository root used to compute the default logs directory. |
| 99 | |
| 100 | .PARAMETER OutputPath |
| 101 | Optional explicit output path. |
| 102 | |
| 103 | .PARAMETER DefaultFileName |
| 104 | Default file name to use when OutputPath is empty. |
| 105 | |
| 106 | .OUTPUTS |
| 107 | Resolved output path string. |
| 108 | #> |
| 109 | [CmdletBinding()] |
| 110 | [OutputType([string])] |
| 111 | param( |
| 112 | [Parameter(Mandatory = $true)] |
| 113 | [hashtable]$Results, |
| 114 | |
| 115 | [Parameter(Mandatory = $true)] |
| 116 | [string]$RepoRoot, |
| 117 | |
| 118 | [Parameter(Mandatory = $false)] |
| 119 | [string]$OutputPath, |
| 120 | |
| 121 | [Parameter(Mandatory = $true)] |
| 122 | [string]$DefaultFileName |
| 123 | ) |
| 124 | |
| 125 | if (-not $OutputPath) { |
| 126 | $logsDir = Join-Path -Path $RepoRoot -ChildPath 'logs' |
| 127 | $OutputPath = Join-Path -Path $logsDir -ChildPath $DefaultFileName |
| 128 | } |
| 129 | |
| 130 | $parentDir = Split-Path -Parent $OutputPath |
| 131 | if ($parentDir -and -not (Test-Path $parentDir)) { |
| 132 | New-Item -ItemType Directory -Path $parentDir -Force | Out-Null |
| 133 | } |
| 134 | |
| 135 | $Results | ConvertTo-Json -Depth 3 | Out-File $OutputPath -Encoding UTF8 |
| 136 | return $OutputPath |
| 137 | } |
| 138 | |
| 139 | Export-ModuleMember -Function Get-PythonSkill, Resolve-RuffCommand, Write-PythonLintResults |
| 140 | |