Skip to main content
Loading...

More PowerShell Posts

#This stuff is pure vibecoded slop, but it's useful

param(
    [Parameter(Mandatory=$true)]
    [string]$Script,

    [switch]$DryRun
)

$OutDir = "clean"
$Cwd = (Get-Location).Path

Write-Host "=== Trace Collector ==="
Write-Host "Script: $Script"
Write-Host "CWD: $Cwd"
Write-Host "DryRun: $DryRun"
Write-Host ""

# Run trace
$trace = python -m trace --trackcalls $Script 2>&1

# Match Windows or Unix-style .py paths
$pattern = '([A-Za-z]:[\\/][^*<>"]+\.py)|([\\/][^*<>"]+\.py)|(\.[\\/][^*<>"]+\.py)'

$files = $trace |
    Select-String -Pattern $pattern -AllMatches |
    ForEach-Object { $_.Matches.Value } |
    Sort-Object -Unique

if ($files.Count -eq 0) {
    Write-Host "⚠ No .py files detected in trace output."
    return
}

if (-not $DryRun) {
    if (-not (Test-Path $OutDir)) {
        New-Item -ItemType Directory -Path $OutDir | Out-Null
    }
}

foreach ($file in $files) {

    # Normalize slashes
    $normalized = $file -replace '/', '\'

    # Skip frozen modules
    if ($normalized -like "<frozen*") {
        continue
    }

    # Only keep files inside the current working directory
    if ($normalized.StartsWith($Cwd)) {

        # Compute relative path
        $relative = $normalized.Substring($Cwd.Length).TrimStart('\')

        $destPath = Join-Path $OutDir $relative
        $destDir = Split-Path $destPath -Parent

        if ($DryRun) {
            Write-Host "[WOULD COPY] $normalized  -->  $destPath"
        } else {
            # Create directory structure
            if (-not (Test-Path $destDir)) {
                New-Item -ItemType Directory -Path $destDir -Force | Out-Null
            }

            Write-Host "[COPYING] $normalized"
            Copy-Item $normalized -Destination $destPath -Force
        }
    }
    else {
        Write-Host "[IGNORED: outside CWD] $normalized"
    }
}

Write-Host ""
Write-Host "=== DONE ==="
if ($DryRun) {
    Write-Host "Dry run complete. No files were copied."
} else {
    Write-Host "Files copied to '$OutDir' with preserved relative paths."
}