microsoft/hve-core

Public

mirrored from https://github.com/microsoft/hve-coreAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a5c38379bc5d730200b9be8d7b8d86f9bd2bcd21

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

scripts/linting/Invoke-PythonLint.ps1

152lines · modeblame

0a90f573Jason4 months ago1#!/usr/bin/env pwsh
2# Copyright (c) Microsoft Corporation.
3# SPDX-License-Identifier: MIT
4#
5# Invoke-PythonLint.ps1
6#
7# Purpose: Dynamically discovers and lints Python skills using ruff
8# Author: HVE Core Team
9
10#Requires -Version 7.0
11
12[CmdletBinding()]
13param(
14[Parameter(Mandatory = $false)]
15[string]$RepoRoot = (Get-Location).Path,
16
17[Parameter(Mandatory = $false)]
18[string]$OutputPath
19)
20
21$ErrorActionPreference = 'Stop'
22
23#region Functions
24
25function Invoke-PythonLint {
26[CmdletBinding()]
27param(
28[Parameter(Mandatory = $true)]
29[string]$RepoRoot,
30
31[Parameter(Mandatory = $false)]
32[string]$OutputPath
33)
34
35Push-Location $RepoRoot
36try {
37# Find all directories with pyproject.toml
17bbe7a3Bill Berry4 months ago38$pythonSkills = Get-ChildItem -Path . -Filter 'pyproject.toml' -Recurse -Force -File |
0a90f573Jason4 months ago39Where-Object { $_.FullName -notmatch 'node_modules' } |
40ForEach-Object { $_.Directory.FullName }
41
42if (-not $pythonSkills) {
43Write-Host 'No Python skills found (no pyproject.toml files detected)' -ForegroundColor Yellow
44return @{ success = $true; skillsChecked = 0; errors = @() }
45}
46
47Write-Host "Found $($pythonSkills.Count) Python skill(s):" -ForegroundColor Cyan
48$pythonSkills | ForEach-Object { Write-Host " - $_" -ForegroundColor Gray }
49
3b922278Bill Berry4 months ago50# Check if ruff is globally available (used as fallback when skill has no venv)
51$globalRuff = Get-Command ruff -ErrorAction SilentlyContinue
0a90f573Jason4 months ago52
53$results = @{
54success = $true
55skillsChecked = 0
56errors = @()
57details = @()
58}
59
60foreach ($skillPath in $pythonSkills) {
61Write-Host "`nRunning ruff in $skillPath..." -ForegroundColor Cyan
62
63Push-Location $skillPath
64try {
3b922278Bill Berry4 months ago65# Resolve ruff: prefer skill venv, fall back to global
66$ruffCmd = $null
67$venvRuff = Join-Path $skillPath '.venv/bin/ruff'
68$venvRuffWin = Join-Path $skillPath '.venv/Scripts/ruff.exe'
69if (Test-Path $venvRuff) {
70$ruffCmd = $venvRuff
71Write-Host ' Using venv ruff' -ForegroundColor Gray
72} elseif (Test-Path $venvRuffWin) {
73$ruffCmd = $venvRuffWin
74Write-Host ' Using venv ruff' -ForegroundColor Gray
75} elseif ($globalRuff) {
76$ruffCmd = 'ruff'
77}
78
79if (-not $ruffCmd) {
80Write-Host '❌ ruff not available (no .venv and not installed globally)' -ForegroundColor Red
81$results.success = $false
82$results.errors += $skillPath
83continue
84}
85
86$output = & $ruffCmd check . 2>&1
0a90f573Jason4 months ago87$exitCode = $LASTEXITCODE
88
89$result = @{
90path = $skillPath
91passed = ($exitCode -eq 0)
92output = $output | Out-String
93}
94
95$results.details += $result
96$results.skillsChecked++
97
98if ($exitCode -ne 0) {
99Write-Host "$output" -ForegroundColor Red
100Write-Host '❌ Linting issues found' -ForegroundColor Red
101$results.success = $false
102$results.errors += $skillPath
103} else {
104if ($output) {
105Write-Host "$output"
106}
107Write-Host '✓ No linting issues' -ForegroundColor Green
108}
109} catch {
110Write-Host "Error running ruff: $_" -ForegroundColor Red
111$results.success = $false
112$results.errors += "$skillPath - error: $_"
113} finally {
114Pop-Location
115}
116}
117
d337e1d2Bill Berry4 months ago118# Default to logs directory when no OutputPath specified
119if (-not $OutputPath) {
120$logsDir = Join-Path -Path $RepoRoot -ChildPath 'logs'
121if (-not (Test-Path $logsDir)) {
122New-Item -ItemType Directory -Path $logsDir -Force | Out-Null
123}
124$OutputPath = Join-Path -Path $logsDir -ChildPath 'python-lint-results.json'
0a90f573Jason4 months ago125}
d337e1d2Bill Berry4 months ago126$results | ConvertTo-Json -Depth 3 | Out-File $OutputPath -Encoding UTF8
127Write-Host "📊 Results written to: $OutputPath" -ForegroundColor Cyan
0a90f573Jason4 months ago128
129return $results
130} finally {
131Pop-Location
132}
133}
134
135#endregion
136
137#region Main Execution
138
139# Don't run main logic if dot-sourced for testing
140if ($MyInvocation.InvocationName -ne '.') {
141$result = Invoke-PythonLint -RepoRoot $RepoRoot -OutputPath $OutputPath
142
143if ($result.success) {
144Write-Host "`n✅ All Python skills passed linting" -ForegroundColor Green
145exit 0
146} else {
147Write-Host "`n❌ Linting completed with errors" -ForegroundColor Red
148exit 1
149}
150}
151
152#endregion